CompressionMiddlewareTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 Test\AppFramework\Middleware;
  8. use OC\AppFramework\Middleware\CompressionMiddleware;
  9. use OC\AppFramework\OCS\V1Response;
  10. use OC\AppFramework\OCS\V2Response;
  11. use OCP\AppFramework\Controller;
  12. use OCP\AppFramework\Http;
  13. use OCP\AppFramework\Http\DataResponse;
  14. use OCP\AppFramework\Http\JSONResponse;
  15. use OCP\IRequest;
  16. class CompressionMiddlewareTest extends \Test\TestCase {
  17. /** @var IRequest */
  18. private $request;
  19. /** @var Controller */
  20. private $controller;
  21. /** @var CompressionMiddleware */
  22. private $middleWare;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $this->request = $this->createMock(IRequest::class);
  26. $this->middleWare = new CompressionMiddleware(
  27. $this->request
  28. );
  29. $this->controller = $this->createMock(Controller::class);
  30. }
  31. public function testGzipOCSV1(): void {
  32. $this->request->method('getHeader')
  33. ->with('Accept-Encoding')
  34. ->willReturn('gzip');
  35. $response = $this->createMock(V1Response::class);
  36. $response->expects($this->once())
  37. ->method('addHeader')
  38. ->with('Content-Encoding', 'gzip');
  39. $response->method('getStatus')
  40. ->willReturn(Http::STATUS_OK);
  41. $this->middleWare->beforeController($this->controller, 'myMethod');
  42. $this->middleWare->afterController($this->controller, 'myMethod', $response);
  43. $output = 'myoutput';
  44. $result = $this->middleWare->beforeOutput($this->controller, 'myMethod', $output);
  45. $this->assertSame($output, gzdecode($result));
  46. }
  47. public function testGzipOCSV2(): void {
  48. $this->request->method('getHeader')
  49. ->with('Accept-Encoding')
  50. ->willReturn('gzip');
  51. $response = $this->createMock(V2Response::class);
  52. $response->expects($this->once())
  53. ->method('addHeader')
  54. ->with('Content-Encoding', 'gzip');
  55. $response->method('getStatus')
  56. ->willReturn(Http::STATUS_OK);
  57. $this->middleWare->beforeController($this->controller, 'myMethod');
  58. $this->middleWare->afterController($this->controller, 'myMethod', $response);
  59. $output = 'myoutput';
  60. $result = $this->middleWare->beforeOutput($this->controller, 'myMethod', $output);
  61. $this->assertSame($output, gzdecode($result));
  62. }
  63. public function testGzipJSONResponse(): void {
  64. $this->request->method('getHeader')
  65. ->with('Accept-Encoding')
  66. ->willReturn('gzip');
  67. $response = $this->createMock(JSONResponse::class);
  68. $response->expects($this->once())
  69. ->method('addHeader')
  70. ->with('Content-Encoding', 'gzip');
  71. $response->method('getStatus')
  72. ->willReturn(Http::STATUS_OK);
  73. $this->middleWare->beforeController($this->controller, 'myMethod');
  74. $this->middleWare->afterController($this->controller, 'myMethod', $response);
  75. $output = 'myoutput';
  76. $result = $this->middleWare->beforeOutput($this->controller, 'myMethod', $output);
  77. $this->assertSame($output, gzdecode($result));
  78. }
  79. public function testNoGzipDataResponse(): void {
  80. $this->request->method('getHeader')
  81. ->with('Accept-Encoding')
  82. ->willReturn('gzip');
  83. $response = $this->createMock(DataResponse::class);
  84. $response->expects($this->never())
  85. ->method('addHeader');
  86. $response->method('getStatus')
  87. ->willReturn(Http::STATUS_OK);
  88. $this->middleWare->beforeController($this->controller, 'myMethod');
  89. $this->middleWare->afterController($this->controller, 'myMethod', $response);
  90. $output = 'myoutput';
  91. $result = $this->middleWare->beforeOutput($this->controller, 'myMethod', $output);
  92. $this->assertSame($output, $result);
  93. }
  94. public function testNoGzipNo200(): void {
  95. $this->request->method('getHeader')
  96. ->with('Accept-Encoding')
  97. ->willReturn('gzip');
  98. $response = $this->createMock(JSONResponse::class);
  99. $response->expects($this->never())
  100. ->method('addHeader');
  101. $response->method('getStatus')
  102. ->willReturn(Http::STATUS_NOT_FOUND);
  103. $this->middleWare->beforeController($this->controller, 'myMethod');
  104. $this->middleWare->afterController($this->controller, 'myMethod', $response);
  105. $output = 'myoutput';
  106. $result = $this->middleWare->beforeOutput($this->controller, 'myMethod', $output);
  107. $this->assertSame($output, $result);
  108. }
  109. }