Nenhuma Descrição

ResponseHeaderBagTest.php 12KB

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