ResponseHeaderBagTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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\ResponseHeaderBag;
  13. use Symfony\Component\HttpFoundation\Cookie;
  14. /**
  15. * @group time-sensitive
  16. */
  17. class ResponseHeaderBagTest extends TestCase
  18. {
  19. /**
  20. * @dataProvider provideAllPreserveCase
  21. */
  22. public function testAllPreserveCase($headers, $expected)
  23. {
  24. $bag = new ResponseHeaderBag($headers);
  25. $this->assertEquals($expected, $bag->allPreserveCase(), '->allPreserveCase() gets all input keys in original case');
  26. }
  27. public function provideAllPreserveCase()
  28. {
  29. return array(
  30. array(
  31. array('fOo' => 'BAR'),
  32. array('fOo' => array('BAR'), 'Cache-Control' => array('no-cache, private')),
  33. ),
  34. array(
  35. array('ETag' => 'xyzzy'),
  36. array('ETag' => array('xyzzy'), 'Cache-Control' => array('private, must-revalidate')),
  37. ),
  38. array(
  39. array('Content-MD5' => 'Q2hlY2sgSW50ZWdyaXR5IQ=='),
  40. array('Content-MD5' => array('Q2hlY2sgSW50ZWdyaXR5IQ=='), 'Cache-Control' => array('no-cache, private')),
  41. ),
  42. array(
  43. array('P3P' => 'CP="CAO PSA OUR"'),
  44. array('P3P' => array('CP="CAO PSA OUR"'), 'Cache-Control' => array('no-cache, private')),
  45. ),
  46. array(
  47. array('WWW-Authenticate' => 'Basic realm="WallyWorld"'),
  48. array('WWW-Authenticate' => array('Basic realm="WallyWorld"'), 'Cache-Control' => array('no-cache, private')),
  49. ),
  50. array(
  51. array('X-UA-Compatible' => 'IE=edge,chrome=1'),
  52. array('X-UA-Compatible' => array('IE=edge,chrome=1'), 'Cache-Control' => array('no-cache, private')),
  53. ),
  54. array(
  55. array('X-XSS-Protection' => '1; mode=block'),
  56. array('X-XSS-Protection' => array('1; mode=block'), 'Cache-Control' => array('no-cache, private')),
  57. ),
  58. );
  59. }
  60. public function testCacheControlHeader()
  61. {
  62. $bag = new ResponseHeaderBag(array());
  63. $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
  64. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  65. $bag = new ResponseHeaderBag(array('Cache-Control' => 'public'));
  66. $this->assertEquals('public', $bag->get('Cache-Control'));
  67. $this->assertTrue($bag->hasCacheControlDirective('public'));
  68. $bag = new ResponseHeaderBag(array('ETag' => 'abcde'));
  69. $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
  70. $this->assertTrue($bag->hasCacheControlDirective('private'));
  71. $this->assertTrue($bag->hasCacheControlDirective('must-revalidate'));
  72. $this->assertFalse($bag->hasCacheControlDirective('max-age'));
  73. $bag = new ResponseHeaderBag(array('Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT'));
  74. $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
  75. $bag = new ResponseHeaderBag(array(
  76. 'Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT',
  77. 'Cache-Control' => 'max-age=3600',
  78. ));
  79. $this->assertEquals('max-age=3600, private', $bag->get('Cache-Control'));
  80. $bag = new ResponseHeaderBag(array('Last-Modified' => 'abcde'));
  81. $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
  82. $bag = new ResponseHeaderBag(array('Etag' => 'abcde', 'Last-Modified' => 'abcde'));
  83. $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
  84. $bag = new ResponseHeaderBag(array('cache-control' => 'max-age=100'));
  85. $this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
  86. $bag = new ResponseHeaderBag(array('cache-control' => 's-maxage=100'));
  87. $this->assertEquals('s-maxage=100', $bag->get('Cache-Control'));
  88. $bag = new ResponseHeaderBag(array('cache-control' => 'private, max-age=100'));
  89. $this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
  90. $bag = new ResponseHeaderBag(array('cache-control' => 'public, max-age=100'));
  91. $this->assertEquals('max-age=100, public', $bag->get('Cache-Control'));
  92. $bag = new ResponseHeaderBag();
  93. $bag->set('Last-Modified', 'abcde');
  94. $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
  95. }
  96. public function testCacheControlClone()
  97. {
  98. $headers = array('foo' => 'bar');
  99. $bag1 = new ResponseHeaderBag($headers);
  100. $bag2 = new ResponseHeaderBag($bag1->allPreserveCase());
  101. $this->assertEquals($bag1->allPreserveCase(), $bag2->allPreserveCase());
  102. }
  103. public function testToStringIncludesCookieHeaders()
  104. {
  105. $bag = new ResponseHeaderBag(array());
  106. $bag->setCookie(new Cookie('foo', 'bar'));
  107. $this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
  108. $bag->clearCookie('foo');
  109. $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; max-age=-31536001; path=/; httponly', $bag);
  110. }
  111. public function testClearCookieSecureNotHttpOnly()
  112. {
  113. $bag = new ResponseHeaderBag(array());
  114. $bag->clearCookie('foo', '/', null, true, false);
  115. $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; max-age=-31536001; path=/; secure', $bag);
  116. }
  117. public function testReplace()
  118. {
  119. $bag = new ResponseHeaderBag(array());
  120. $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
  121. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  122. $bag->replace(array('Cache-Control' => 'public'));
  123. $this->assertEquals('public', $bag->get('Cache-Control'));
  124. $this->assertTrue($bag->hasCacheControlDirective('public'));
  125. }
  126. public function testReplaceWithRemove()
  127. {
  128. $bag = new ResponseHeaderBag(array());
  129. $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
  130. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  131. $bag->remove('Cache-Control');
  132. $bag->replace(array());
  133. $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
  134. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  135. }
  136. public function testCookiesWithSameNames()
  137. {
  138. $bag = new ResponseHeaderBag();
  139. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
  140. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'foo.bar'));
  141. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'bar.foo'));
  142. $bag->setCookie(new Cookie('foo', 'bar'));
  143. $this->assertCount(4, $bag->getCookies());
  144. $this->assertEquals('foo=bar; path=/path/foo; domain=foo.bar; httponly', $bag->get('set-cookie'));
  145. $this->assertEquals(array(
  146. 'foo=bar; path=/path/foo; domain=foo.bar; httponly',
  147. 'foo=bar; path=/path/bar; domain=foo.bar; httponly',
  148. 'foo=bar; path=/path/bar; domain=bar.foo; httponly',
  149. 'foo=bar; path=/; httponly',
  150. ), $bag->get('set-cookie', null, false));
  151. $this->assertSetCookieHeader('foo=bar; path=/path/foo; domain=foo.bar; httponly', $bag);
  152. $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=foo.bar; httponly', $bag);
  153. $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=bar.foo; httponly', $bag);
  154. $this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
  155. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  156. $this->assertTrue(isset($cookies['foo.bar']['/path/foo']['foo']));
  157. $this->assertTrue(isset($cookies['foo.bar']['/path/bar']['foo']));
  158. $this->assertTrue(isset($cookies['bar.foo']['/path/bar']['foo']));
  159. $this->assertTrue(isset($cookies['']['/']['foo']));
  160. }
  161. public function testRemoveCookie()
  162. {
  163. $bag = new ResponseHeaderBag();
  164. $this->assertFalse($bag->has('set-cookie'));
  165. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
  166. $bag->setCookie(new Cookie('bar', 'foo', 0, '/path/bar', 'foo.bar'));
  167. $this->assertTrue($bag->has('set-cookie'));
  168. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  169. $this->assertTrue(isset($cookies['foo.bar']['/path/foo']));
  170. $bag->removeCookie('foo', '/path/foo', 'foo.bar');
  171. $this->assertTrue($bag->has('set-cookie'));
  172. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  173. $this->assertFalse(isset($cookies['foo.bar']['/path/foo']));
  174. $bag->removeCookie('bar', '/path/bar', 'foo.bar');
  175. $this->assertFalse($bag->has('set-cookie'));
  176. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  177. $this->assertFalse(isset($cookies['foo.bar']));
  178. }
  179. public function testRemoveCookieWithNullRemove()
  180. {
  181. $bag = new ResponseHeaderBag();
  182. $bag->setCookie(new Cookie('foo', 'bar', 0));
  183. $bag->setCookie(new Cookie('bar', 'foo', 0));
  184. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  185. $this->assertTrue(isset($cookies['']['/']));
  186. $bag->removeCookie('foo', null);
  187. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  188. $this->assertFalse(isset($cookies['']['/']['foo']));
  189. $bag->removeCookie('bar', null);
  190. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  191. $this->assertFalse(isset($cookies['']['/']['bar']));
  192. }
  193. public function testSetCookieHeader()
  194. {
  195. $bag = new ResponseHeaderBag();
  196. $bag->set('set-cookie', 'foo=bar');
  197. $this->assertEquals(array(new Cookie('foo', 'bar', 0, '/', null, false, true, true)), $bag->getCookies());
  198. $bag->set('set-cookie', 'foo2=bar2', false);
  199. $this->assertEquals(array(
  200. new Cookie('foo', 'bar', 0, '/', null, false, true, true),
  201. new Cookie('foo2', 'bar2', 0, '/', null, false, true, true),
  202. ), $bag->getCookies());
  203. $bag->remove('set-cookie');
  204. $this->assertEquals(array(), $bag->getCookies());
  205. }
  206. /**
  207. * @expectedException \InvalidArgumentException
  208. */
  209. public function testGetCookiesWithInvalidArgument()
  210. {
  211. $bag = new ResponseHeaderBag();
  212. $bag->getCookies('invalid_argument');
  213. }
  214. /**
  215. * @expectedException \InvalidArgumentException
  216. */
  217. public function testMakeDispositionInvalidDisposition()
  218. {
  219. $headers = new ResponseHeaderBag();
  220. $headers->makeDisposition('invalid', 'foo.html');
  221. }
  222. /**
  223. * @dataProvider provideMakeDisposition
  224. */
  225. public function testMakeDisposition($disposition, $filename, $filenameFallback, $expected)
  226. {
  227. $headers = new ResponseHeaderBag();
  228. $this->assertEquals($expected, $headers->makeDisposition($disposition, $filename, $filenameFallback));
  229. }
  230. public function testToStringDoesntMessUpHeaders()
  231. {
  232. $headers = new ResponseHeaderBag();
  233. $headers->set('Location', 'http://www.symfony.com');
  234. $headers->set('Content-type', 'text/html');
  235. (string) $headers;
  236. $allHeaders = $headers->allPreserveCase();
  237. $this->assertEquals(array('http://www.symfony.com'), $allHeaders['Location']);
  238. $this->assertEquals(array('text/html'), $allHeaders['Content-type']);
  239. }
  240. public function provideMakeDisposition()
  241. {
  242. return array(
  243. array('attachment', 'foo.html', 'foo.html', 'attachment; filename="foo.html"'),
  244. array('attachment', 'foo.html', '', 'attachment; filename="foo.html"'),
  245. array('attachment', 'foo bar.html', '', 'attachment; filename="foo bar.html"'),
  246. array('attachment', 'foo "bar".html', '', 'attachment; filename="foo \\"bar\\".html"'),
  247. array('attachment', 'foo%20bar.html', 'foo bar.html', 'attachment; filename="foo bar.html"; filename*=utf-8\'\'foo%2520bar.html'),
  248. array('attachment', 'föö.html', 'foo.html', 'attachment; filename="foo.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html'),
  249. );
  250. }
  251. /**
  252. * @dataProvider provideMakeDispositionFail
  253. * @expectedException \InvalidArgumentException
  254. */
  255. public function testMakeDispositionFail($disposition, $filename)
  256. {
  257. $headers = new ResponseHeaderBag();
  258. $headers->makeDisposition($disposition, $filename);
  259. }
  260. public function provideMakeDispositionFail()
  261. {
  262. return array(
  263. array('attachment', 'foo%20bar.html'),
  264. array('attachment', 'foo/bar.html'),
  265. array('attachment', '/foo.html'),
  266. array('attachment', 'foo\bar.html'),
  267. array('attachment', '\foo.html'),
  268. array('attachment', 'föö.html'),
  269. );
  270. }
  271. private function assertSetCookieHeader($expected, ResponseHeaderBag $actual)
  272. {
  273. $this->assertRegExp('#^Set-Cookie:\s+'.preg_quote($expected, '#').'$#m', str_replace("\r\n", "\n", (string) $actual));
  274. }
  275. }