PropfindCompressionPluginTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 OCA\DAV\Tests\unit\Connector\Sabre;
  25. use OCA\DAV\Connector\Sabre\PropfindCompressionPlugin;
  26. use Sabre\HTTP\Request;
  27. use Sabre\HTTP\Response;
  28. use Test\TestCase;
  29. class PropfindCompressionPluginTest extends TestCase {
  30. /** @var PropfindCompressionPlugin */
  31. private $plugin;
  32. protected function setUp(): void {
  33. parent::setUp();
  34. $this->plugin = new PropfindCompressionPlugin();
  35. }
  36. public function testNoHeader(): void {
  37. $request = $this->createMock(Request::class);
  38. $response = $this->createMock(Response::class);
  39. $request->method('getHeader')
  40. ->with('Accept-Encoding')
  41. ->willReturn(null);
  42. $response->expects($this->never())
  43. ->method($this->anything());
  44. $result = $this->plugin->compressResponse($request, $response);
  45. $this->assertSame($response, $result);
  46. }
  47. public function testHeaderButNoGzip(): void {
  48. $request = $this->createMock(Request::class);
  49. $response = $this->createMock(Response::class);
  50. $request->method('getHeader')
  51. ->with('Accept-Encoding')
  52. ->willReturn('deflate');
  53. $response->expects($this->never())
  54. ->method($this->anything());
  55. $result = $this->plugin->compressResponse($request, $response);
  56. $this->assertSame($response, $result);
  57. }
  58. public function testHeaderGzipButNoStringBody(): void {
  59. $request = $this->createMock(Request::class);
  60. $response = $this->createMock(Response::class);
  61. $request->method('getHeader')
  62. ->with('Accept-Encoding')
  63. ->willReturn('deflate');
  64. $response->method('getBody')
  65. ->willReturn(5);
  66. $result = $this->plugin->compressResponse($request, $response);
  67. $this->assertSame($response, $result);
  68. }
  69. public function testProperGzip(): void {
  70. $request = $this->createMock(Request::class);
  71. $response = $this->createMock(Response::class);
  72. $request->method('getHeader')
  73. ->with('Accept-Encoding')
  74. ->willReturn('gzip, deflate');
  75. $response->method('getBody')
  76. ->willReturn('my gzip test');
  77. $response->expects($this->once())
  78. ->method('setHeader')
  79. ->with(
  80. $this->equalTo('Content-Encoding'),
  81. $this->equalTo('gzip')
  82. );
  83. $response->expects($this->once())
  84. ->method('setBody')
  85. ->with($this->callback(function ($data) {
  86. $orig = gzdecode($data);
  87. return $orig === 'my gzip test';
  88. }));
  89. $result = $this->plugin->compressResponse($request, $response);
  90. $this->assertSame($response, $result);
  91. }
  92. }