PropfindCompressionPluginTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  8. use OCA\DAV\Connector\Sabre\PropfindCompressionPlugin;
  9. use Sabre\HTTP\Request;
  10. use Sabre\HTTP\Response;
  11. use Test\TestCase;
  12. class PropfindCompressionPluginTest extends TestCase {
  13. /** @var PropfindCompressionPlugin */
  14. private $plugin;
  15. protected function setUp(): void {
  16. parent::setUp();
  17. $this->plugin = new PropfindCompressionPlugin();
  18. }
  19. public function testNoHeader(): void {
  20. $request = $this->createMock(Request::class);
  21. $response = $this->createMock(Response::class);
  22. $request->method('getHeader')
  23. ->with('Accept-Encoding')
  24. ->willReturn(null);
  25. $response->expects($this->never())
  26. ->method($this->anything());
  27. $result = $this->plugin->compressResponse($request, $response);
  28. $this->assertSame($response, $result);
  29. }
  30. public function testHeaderButNoGzip(): void {
  31. $request = $this->createMock(Request::class);
  32. $response = $this->createMock(Response::class);
  33. $request->method('getHeader')
  34. ->with('Accept-Encoding')
  35. ->willReturn('deflate');
  36. $response->expects($this->never())
  37. ->method($this->anything());
  38. $result = $this->plugin->compressResponse($request, $response);
  39. $this->assertSame($response, $result);
  40. }
  41. public function testHeaderGzipButNoStringBody(): void {
  42. $request = $this->createMock(Request::class);
  43. $response = $this->createMock(Response::class);
  44. $request->method('getHeader')
  45. ->with('Accept-Encoding')
  46. ->willReturn('deflate');
  47. $response->method('getBody')
  48. ->willReturn(5);
  49. $result = $this->plugin->compressResponse($request, $response);
  50. $this->assertSame($response, $result);
  51. }
  52. public function testProperGzip(): void {
  53. $request = $this->createMock(Request::class);
  54. $response = $this->createMock(Response::class);
  55. $request->method('getHeader')
  56. ->with('Accept-Encoding')
  57. ->willReturn('gzip, deflate');
  58. $response->method('getBody')
  59. ->willReturn('my gzip test');
  60. $response->expects($this->once())
  61. ->method('setHeader')
  62. ->with(
  63. $this->equalTo('Content-Encoding'),
  64. $this->equalTo('gzip')
  65. );
  66. $response->expects($this->once())
  67. ->method('setBody')
  68. ->with($this->callback(function ($data) {
  69. $orig = gzdecode($data);
  70. return $orig === 'my gzip test';
  71. }));
  72. $result = $this->plugin->compressResponse($request, $response);
  73. $this->assertSame($response, $result);
  74. }
  75. }