You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CompressionMiddlewareTest.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace Test\AppFramework\Middleware;
  25. use OC\AppFramework\Middleware\CompressionMiddleware;
  26. use OC\AppFramework\OCS\V1Response;
  27. use OC\AppFramework\OCS\V2Response;
  28. use OCP\AppFramework\Controller;
  29. use OCP\AppFramework\Http;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\AppFramework\Http\JSONResponse;
  32. use OCP\IRequest;
  33. class CompressionMiddlewareTest extends \Test\TestCase {
  34. /** @var IRequest */
  35. private $request;
  36. /** @var Controller */
  37. private $controller;
  38. /** @var CompressionMiddleware */
  39. private $middleWare;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $this->request = $this->createMock(IRequest::class);
  43. $this->middleWare = new CompressionMiddleware(
  44. $this->request
  45. );
  46. $this->controller = $this->createMock(Controller::class);
  47. }
  48. public function testGzipOCSV1() {
  49. $this->request->method('getHeader')
  50. ->with('Accept-Encoding')
  51. ->willReturn('gzip');
  52. $response = $this->createMock(V1Response::class);
  53. $response->expects($this->once())
  54. ->method('addHeader')
  55. ->with('Content-Encoding', 'gzip');
  56. $response->method('getStatus')
  57. ->willReturn(Http::STATUS_OK);
  58. $this->middleWare->beforeController($this->controller, 'myMethod');
  59. $this->middleWare->afterController($this->controller, 'myMethod', $response);
  60. $output = 'myoutput';
  61. $result = $this->middleWare->beforeOutput($this->controller, 'myMethod', $output);
  62. $this->assertSame($output, gzdecode($result));
  63. }
  64. public function testGzipOCSV2() {
  65. $this->request->method('getHeader')
  66. ->with('Accept-Encoding')
  67. ->willReturn('gzip');
  68. $response = $this->createMock(V2Response::class);
  69. $response->expects($this->once())
  70. ->method('addHeader')
  71. ->with('Content-Encoding', 'gzip');
  72. $response->method('getStatus')
  73. ->willReturn(Http::STATUS_OK);
  74. $this->middleWare->beforeController($this->controller, 'myMethod');
  75. $this->middleWare->afterController($this->controller, 'myMethod', $response);
  76. $output = 'myoutput';
  77. $result = $this->middleWare->beforeOutput($this->controller, 'myMethod', $output);
  78. $this->assertSame($output, gzdecode($result));
  79. }
  80. public function testGzipJSONResponse() {
  81. $this->request->method('getHeader')
  82. ->with('Accept-Encoding')
  83. ->willReturn('gzip');
  84. $response = $this->createMock(JSONResponse::class);
  85. $response->expects($this->once())
  86. ->method('addHeader')
  87. ->with('Content-Encoding', 'gzip');
  88. $response->method('getStatus')
  89. ->willReturn(Http::STATUS_OK);
  90. $this->middleWare->beforeController($this->controller, 'myMethod');
  91. $this->middleWare->afterController($this->controller, 'myMethod', $response);
  92. $output = 'myoutput';
  93. $result = $this->middleWare->beforeOutput($this->controller, 'myMethod', $output);
  94. $this->assertSame($output, gzdecode($result));
  95. }
  96. public function testNoGzipDataResponse() {
  97. $this->request->method('getHeader')
  98. ->with('Accept-Encoding')
  99. ->willReturn('gzip');
  100. $response = $this->createMock(DataResponse::class);
  101. $response->expects($this->never())
  102. ->method('addHeader');
  103. $response->method('getStatus')
  104. ->willReturn(Http::STATUS_OK);
  105. $this->middleWare->beforeController($this->controller, 'myMethod');
  106. $this->middleWare->afterController($this->controller, 'myMethod', $response);
  107. $output = 'myoutput';
  108. $result = $this->middleWare->beforeOutput($this->controller, 'myMethod', $output);
  109. $this->assertSame($output, $result);
  110. }
  111. public function testNoGzipNo200() {
  112. $this->request->method('getHeader')
  113. ->with('Accept-Encoding')
  114. ->willReturn('gzip');
  115. $response = $this->createMock(JSONResponse::class);
  116. $response->expects($this->never())
  117. ->method('addHeader');
  118. $response->method('getStatus')
  119. ->willReturn(Http::STATUS_NOT_FOUND);
  120. $this->middleWare->beforeController($this->controller, 'myMethod');
  121. $this->middleWare->afterController($this->controller, 'myMethod', $response);
  122. $output = 'myoutput';
  123. $result = $this->middleWare->beforeOutput($this->controller, 'myMethod', $output);
  124. $this->assertSame($output, $result);
  125. }
  126. }