ResponseHeaderBagTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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(new Cookie('foo', 'bar'));
  92. $this->assertSetCookieHeader('foo=bar; path=/; httponly', $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(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
  125. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'foo.bar'));
  126. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'bar.foo'));
  127. $bag->setCookie(new Cookie('foo', 'bar'));
  128. $this->assertCount(4, $bag->getCookies());
  129. $this->assertEquals('foo=bar; path=/path/foo; domain=foo.bar; httponly', $bag->get('set-cookie'));
  130. $this->assertEquals([
  131. 'foo=bar; path=/path/foo; domain=foo.bar; httponly',
  132. 'foo=bar; path=/path/bar; domain=foo.bar; httponly',
  133. 'foo=bar; path=/path/bar; domain=bar.foo; httponly',
  134. 'foo=bar; path=/; httponly',
  135. ], $bag->get('set-cookie', null, false));
  136. $this->assertSetCookieHeader('foo=bar; path=/path/foo; domain=foo.bar; httponly', $bag);
  137. $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=foo.bar; httponly', $bag);
  138. $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=bar.foo; httponly', $bag);
  139. $this->assertSetCookieHeader('foo=bar; path=/; httponly', $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(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
  151. $bag->setCookie(new Cookie('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(new Cookie('foo', 'bar', 0));
  168. $bag->setCookie(new Cookie('bar', 'foo', 0));
  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([new Cookie('foo', 'bar', 0, '/', null, false, false, true)], $bag->getCookies());
  183. $bag->set('set-cookie', 'foo2=bar2', false);
  184. $this->assertEquals([
  185. new Cookie('foo', 'bar', 0, '/', null, false, false, true),
  186. new Cookie('foo2', 'bar2', 0, '/', null, false, false, true),
  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. /**
  200. * @expectedException \InvalidArgumentException
  201. */
  202. public function testMakeDispositionInvalidDisposition()
  203. {
  204. $headers = new ResponseHeaderBag();
  205. $headers->makeDisposition('invalid', 'foo.html');
  206. }
  207. /**
  208. * @dataProvider provideMakeDisposition
  209. */
  210. public function testMakeDisposition($disposition, $filename, $filenameFallback, $expected)
  211. {
  212. $headers = new ResponseHeaderBag();
  213. $this->assertEquals($expected, $headers->makeDisposition($disposition, $filename, $filenameFallback));
  214. }
  215. public function testToStringDoesntMessUpHeaders()
  216. {
  217. $headers = new ResponseHeaderBag();
  218. $headers->set('Location', 'http://www.symfony.com');
  219. $headers->set('Content-type', 'text/html');
  220. (string) $headers;
  221. $allHeaders = $headers->allPreserveCase();
  222. $this->assertEquals(['http://www.symfony.com'], $allHeaders['Location']);
  223. $this->assertEquals(['text/html'], $allHeaders['Content-type']);
  224. }
  225. public function provideMakeDisposition()
  226. {
  227. return [
  228. ['attachment', 'foo.html', 'foo.html', 'attachment; filename="foo.html"'],
  229. ['attachment', 'foo.html', '', 'attachment; filename="foo.html"'],
  230. ['attachment', 'foo bar.html', '', 'attachment; filename="foo bar.html"'],
  231. ['attachment', 'foo "bar".html', '', 'attachment; filename="foo \\"bar\\".html"'],
  232. ['attachment', 'foo%20bar.html', 'foo bar.html', 'attachment; filename="foo bar.html"; filename*=utf-8\'\'foo%2520bar.html'],
  233. ['attachment', 'föö.html', 'foo.html', 'attachment; filename="foo.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html'],
  234. ];
  235. }
  236. /**
  237. * @dataProvider provideMakeDispositionFail
  238. * @expectedException \InvalidArgumentException
  239. */
  240. public function testMakeDispositionFail($disposition, $filename)
  241. {
  242. $headers = new ResponseHeaderBag();
  243. $headers->makeDisposition($disposition, $filename);
  244. }
  245. public function provideMakeDispositionFail()
  246. {
  247. return [
  248. ['attachment', 'foo%20bar.html'],
  249. ['attachment', 'foo/bar.html'],
  250. ['attachment', '/foo.html'],
  251. ['attachment', 'foo\bar.html'],
  252. ['attachment', '\foo.html'],
  253. ['attachment', 'föö.html'],
  254. ];
  255. }
  256. public function testDateHeaderAddedOnCreation()
  257. {
  258. $now = time();
  259. $bag = new ResponseHeaderBag();
  260. $this->assertTrue($bag->has('Date'));
  261. $this->assertEquals($now, $bag->getDate('Date')->getTimestamp());
  262. }
  263. public function testDateHeaderCanBeSetOnCreation()
  264. {
  265. $someDate = 'Thu, 23 Mar 2017 09:15:12 GMT';
  266. $bag = new ResponseHeaderBag(['Date' => $someDate]);
  267. $this->assertEquals($someDate, $bag->get('Date'));
  268. }
  269. public function testDateHeaderWillBeRecreatedWhenRemoved()
  270. {
  271. $someDate = 'Thu, 23 Mar 2017 09:15:12 GMT';
  272. $bag = new ResponseHeaderBag(['Date' => $someDate]);
  273. $bag->remove('Date');
  274. // a (new) Date header is still present
  275. $this->assertTrue($bag->has('Date'));
  276. $this->assertNotEquals($someDate, $bag->get('Date'));
  277. }
  278. public function testDateHeaderWillBeRecreatedWhenHeadersAreReplaced()
  279. {
  280. $bag = new ResponseHeaderBag();
  281. $bag->replace([]);
  282. $this->assertTrue($bag->has('Date'));
  283. }
  284. private function assertSetCookieHeader($expected, ResponseHeaderBag $actual)
  285. {
  286. $this->assertRegExp('#^Set-Cookie:\s+'.preg_quote($expected, '#').'$#m', str_replace("\r\n", "\n", (string) $actual));
  287. }
  288. }