ResponseHeaderBagTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Cookie;
  13. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  14. /**
  15. * @group time-sensitive
  16. */
  17. class ResponseHeaderBagTest extends TestCase
  18. {
  19. public function testAllPreserveCase()
  20. {
  21. $headers = [
  22. 'fOo' => 'BAR',
  23. 'ETag' => 'xyzzy',
  24. 'Content-MD5' => 'Q2hlY2sgSW50ZWdyaXR5IQ==',
  25. 'P3P' => 'CP="CAO PSA OUR"',
  26. 'WWW-Authenticate' => 'Basic realm="WallyWorld"',
  27. 'X-UA-Compatible' => 'IE=edge,chrome=1',
  28. 'X-XSS-Protection' => '1; mode=block',
  29. ];
  30. $bag = new ResponseHeaderBag($headers);
  31. $allPreservedCase = $bag->allPreserveCase();
  32. foreach (array_keys($headers) as $headerName) {
  33. $this->assertArrayHasKey($headerName, $allPreservedCase, '->allPreserveCase() gets all input keys in original case');
  34. }
  35. }
  36. public function testCacheControlHeader()
  37. {
  38. $bag = new ResponseHeaderBag([]);
  39. $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
  40. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  41. $bag = new ResponseHeaderBag(['Cache-Control' => 'public']);
  42. $this->assertEquals('public', $bag->get('Cache-Control'));
  43. $this->assertTrue($bag->hasCacheControlDirective('public'));
  44. $bag = new ResponseHeaderBag(['ETag' => 'abcde']);
  45. $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
  46. $this->assertTrue($bag->hasCacheControlDirective('private'));
  47. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  48. $this->assertFalse($bag->hasCacheControlDirective('max-age'));
  49. $bag = new ResponseHeaderBag(['Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT']);
  50. $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
  51. $bag = new ResponseHeaderBag([
  52. 'Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT',
  53. 'Cache-Control' => 'max-age=3600',
  54. ]);
  55. $this->assertEquals('max-age=3600, private', $bag->get('Cache-Control'));
  56. $bag = new ResponseHeaderBag(['Last-Modified' => 'abcde']);
  57. $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
  58. $bag = new ResponseHeaderBag(['Etag' => 'abcde', 'Last-Modified' => 'abcde']);
  59. $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
  60. $bag = new ResponseHeaderBag(['cache-control' => 'max-age=100']);
  61. $this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
  62. $bag = new ResponseHeaderBag(['cache-control' => 's-maxage=100']);
  63. $this->assertEquals('s-maxage=100', $bag->get('Cache-Control'));
  64. $bag = new ResponseHeaderBag(['cache-control' => 'private, max-age=100']);
  65. $this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
  66. $bag = new ResponseHeaderBag(['cache-control' => 'public, max-age=100']);
  67. $this->assertEquals('max-age=100, public', $bag->get('Cache-Control'));
  68. $bag = new ResponseHeaderBag();
  69. $bag->set('Last-Modified', 'abcde');
  70. $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
  71. $bag = new ResponseHeaderBag();
  72. $bag->set('Cache-Control', ['public', 'must-revalidate']);
  73. $this->assertCount(1, $bag->all('Cache-Control'));
  74. $this->assertEquals('must-revalidate, public', $bag->get('Cache-Control'));
  75. $bag = new ResponseHeaderBag();
  76. $bag->set('Cache-Control', 'public');
  77. $bag->set('Cache-Control', 'must-revalidate', false);
  78. $this->assertCount(1, $bag->all('Cache-Control'));
  79. $this->assertEquals('must-revalidate, public', $bag->get('Cache-Control'));
  80. }
  81. public function testCacheControlClone()
  82. {
  83. $headers = ['foo' => 'bar'];
  84. $bag1 = new ResponseHeaderBag($headers);
  85. $bag2 = new ResponseHeaderBag($bag1->allPreserveCase());
  86. $this->assertEquals($bag1->allPreserveCase(), $bag2->allPreserveCase());
  87. }
  88. public function testToStringIncludesCookieHeaders()
  89. {
  90. $bag = new ResponseHeaderBag([]);
  91. $bag->setCookie(Cookie::create('foo', 'bar'));
  92. $this->assertSetCookieHeader('foo=bar; path=/; httponly; samesite=lax', $bag);
  93. $bag->clearCookie('foo');
  94. $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; Max-Age=0; path=/; httponly', $bag);
  95. }
  96. public function testClearCookieSecureNotHttpOnly()
  97. {
  98. $bag = new ResponseHeaderBag([]);
  99. $bag->clearCookie('foo', '/', null, true, false);
  100. $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; Max-Age=0; path=/; secure', $bag);
  101. }
  102. public function testClearCookieSamesite()
  103. {
  104. $bag = new ResponseHeaderBag([]);
  105. $bag->clearCookie('foo', '/', null, true, false, 'none');
  106. $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; Max-Age=0; path=/; secure; samesite=none', $bag);
  107. }
  108. public function testReplace()
  109. {
  110. $bag = new ResponseHeaderBag([]);
  111. $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
  112. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  113. $bag->replace(['Cache-Control' => 'public']);
  114. $this->assertEquals('public', $bag->get('Cache-Control'));
  115. $this->assertTrue($bag->hasCacheControlDirective('public'));
  116. }
  117. public function testReplaceWithRemove()
  118. {
  119. $bag = new ResponseHeaderBag([]);
  120. $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
  121. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  122. $bag->remove('Cache-Control');
  123. $bag->replace([]);
  124. $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
  125. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  126. }
  127. public function testCookiesWithSameNames()
  128. {
  129. $bag = new ResponseHeaderBag();
  130. $bag->setCookie(Cookie::create('foo', 'bar', 0, '/path/foo', 'foo.bar'));
  131. $bag->setCookie(Cookie::create('foo', 'bar', 0, '/path/bar', 'foo.bar'));
  132. $bag->setCookie(Cookie::create('foo', 'bar', 0, '/path/bar', 'bar.foo'));
  133. $bag->setCookie(Cookie::create('foo', 'bar'));
  134. $this->assertCount(4, $bag->getCookies());
  135. $this->assertEquals('foo=bar; path=/path/foo; domain=foo.bar; httponly; samesite=lax', $bag->get('set-cookie'));
  136. $this->assertEquals([
  137. 'foo=bar; path=/path/foo; domain=foo.bar; httponly; samesite=lax',
  138. 'foo=bar; path=/path/bar; domain=foo.bar; httponly; samesite=lax',
  139. 'foo=bar; path=/path/bar; domain=bar.foo; httponly; samesite=lax',
  140. 'foo=bar; path=/; httponly; samesite=lax',
  141. ], $bag->all('set-cookie'));
  142. $this->assertSetCookieHeader('foo=bar; path=/path/foo; domain=foo.bar; httponly; samesite=lax', $bag);
  143. $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=foo.bar; httponly; samesite=lax', $bag);
  144. $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=bar.foo; httponly; samesite=lax', $bag);
  145. $this->assertSetCookieHeader('foo=bar; path=/; httponly; samesite=lax', $bag);
  146. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  147. $this->assertArrayHasKey('foo', $cookies['foo.bar']['/path/foo']);
  148. $this->assertArrayHasKey('foo', $cookies['foo.bar']['/path/bar']);
  149. $this->assertArrayHasKey('foo', $cookies['bar.foo']['/path/bar']);
  150. $this->assertArrayHasKey('foo', $cookies['']['/']);
  151. }
  152. public function testRemoveCookie()
  153. {
  154. $bag = new ResponseHeaderBag();
  155. $this->assertFalse($bag->has('set-cookie'));
  156. $bag->setCookie(Cookie::create('foo', 'bar', 0, '/path/foo', 'foo.bar'));
  157. $bag->setCookie(Cookie::create('bar', 'foo', 0, '/path/bar', 'foo.bar'));
  158. $this->assertTrue($bag->has('set-cookie'));
  159. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  160. $this->assertArrayHasKey('/path/foo', $cookies['foo.bar']);
  161. $bag->removeCookie('foo', '/path/foo', 'foo.bar');
  162. $this->assertTrue($bag->has('set-cookie'));
  163. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  164. $this->assertArrayNotHasKey('/path/foo', $cookies['foo.bar']);
  165. $bag->removeCookie('bar', '/path/bar', 'foo.bar');
  166. $this->assertFalse($bag->has('set-cookie'));
  167. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  168. $this->assertArrayNotHasKey('foo.bar', $cookies);
  169. }
  170. public function testRemoveCookieWithNullRemove()
  171. {
  172. $bag = new ResponseHeaderBag();
  173. $bag->setCookie(Cookie::create('foo', 'bar'));
  174. $bag->setCookie(Cookie::create('bar', 'foo'));
  175. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  176. $this->assertArrayHasKey('/', $cookies['']);
  177. $bag->removeCookie('foo', null);
  178. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  179. $this->assertArrayNotHasKey('foo', $cookies['']['/']);
  180. $bag->removeCookie('bar', null);
  181. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  182. $this->assertFalse(isset($cookies['']['/']['bar']));
  183. }
  184. public function testSetCookieHeader()
  185. {
  186. $bag = new ResponseHeaderBag();
  187. $bag->set('set-cookie', 'foo=bar');
  188. $this->assertEquals([Cookie::create('foo', 'bar', 0, '/', null, false, false, true, null)], $bag->getCookies());
  189. $bag->set('set-cookie', 'foo2=bar2', false);
  190. $this->assertEquals([
  191. Cookie::create('foo', 'bar', 0, '/', null, false, false, true, null),
  192. Cookie::create('foo2', 'bar2', 0, '/', null, false, false, true, null),
  193. ], $bag->getCookies());
  194. $bag->remove('set-cookie');
  195. $this->assertEquals([], $bag->getCookies());
  196. }
  197. public function testGetCookiesWithInvalidArgument()
  198. {
  199. $this->expectException(\InvalidArgumentException::class);
  200. $bag = new ResponseHeaderBag();
  201. $bag->getCookies('invalid_argument');
  202. }
  203. public function testToStringDoesntMessUpHeaders()
  204. {
  205. $headers = new ResponseHeaderBag();
  206. $headers->set('Location', 'http://www.symfony.com');
  207. $headers->set('Content-type', 'text/html');
  208. (string) $headers;
  209. $allHeaders = $headers->allPreserveCase();
  210. $this->assertEquals(['http://www.symfony.com'], $allHeaders['Location']);
  211. $this->assertEquals(['text/html'], $allHeaders['Content-type']);
  212. }
  213. public function testDateHeaderAddedOnCreation()
  214. {
  215. $now = time();
  216. $bag = new ResponseHeaderBag();
  217. $this->assertTrue($bag->has('Date'));
  218. $this->assertEquals($now, $bag->getDate('Date')->getTimestamp());
  219. }
  220. public function testDateHeaderCanBeSetOnCreation()
  221. {
  222. $someDate = 'Thu, 23 Mar 2017 09:15:12 GMT';
  223. $bag = new ResponseHeaderBag(['Date' => $someDate]);
  224. $this->assertEquals($someDate, $bag->get('Date'));
  225. }
  226. public function testDateHeaderWillBeRecreatedWhenRemoved()
  227. {
  228. $someDate = 'Thu, 23 Mar 2017 09:15:12 GMT';
  229. $bag = new ResponseHeaderBag(['Date' => $someDate]);
  230. $bag->remove('Date');
  231. // a (new) Date header is still present
  232. $this->assertTrue($bag->has('Date'));
  233. $this->assertNotEquals($someDate, $bag->get('Date'));
  234. }
  235. public function testDateHeaderWillBeRecreatedWhenHeadersAreReplaced()
  236. {
  237. $bag = new ResponseHeaderBag();
  238. $bag->replace([]);
  239. $this->assertTrue($bag->has('Date'));
  240. }
  241. private function assertSetCookieHeader(string $expected, ResponseHeaderBag $actual)
  242. {
  243. $this->assertMatchesRegularExpression('#^Set-Cookie:\s+'.preg_quote($expected, '#').'$#m', str_replace("\r\n", "\n", (string) $actual));
  244. }
  245. }