ResponseHeaderBagTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. /**
  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')),
  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')),
  41. ),
  42. array(
  43. array('P3P' => 'CP="CAO PSA OUR"'),
  44. array('P3P' => array('CP="CAO PSA OUR"'), 'Cache-Control' => array('no-cache')),
  45. ),
  46. array(
  47. array('WWW-Authenticate' => 'Basic realm="WallyWorld"'),
  48. array('WWW-Authenticate' => array('Basic realm="WallyWorld"'), 'Cache-Control' => array('no-cache')),
  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')),
  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')),
  57. ),
  58. );
  59. }
  60. public function testCacheControlHeader()
  61. {
  62. $bag = new ResponseHeaderBag(array());
  63. $this->assertEquals('no-cache', $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. $bag = new ResponseHeaderBag();
  96. $bag->set('Cache-Control', array('public', 'must-revalidate'));
  97. $this->assertCount(1, $bag->get('Cache-Control', null, false));
  98. $this->assertEquals('must-revalidate, public', $bag->get('Cache-Control'));
  99. $bag = new ResponseHeaderBag();
  100. $bag->set('Cache-Control', 'public');
  101. $bag->set('Cache-Control', 'must-revalidate', false);
  102. $this->assertCount(1, $bag->get('Cache-Control', null, false));
  103. $this->assertEquals('must-revalidate, public', $bag->get('Cache-Control'));
  104. }
  105. public function testToStringIncludesCookieHeaders()
  106. {
  107. $bag = new ResponseHeaderBag(array());
  108. $bag->setCookie(new Cookie('foo', 'bar'));
  109. $this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
  110. $bag->clearCookie('foo');
  111. $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/; httponly', $bag);
  112. }
  113. public function testClearCookieSecureNotHttpOnly()
  114. {
  115. $bag = new ResponseHeaderBag(array());
  116. $bag->clearCookie('foo', '/', null, true, false);
  117. $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/; secure', $bag);
  118. }
  119. public function testReplace()
  120. {
  121. $bag = new ResponseHeaderBag(array());
  122. $this->assertEquals('no-cache', $bag->get('Cache-Control'));
  123. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  124. $bag->replace(array('Cache-Control' => 'public'));
  125. $this->assertEquals('public', $bag->get('Cache-Control'));
  126. $this->assertTrue($bag->hasCacheControlDirective('public'));
  127. }
  128. public function testReplaceWithRemove()
  129. {
  130. $bag = new ResponseHeaderBag(array());
  131. $this->assertEquals('no-cache', $bag->get('Cache-Control'));
  132. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  133. $bag->remove('Cache-Control');
  134. $bag->replace(array());
  135. $this->assertEquals('no-cache', $bag->get('Cache-Control'));
  136. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  137. }
  138. public function testCookiesWithSameNames()
  139. {
  140. $bag = new ResponseHeaderBag();
  141. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
  142. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'foo.bar'));
  143. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'bar.foo'));
  144. $bag->setCookie(new Cookie('foo', 'bar'));
  145. $this->assertCount(4, $bag->getCookies());
  146. $this->assertSetCookieHeader('foo=bar; path=/path/foo; domain=foo.bar; httponly', $bag);
  147. $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=foo.bar; httponly', $bag);
  148. $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=bar.foo; httponly', $bag);
  149. $this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
  150. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  151. $this->assertArrayHasKey('foo', $cookies['foo.bar']['/path/foo']);
  152. $this->assertArrayHasKey('foo', $cookies['foo.bar']['/path/bar']);
  153. $this->assertArrayHasKey('foo', $cookies['bar.foo']['/path/bar']);
  154. $this->assertArrayHasKey('foo', $cookies['']['/']);
  155. }
  156. public function testRemoveCookie()
  157. {
  158. $bag = new ResponseHeaderBag();
  159. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
  160. $bag->setCookie(new Cookie('bar', 'foo', 0, '/path/bar', 'foo.bar'));
  161. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  162. $this->assertArrayHasKey('/path/foo', $cookies['foo.bar']);
  163. $bag->removeCookie('foo', '/path/foo', 'foo.bar');
  164. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  165. $this->assertArrayNotHasKey('/path/foo', $cookies['foo.bar']);
  166. $bag->removeCookie('bar', '/path/bar', 'foo.bar');
  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(new Cookie('foo', 'bar', 0));
  174. $bag->setCookie(new Cookie('bar', 'foo', 0));
  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. /**
  185. * @expectedException \InvalidArgumentException
  186. */
  187. public function testGetCookiesWithInvalidArgument()
  188. {
  189. $bag = new ResponseHeaderBag();
  190. $bag->getCookies('invalid_argument');
  191. }
  192. /**
  193. * @expectedException \InvalidArgumentException
  194. */
  195. public function testMakeDispositionInvalidDisposition()
  196. {
  197. $headers = new ResponseHeaderBag();
  198. $headers->makeDisposition('invalid', 'foo.html');
  199. }
  200. /**
  201. * @dataProvider provideMakeDisposition
  202. */
  203. public function testMakeDisposition($disposition, $filename, $filenameFallback, $expected)
  204. {
  205. $headers = new ResponseHeaderBag();
  206. $this->assertEquals($expected, $headers->makeDisposition($disposition, $filename, $filenameFallback));
  207. }
  208. public function testToStringDoesntMessUpHeaders()
  209. {
  210. $headers = new ResponseHeaderBag();
  211. $headers->set('Location', 'http://www.symfony.com');
  212. $headers->set('Content-type', 'text/html');
  213. (string) $headers;
  214. $allHeaders = $headers->allPreserveCase();
  215. $this->assertEquals(array('http://www.symfony.com'), $allHeaders['Location']);
  216. $this->assertEquals(array('text/html'), $allHeaders['Content-type']);
  217. }
  218. public function provideMakeDisposition()
  219. {
  220. return array(
  221. array('attachment', 'foo.html', 'foo.html', 'attachment; filename="foo.html"'),
  222. array('attachment', 'foo.html', '', 'attachment; filename="foo.html"'),
  223. array('attachment', 'foo bar.html', '', 'attachment; filename="foo bar.html"'),
  224. array('attachment', 'foo "bar".html', '', 'attachment; filename="foo \\"bar\\".html"'),
  225. array('attachment', 'foo%20bar.html', 'foo bar.html', 'attachment; filename="foo bar.html"; filename*=utf-8\'\'foo%2520bar.html'),
  226. array('attachment', 'föö.html', 'foo.html', 'attachment; filename="foo.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html'),
  227. );
  228. }
  229. /**
  230. * @dataProvider provideMakeDispositionFail
  231. * @expectedException \InvalidArgumentException
  232. */
  233. public function testMakeDispositionFail($disposition, $filename)
  234. {
  235. $headers = new ResponseHeaderBag();
  236. $headers->makeDisposition($disposition, $filename);
  237. }
  238. public function provideMakeDispositionFail()
  239. {
  240. return array(
  241. array('attachment', 'foo%20bar.html'),
  242. array('attachment', 'foo/bar.html'),
  243. array('attachment', '/foo.html'),
  244. array('attachment', 'foo\bar.html'),
  245. array('attachment', '\foo.html'),
  246. array('attachment', 'föö.html'),
  247. );
  248. }
  249. private function assertSetCookieHeader($expected, ResponseHeaderBag $actual)
  250. {
  251. $this->assertRegExp('#^Set-Cookie:\s+'.preg_quote($expected, '#').'$#m', str_replace("\r\n", "\n", (string) $actual));
  252. }
  253. }