ResponseHeaderBagTest.php 11 KB

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