1
0

ResponseTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\AppFramework\Http;
  8. use OCP\AppFramework\Http;
  9. use OCP\AppFramework\Http\Response;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. class ResponseTest extends \Test\TestCase {
  12. /**
  13. * @var \OCP\AppFramework\Http\Response
  14. */
  15. private $childResponse;
  16. protected function setUp(): void {
  17. parent::setUp();
  18. $this->childResponse = new Response();
  19. }
  20. public function testAddHeader() {
  21. $this->childResponse->addHeader(' hello ', 'world');
  22. $headers = $this->childResponse->getHeaders();
  23. $this->assertEquals('world', $headers['hello']);
  24. }
  25. public function testSetHeaders() {
  26. $expected = [
  27. 'Last-Modified' => 1,
  28. 'ETag' => 3,
  29. 'Something-Else' => 'hi',
  30. 'X-Robots-Tag' => 'noindex, nofollow',
  31. 'Cache-Control' => 'no-cache, no-store, must-revalidate',
  32. ];
  33. $this->childResponse->setHeaders($expected);
  34. $expected['Content-Security-Policy'] = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  35. $expected['Feature-Policy'] = "autoplay 'none';camera 'none';fullscreen 'none';geolocation 'none';microphone 'none';payment 'none'";
  36. $headers = $this->childResponse->getHeaders();
  37. unset($headers['X-Request-Id']);
  38. $this->assertEquals($expected, $headers);
  39. }
  40. public function testOverwriteCsp() {
  41. $expected = [
  42. 'Content-Security-Policy' => "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'self' 'unsafe-inline';style-src 'self' 'unsafe-inline';img-src 'self';font-src 'self' data:;connect-src 'self';media-src 'self'",
  43. ];
  44. $policy = new Http\ContentSecurityPolicy();
  45. $this->childResponse->setContentSecurityPolicy($policy);
  46. $headers = $this->childResponse->getHeaders();
  47. $this->assertEquals(array_merge($expected, $headers), $headers);
  48. }
  49. public function testGetCsp() {
  50. $policy = new Http\ContentSecurityPolicy();
  51. $this->childResponse->setContentSecurityPolicy($policy);
  52. $this->assertEquals($policy, $this->childResponse->getContentSecurityPolicy());
  53. }
  54. public function testGetCspEmpty() {
  55. $this->assertEquals(new Http\EmptyContentSecurityPolicy(), $this->childResponse->getContentSecurityPolicy());
  56. }
  57. public function testAddHeaderValueNullDeletesIt() {
  58. $this->childResponse->addHeader('hello', 'world');
  59. $this->childResponse->addHeader('hello', null);
  60. $this->assertEquals(5, count($this->childResponse->getHeaders()));
  61. }
  62. public function testCacheHeadersAreDisabledByDefault() {
  63. $headers = $this->childResponse->getHeaders();
  64. $this->assertEquals('no-cache, no-store, must-revalidate', $headers['Cache-Control']);
  65. }
  66. public function testAddCookie() {
  67. $this->childResponse->addCookie('foo', 'bar');
  68. $this->childResponse->addCookie('bar', 'foo', new \DateTime('1970-01-01'));
  69. $expectedResponse = [
  70. 'foo' => [
  71. 'value' => 'bar',
  72. 'expireDate' => null,
  73. 'sameSite' => 'Lax',
  74. ],
  75. 'bar' => [
  76. 'value' => 'foo',
  77. 'expireDate' => new \DateTime('1970-01-01'),
  78. 'sameSite' => 'Lax',
  79. ]
  80. ];
  81. $this->assertEquals($expectedResponse, $this->childResponse->getCookies());
  82. }
  83. public function testSetCookies() {
  84. $expected = [
  85. 'foo' => [
  86. 'value' => 'bar',
  87. 'expireDate' => null,
  88. ],
  89. 'bar' => [
  90. 'value' => 'foo',
  91. 'expireDate' => new \DateTime('1970-01-01')
  92. ]
  93. ];
  94. $this->childResponse->setCookies($expected);
  95. $cookies = $this->childResponse->getCookies();
  96. $this->assertEquals($expected, $cookies);
  97. }
  98. public function testInvalidateCookie() {
  99. $this->childResponse->addCookie('foo', 'bar');
  100. $this->childResponse->invalidateCookie('foo');
  101. $expected = [
  102. 'foo' => [
  103. 'value' => 'expired',
  104. 'expireDate' => new \DateTime('1971-01-01'),
  105. 'sameSite' => 'Lax',
  106. ]
  107. ];
  108. $cookies = $this->childResponse->getCookies();
  109. $this->assertEquals($expected, $cookies);
  110. }
  111. public function testInvalidateCookies() {
  112. $this->childResponse->addCookie('foo', 'bar');
  113. $this->childResponse->addCookie('bar', 'foo');
  114. $expected = [
  115. 'foo' => [
  116. 'value' => 'bar',
  117. 'expireDate' => null,
  118. 'sameSite' => 'Lax',
  119. ],
  120. 'bar' => [
  121. 'value' => 'foo',
  122. 'expireDate' => null,
  123. 'sameSite' => 'Lax',
  124. ]
  125. ];
  126. $cookies = $this->childResponse->getCookies();
  127. $this->assertEquals($expected, $cookies);
  128. $this->childResponse->invalidateCookies(['foo', 'bar']);
  129. $expected = [
  130. 'foo' => [
  131. 'value' => 'expired',
  132. 'expireDate' => new \DateTime('1971-01-01'),
  133. 'sameSite' => 'Lax',
  134. ],
  135. 'bar' => [
  136. 'value' => 'expired',
  137. 'expireDate' => new \DateTime('1971-01-01'),
  138. 'sameSite' => 'Lax',
  139. ]
  140. ];
  141. $cookies = $this->childResponse->getCookies();
  142. $this->assertEquals($expected, $cookies);
  143. }
  144. public function testRenderReturnNullByDefault() {
  145. $this->assertEquals(null, $this->childResponse->render());
  146. }
  147. public function testGetStatus() {
  148. $default = $this->childResponse->getStatus();
  149. $this->childResponse->setStatus(Http::STATUS_NOT_FOUND);
  150. $this->assertEquals(Http::STATUS_OK, $default);
  151. $this->assertEquals(Http::STATUS_NOT_FOUND, $this->childResponse->getStatus());
  152. }
  153. public function testGetEtag() {
  154. $this->childResponse->setEtag('hi');
  155. $this->assertSame('hi', $this->childResponse->getEtag());
  156. }
  157. public function testGetLastModified() {
  158. $lastModified = new \DateTime('now', new \DateTimeZone('GMT'));
  159. $lastModified->setTimestamp(1);
  160. $this->childResponse->setLastModified($lastModified);
  161. $this->assertEquals($lastModified, $this->childResponse->getLastModified());
  162. }
  163. public function testCacheSecondsZero() {
  164. $this->childResponse->cacheFor(0);
  165. $headers = $this->childResponse->getHeaders();
  166. $this->assertEquals('no-cache, no-store, must-revalidate', $headers['Cache-Control']);
  167. $this->assertFalse(isset($headers['Expires']));
  168. }
  169. public function testCacheSeconds() {
  170. $time = $this->createMock(ITimeFactory::class);
  171. $time->method('getTime')
  172. ->willReturn(1234567);
  173. $this->overwriteService(ITimeFactory::class, $time);
  174. $this->childResponse->cacheFor(33);
  175. $headers = $this->childResponse->getHeaders();
  176. $this->assertEquals('private, max-age=33, must-revalidate', $headers['Cache-Control']);
  177. $this->assertEquals('Thu, 15 Jan 1970 06:56:40 +0000', $headers['Expires']);
  178. }
  179. public function testEtagLastModifiedHeaders() {
  180. $lastModified = new \DateTime('now', new \DateTimeZone('GMT'));
  181. $lastModified->setTimestamp(1);
  182. $this->childResponse->setLastModified($lastModified);
  183. $headers = $this->childResponse->getHeaders();
  184. $this->assertEquals('Thu, 01 Jan 1970 00:00:01 +0000', $headers['Last-Modified']);
  185. }
  186. public function testChainability() {
  187. $lastModified = new \DateTime('now', new \DateTimeZone('GMT'));
  188. $lastModified->setTimestamp(1);
  189. $this->childResponse->setEtag('hi')
  190. ->setStatus(Http::STATUS_NOT_FOUND)
  191. ->setLastModified($lastModified)
  192. ->cacheFor(33)
  193. ->addHeader('hello', 'world');
  194. $headers = $this->childResponse->getHeaders();
  195. $this->assertEquals('world', $headers['hello']);
  196. $this->assertEquals(Http::STATUS_NOT_FOUND, $this->childResponse->getStatus());
  197. $this->assertEquals('hi', $this->childResponse->getEtag());
  198. $this->assertEquals('Thu, 01 Jan 1970 00:00:01 +0000', $headers['Last-Modified']);
  199. $this->assertEquals('private, max-age=33, must-revalidate',
  200. $headers['Cache-Control']);
  201. }
  202. public function testThrottle() {
  203. $this->assertFalse($this->childResponse->isThrottled());
  204. $this->childResponse->throttle();
  205. $this->assertTrue($this->childResponse->isThrottled());
  206. }
  207. public function testGetThrottleMetadata() {
  208. $this->childResponse->throttle(['foo' => 'bar']);
  209. $this->assertSame(['foo' => 'bar'], $this->childResponse->getThrottleMetadata());
  210. }
  211. }