ResponseHeaderBagTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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('private, must-revalidate', $bag->get('Cache-Control'));
  46. $this->assertTrue($bag->hasCacheControlDirective('private'));
  47. $this->assertTrue($bag->hasCacheControlDirective('must-revalidate'));
  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->get('Cache-Control', null, false));
  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->get('Cache-Control', null, false));
  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 testReplace()
  103. {
  104. $bag = new ResponseHeaderBag([]);
  105. $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
  106. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  107. $bag->replace(['Cache-Control' => 'public']);
  108. $this->assertEquals('public', $bag->get('Cache-Control'));
  109. $this->assertTrue($bag->hasCacheControlDirective('public'));
  110. }
  111. public function testReplaceWithRemove()
  112. {
  113. $bag = new ResponseHeaderBag([]);
  114. $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
  115. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  116. $bag->remove('Cache-Control');
  117. $bag->replace([]);
  118. $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
  119. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  120. }
  121. public function testCookiesWithSameNames()
  122. {
  123. $bag = new ResponseHeaderBag();
  124. $bag->setCookie(Cookie::create('foo', 'bar', 0, '/path/foo', 'foo.bar'));
  125. $bag->setCookie(Cookie::create('foo', 'bar', 0, '/path/bar', 'foo.bar'));
  126. $bag->setCookie(Cookie::create('foo', 'bar', 0, '/path/bar', 'bar.foo'));
  127. $bag->setCookie(Cookie::create('foo', 'bar'));
  128. $this->assertCount(4, $bag->getCookies());
  129. $this->assertEquals('foo=bar; path=/path/foo; domain=foo.bar; httponly; samesite=lax', $bag->get('set-cookie'));
  130. $this->assertEquals([
  131. 'foo=bar; path=/path/foo; domain=foo.bar; httponly; samesite=lax',
  132. 'foo=bar; path=/path/bar; domain=foo.bar; httponly; samesite=lax',
  133. 'foo=bar; path=/path/bar; domain=bar.foo; httponly; samesite=lax',
  134. 'foo=bar; path=/; httponly; samesite=lax',
  135. ], $bag->get('set-cookie', null, false));
  136. $this->assertSetCookieHeader('foo=bar; path=/path/foo; domain=foo.bar; httponly; samesite=lax', $bag);
  137. $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=foo.bar; httponly; samesite=lax', $bag);
  138. $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=bar.foo; httponly; samesite=lax', $bag);
  139. $this->assertSetCookieHeader('foo=bar; path=/; httponly; samesite=lax', $bag);
  140. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  141. $this->assertArrayHasKey('foo', $cookies['foo.bar']['/path/foo']);
  142. $this->assertArrayHasKey('foo', $cookies['foo.bar']['/path/bar']);
  143. $this->assertArrayHasKey('foo', $cookies['bar.foo']['/path/bar']);
  144. $this->assertArrayHasKey('foo', $cookies['']['/']);
  145. }
  146. public function testRemoveCookie()
  147. {
  148. $bag = new ResponseHeaderBag();
  149. $this->assertFalse($bag->has('set-cookie'));
  150. $bag->setCookie(Cookie::create('foo', 'bar', 0, '/path/foo', 'foo.bar'));
  151. $bag->setCookie(Cookie::create('bar', 'foo', 0, '/path/bar', 'foo.bar'));
  152. $this->assertTrue($bag->has('set-cookie'));
  153. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  154. $this->assertArrayHasKey('/path/foo', $cookies['foo.bar']);
  155. $bag->removeCookie('foo', '/path/foo', 'foo.bar');
  156. $this->assertTrue($bag->has('set-cookie'));
  157. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  158. $this->assertArrayNotHasKey('/path/foo', $cookies['foo.bar']);
  159. $bag->removeCookie('bar', '/path/bar', 'foo.bar');
  160. $this->assertFalse($bag->has('set-cookie'));
  161. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  162. $this->assertArrayNotHasKey('foo.bar', $cookies);
  163. }
  164. public function testRemoveCookieWithNullRemove()
  165. {
  166. $bag = new ResponseHeaderBag();
  167. $bag->setCookie(Cookie::create('foo', 'bar'));
  168. $bag->setCookie(Cookie::create('bar', 'foo'));
  169. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  170. $this->assertArrayHasKey('/', $cookies['']);
  171. $bag->removeCookie('foo', null);
  172. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  173. $this->assertArrayNotHasKey('foo', $cookies['']['/']);
  174. $bag->removeCookie('bar', null);
  175. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  176. $this->assertFalse(isset($cookies['']['/']['bar']));
  177. }
  178. public function testSetCookieHeader()
  179. {
  180. $bag = new ResponseHeaderBag();
  181. $bag->set('set-cookie', 'foo=bar');
  182. $this->assertEquals([Cookie::create('foo', 'bar', 0, '/', null, false, false, true, null)], $bag->getCookies());
  183. $bag->set('set-cookie', 'foo2=bar2', false);
  184. $this->assertEquals([
  185. Cookie::create('foo', 'bar', 0, '/', null, false, false, true, null),
  186. Cookie::create('foo2', 'bar2', 0, '/', null, false, false, true, null),
  187. ], $bag->getCookies());
  188. $bag->remove('set-cookie');
  189. $this->assertEquals([], $bag->getCookies());
  190. }
  191. /**
  192. * @expectedException \InvalidArgumentException
  193. */
  194. public function testGetCookiesWithInvalidArgument()
  195. {
  196. $bag = new ResponseHeaderBag();
  197. $bag->getCookies('invalid_argument');
  198. }
  199. public function testToStringDoesntMessUpHeaders()
  200. {
  201. $headers = new ResponseHeaderBag();
  202. $headers->set('Location', 'http://www.symfony.com');
  203. $headers->set('Content-type', 'text/html');
  204. (string) $headers;
  205. $allHeaders = $headers->allPreserveCase();
  206. $this->assertEquals(['http://www.symfony.com'], $allHeaders['Location']);
  207. $this->assertEquals(['text/html'], $allHeaders['Content-type']);
  208. }
  209. public function testDateHeaderAddedOnCreation()
  210. {
  211. $now = time();
  212. $bag = new ResponseHeaderBag();
  213. $this->assertTrue($bag->has('Date'));
  214. $this->assertEquals($now, $bag->getDate('Date')->getTimestamp());
  215. }
  216. public function testDateHeaderCanBeSetOnCreation()
  217. {
  218. $someDate = 'Thu, 23 Mar 2017 09:15:12 GMT';
  219. $bag = new ResponseHeaderBag(['Date' => $someDate]);
  220. $this->assertEquals($someDate, $bag->get('Date'));
  221. }
  222. public function testDateHeaderWillBeRecreatedWhenRemoved()
  223. {
  224. $someDate = 'Thu, 23 Mar 2017 09:15:12 GMT';
  225. $bag = new ResponseHeaderBag(['Date' => $someDate]);
  226. $bag->remove('Date');
  227. // a (new) Date header is still present
  228. $this->assertTrue($bag->has('Date'));
  229. $this->assertNotEquals($someDate, $bag->get('Date'));
  230. }
  231. public function testDateHeaderWillBeRecreatedWhenHeadersAreReplaced()
  232. {
  233. $bag = new ResponseHeaderBag();
  234. $bag->replace([]);
  235. $this->assertTrue($bag->has('Date'));
  236. }
  237. private function assertSetCookieHeader($expected, ResponseHeaderBag $actual)
  238. {
  239. $this->assertRegExp('#^Set-Cookie:\s+'.preg_quote($expected, '#').'$#m', str_replace("\r\n", "\n", (string) $actual));
  240. }
  241. }