ResponseTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\AppFramework\Http;
  23. use OCP\AppFramework\Http;
  24. use OCP\AppFramework\Http\Response;
  25. use OCP\AppFramework\Utility\ITimeFactory;
  26. class ResponseTest extends \Test\TestCase {
  27. /**
  28. * @var \OCP\AppFramework\Http\Response
  29. */
  30. private $childResponse;
  31. protected function setUp(): void {
  32. parent::setUp();
  33. $this->childResponse = new Response();
  34. }
  35. public function testAddHeader() {
  36. $this->childResponse->addHeader(' hello ', 'world');
  37. $headers = $this->childResponse->getHeaders();
  38. $this->assertEquals('world', $headers['hello']);
  39. }
  40. public function testSetHeaders() {
  41. $expected = [
  42. 'Last-Modified' => 1,
  43. 'ETag' => 3,
  44. 'Something-Else' => 'hi',
  45. 'X-Robots-Tag' => 'noindex, nofollow',
  46. ];
  47. $this->childResponse->setHeaders($expected);
  48. $headers = $this->childResponse->getHeaders();
  49. $expected['Content-Security-Policy'] = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  50. $expected['Feature-Policy'] = "autoplay 'none';camera 'none';fullscreen 'none';geolocation 'none';microphone 'none';payment 'none'";
  51. $this->assertEquals($expected, $headers);
  52. }
  53. public function testOverwriteCsp() {
  54. $expected = [
  55. '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'",
  56. ];
  57. $policy = new Http\ContentSecurityPolicy();
  58. $this->childResponse->setContentSecurityPolicy($policy);
  59. $headers = $this->childResponse->getHeaders();
  60. $this->assertEquals(array_merge($expected, $headers), $headers);
  61. }
  62. public function testGetCsp() {
  63. $policy = new Http\ContentSecurityPolicy();
  64. $this->childResponse->setContentSecurityPolicy($policy);
  65. $this->assertEquals($policy, $this->childResponse->getContentSecurityPolicy());
  66. }
  67. public function testGetCspEmpty() {
  68. $this->assertEquals(new Http\EmptyContentSecurityPolicy(), $this->childResponse->getContentSecurityPolicy());
  69. }
  70. public function testAddHeaderValueNullDeletesIt() {
  71. $this->childResponse->addHeader('hello', 'world');
  72. $this->childResponse->addHeader('hello', null);
  73. $this->assertEquals(5, count($this->childResponse->getHeaders()));
  74. }
  75. public function testCacheHeadersAreDisabledByDefault() {
  76. $headers = $this->childResponse->getHeaders();
  77. $this->assertEquals('no-cache, no-store, must-revalidate', $headers['Cache-Control']);
  78. }
  79. public function testAddCookie() {
  80. $this->childResponse->addCookie('foo', 'bar');
  81. $this->childResponse->addCookie('bar', 'foo', new \DateTime('1970-01-01'));
  82. $expectedResponse = [
  83. 'foo' => [
  84. 'value' => 'bar',
  85. 'expireDate' => null,
  86. 'sameSite' => 'Lax',
  87. ],
  88. 'bar' => [
  89. 'value' => 'foo',
  90. 'expireDate' => new \DateTime('1970-01-01'),
  91. 'sameSite' => 'Lax',
  92. ]
  93. ];
  94. $this->assertEquals($expectedResponse, $this->childResponse->getCookies());
  95. }
  96. public function testSetCookies() {
  97. $expected = [
  98. 'foo' => [
  99. 'value' => 'bar',
  100. 'expireDate' => null,
  101. ],
  102. 'bar' => [
  103. 'value' => 'foo',
  104. 'expireDate' => new \DateTime('1970-01-01')
  105. ]
  106. ];
  107. $this->childResponse->setCookies($expected);
  108. $cookies = $this->childResponse->getCookies();
  109. $this->assertEquals($expected, $cookies);
  110. }
  111. public function testInvalidateCookie() {
  112. $this->childResponse->addCookie('foo', 'bar');
  113. $this->childResponse->invalidateCookie('foo');
  114. $expected = [
  115. 'foo' => [
  116. 'value' => 'expired',
  117. 'expireDate' => new \DateTime('1971-01-01'),
  118. 'sameSite' => 'Lax',
  119. ]
  120. ];
  121. $cookies = $this->childResponse->getCookies();
  122. $this->assertEquals($expected, $cookies);
  123. }
  124. public function testInvalidateCookies() {
  125. $this->childResponse->addCookie('foo', 'bar');
  126. $this->childResponse->addCookie('bar', 'foo');
  127. $expected = [
  128. 'foo' => [
  129. 'value' => 'bar',
  130. 'expireDate' => null,
  131. 'sameSite' => 'Lax',
  132. ],
  133. 'bar' => [
  134. 'value' => 'foo',
  135. 'expireDate' => null,
  136. 'sameSite' => 'Lax',
  137. ]
  138. ];
  139. $cookies = $this->childResponse->getCookies();
  140. $this->assertEquals($expected, $cookies);
  141. $this->childResponse->invalidateCookies(['foo', 'bar']);
  142. $expected = [
  143. 'foo' => [
  144. 'value' => 'expired',
  145. 'expireDate' => new \DateTime('1971-01-01'),
  146. 'sameSite' => 'Lax',
  147. ],
  148. 'bar' => [
  149. 'value' => 'expired',
  150. 'expireDate' => new \DateTime('1971-01-01'),
  151. 'sameSite' => 'Lax',
  152. ]
  153. ];
  154. $cookies = $this->childResponse->getCookies();
  155. $this->assertEquals($expected, $cookies);
  156. }
  157. public function testRenderReturnNullByDefault() {
  158. $this->assertEquals(null, $this->childResponse->render());
  159. }
  160. public function testGetStatus() {
  161. $default = $this->childResponse->getStatus();
  162. $this->childResponse->setStatus(Http::STATUS_NOT_FOUND);
  163. $this->assertEquals(Http::STATUS_OK, $default);
  164. $this->assertEquals(Http::STATUS_NOT_FOUND, $this->childResponse->getStatus());
  165. }
  166. public function testGetEtag() {
  167. $this->childResponse->setEtag('hi');
  168. $this->assertSame('hi', $this->childResponse->getEtag());
  169. }
  170. public function testGetLastModified() {
  171. $lastModified = new \DateTime('now', new \DateTimeZone('GMT'));
  172. $lastModified->setTimestamp(1);
  173. $this->childResponse->setLastModified($lastModified);
  174. $this->assertEquals($lastModified, $this->childResponse->getLastModified());
  175. }
  176. public function testCacheSecondsZero() {
  177. $this->childResponse->cacheFor(0);
  178. $headers = $this->childResponse->getHeaders();
  179. $this->assertEquals('no-cache, no-store, must-revalidate', $headers['Cache-Control']);
  180. $this->assertFalse(isset($headers['Pragma']));
  181. $this->assertFalse(isset($headers['Expires']));
  182. }
  183. public function testCacheSeconds() {
  184. $time = $this->createMock(ITimeFactory::class);
  185. $time->method('getTime')
  186. ->willReturn(1234567);
  187. $this->overwriteService(ITimeFactory::class, $time);
  188. $this->childResponse->cacheFor(33);
  189. $headers = $this->childResponse->getHeaders();
  190. $this->assertEquals('private, max-age=33, must-revalidate', $headers['Cache-Control']);
  191. $this->assertEquals('private', $headers['Pragma']);
  192. $this->assertEquals('Thu, 15 Jan 1970 06:56:40 +0000', $headers['Expires']);
  193. }
  194. public function testEtagLastModifiedHeaders() {
  195. $lastModified = new \DateTime('now', new \DateTimeZone('GMT'));
  196. $lastModified->setTimestamp(1);
  197. $this->childResponse->setLastModified($lastModified);
  198. $headers = $this->childResponse->getHeaders();
  199. $this->assertEquals('Thu, 01 Jan 1970 00:00:01 +0000', $headers['Last-Modified']);
  200. }
  201. public function testChainability() {
  202. $lastModified = new \DateTime('now', new \DateTimeZone('GMT'));
  203. $lastModified->setTimestamp(1);
  204. $this->childResponse->setEtag('hi')
  205. ->setStatus(Http::STATUS_NOT_FOUND)
  206. ->setLastModified($lastModified)
  207. ->cacheFor(33)
  208. ->addHeader('hello', 'world');
  209. $headers = $this->childResponse->getHeaders();
  210. $this->assertEquals('world', $headers['hello']);
  211. $this->assertEquals(Http::STATUS_NOT_FOUND, $this->childResponse->getStatus());
  212. $this->assertEquals('hi', $this->childResponse->getEtag());
  213. $this->assertEquals('Thu, 01 Jan 1970 00:00:01 +0000', $headers['Last-Modified']);
  214. $this->assertEquals('private, max-age=33, must-revalidate',
  215. $headers['Cache-Control']);
  216. }
  217. public function testThrottle() {
  218. $this->assertFalse($this->childResponse->isThrottled());
  219. $this->childResponse->throttle();
  220. $this->assertTrue($this->childResponse->isThrottled());
  221. }
  222. public function testGetThrottleMetadata() {
  223. $this->childResponse->throttle(['foo' => 'bar']);
  224. $this->assertSame(['foo' => 'bar'], $this->childResponse->getThrottleMetadata());
  225. }
  226. }