ResponseHeaderBagTest.php 11 KB

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