菜谱项目

ResponseHeaderBagTest.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. $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 testCacheControlClone()
  106. {
  107. $headers = array('foo' => 'bar');
  108. $bag1 = new ResponseHeaderBag($headers);
  109. $bag2 = new ResponseHeaderBag($bag1->allPreserveCase());
  110. $this->assertEquals($bag1->allPreserveCase(), $bag2->allPreserveCase());
  111. }
  112. public function testToStringIncludesCookieHeaders()
  113. {
  114. $bag = new ResponseHeaderBag(array());
  115. $bag->setCookie(new Cookie('foo', 'bar'));
  116. $this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
  117. $bag->clearCookie('foo');
  118. $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; max-age=-31536001; path=/; httponly', $bag);
  119. }
  120. public function testClearCookieSecureNotHttpOnly()
  121. {
  122. $bag = new ResponseHeaderBag(array());
  123. $bag->clearCookie('foo', '/', null, true, false);
  124. $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; max-age=-31536001; path=/; secure', $bag);
  125. }
  126. public function testReplace()
  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->replace(array('Cache-Control' => 'public'));
  132. $this->assertEquals('public', $bag->get('Cache-Control'));
  133. $this->assertTrue($bag->hasCacheControlDirective('public'));
  134. }
  135. public function testReplaceWithRemove()
  136. {
  137. $bag = new ResponseHeaderBag(array());
  138. $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
  139. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  140. $bag->remove('Cache-Control');
  141. $bag->replace(array());
  142. $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
  143. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  144. }
  145. public function testCookiesWithSameNames()
  146. {
  147. $bag = new ResponseHeaderBag();
  148. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
  149. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'foo.bar'));
  150. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'bar.foo'));
  151. $bag->setCookie(new Cookie('foo', 'bar'));
  152. $this->assertCount(4, $bag->getCookies());
  153. $this->assertEquals('foo=bar; path=/path/foo; domain=foo.bar; httponly', $bag->get('set-cookie'));
  154. $this->assertEquals(array(
  155. 'foo=bar; path=/path/foo; domain=foo.bar; httponly',
  156. 'foo=bar; path=/path/bar; domain=foo.bar; httponly',
  157. 'foo=bar; path=/path/bar; domain=bar.foo; httponly',
  158. 'foo=bar; path=/; httponly',
  159. ), $bag->get('set-cookie', null, false));
  160. $this->assertSetCookieHeader('foo=bar; path=/path/foo; domain=foo.bar; httponly', $bag);
  161. $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=foo.bar; httponly', $bag);
  162. $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=bar.foo; httponly', $bag);
  163. $this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
  164. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  165. $this->assertTrue(isset($cookies['foo.bar']['/path/foo']['foo']));
  166. $this->assertTrue(isset($cookies['foo.bar']['/path/bar']['foo']));
  167. $this->assertTrue(isset($cookies['bar.foo']['/path/bar']['foo']));
  168. $this->assertTrue(isset($cookies['']['/']['foo']));
  169. }
  170. public function testRemoveCookie()
  171. {
  172. $bag = new ResponseHeaderBag();
  173. $this->assertFalse($bag->has('set-cookie'));
  174. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
  175. $bag->setCookie(new Cookie('bar', 'foo', 0, '/path/bar', 'foo.bar'));
  176. $this->assertTrue($bag->has('set-cookie'));
  177. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  178. $this->assertTrue(isset($cookies['foo.bar']['/path/foo']));
  179. $bag->removeCookie('foo', '/path/foo', 'foo.bar');
  180. $this->assertTrue($bag->has('set-cookie'));
  181. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  182. $this->assertFalse(isset($cookies['foo.bar']['/path/foo']));
  183. $bag->removeCookie('bar', '/path/bar', 'foo.bar');
  184. $this->assertFalse($bag->has('set-cookie'));
  185. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  186. $this->assertFalse(isset($cookies['foo.bar']));
  187. }
  188. public function testRemoveCookieWithNullRemove()
  189. {
  190. $bag = new ResponseHeaderBag();
  191. $bag->setCookie(new Cookie('foo', 'bar', 0));
  192. $bag->setCookie(new Cookie('bar', 'foo', 0));
  193. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  194. $this->assertTrue(isset($cookies['']['/']));
  195. $bag->removeCookie('foo', null);
  196. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  197. $this->assertFalse(isset($cookies['']['/']['foo']));
  198. $bag->removeCookie('bar', null);
  199. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  200. $this->assertFalse(isset($cookies['']['/']['bar']));
  201. }
  202. public function testSetCookieHeader()
  203. {
  204. $bag = new ResponseHeaderBag();
  205. $bag->set('set-cookie', 'foo=bar');
  206. $this->assertEquals(array(new Cookie('foo', 'bar', 0, '/', null, false, false, true)), $bag->getCookies());
  207. $bag->set('set-cookie', 'foo2=bar2', false);
  208. $this->assertEquals(array(
  209. new Cookie('foo', 'bar', 0, '/', null, false, false, true),
  210. new Cookie('foo2', 'bar2', 0, '/', null, false, false, true),
  211. ), $bag->getCookies());
  212. $bag->remove('set-cookie');
  213. $this->assertEquals(array(), $bag->getCookies());
  214. }
  215. /**
  216. * @expectedException \InvalidArgumentException
  217. */
  218. public function testGetCookiesWithInvalidArgument()
  219. {
  220. $bag = new ResponseHeaderBag();
  221. $bag->getCookies('invalid_argument');
  222. }
  223. /**
  224. * @expectedException \InvalidArgumentException
  225. */
  226. public function testMakeDispositionInvalidDisposition()
  227. {
  228. $headers = new ResponseHeaderBag();
  229. $headers->makeDisposition('invalid', 'foo.html');
  230. }
  231. /**
  232. * @dataProvider provideMakeDisposition
  233. */
  234. public function testMakeDisposition($disposition, $filename, $filenameFallback, $expected)
  235. {
  236. $headers = new ResponseHeaderBag();
  237. $this->assertEquals($expected, $headers->makeDisposition($disposition, $filename, $filenameFallback));
  238. }
  239. public function testToStringDoesntMessUpHeaders()
  240. {
  241. $headers = new ResponseHeaderBag();
  242. $headers->set('Location', 'http://www.symfony.com');
  243. $headers->set('Content-type', 'text/html');
  244. (string) $headers;
  245. $allHeaders = $headers->allPreserveCase();
  246. $this->assertEquals(array('http://www.symfony.com'), $allHeaders['Location']);
  247. $this->assertEquals(array('text/html'), $allHeaders['Content-type']);
  248. }
  249. public function provideMakeDisposition()
  250. {
  251. return array(
  252. array('attachment', 'foo.html', 'foo.html', 'attachment; filename="foo.html"'),
  253. array('attachment', 'foo.html', '', 'attachment; filename="foo.html"'),
  254. array('attachment', 'foo bar.html', '', 'attachment; filename="foo bar.html"'),
  255. array('attachment', 'foo "bar".html', '', 'attachment; filename="foo \\"bar\\".html"'),
  256. array('attachment', 'foo%20bar.html', 'foo bar.html', 'attachment; filename="foo bar.html"; filename*=utf-8\'\'foo%2520bar.html'),
  257. array('attachment', 'föö.html', 'foo.html', 'attachment; filename="foo.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html'),
  258. );
  259. }
  260. /**
  261. * @dataProvider provideMakeDispositionFail
  262. * @expectedException \InvalidArgumentException
  263. */
  264. public function testMakeDispositionFail($disposition, $filename)
  265. {
  266. $headers = new ResponseHeaderBag();
  267. $headers->makeDisposition($disposition, $filename);
  268. }
  269. public function provideMakeDispositionFail()
  270. {
  271. return array(
  272. array('attachment', 'foo%20bar.html'),
  273. array('attachment', 'foo/bar.html'),
  274. array('attachment', '/foo.html'),
  275. array('attachment', 'foo\bar.html'),
  276. array('attachment', '\foo.html'),
  277. array('attachment', 'föö.html'),
  278. );
  279. }
  280. private function assertSetCookieHeader($expected, ResponseHeaderBag $actual)
  281. {
  282. $this->assertRegExp('#^Set-Cookie:\s+'.preg_quote($expected, '#').'$#m', str_replace("\r\n", "\n", (string) $actual));
  283. }
  284. }