Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

368 lignes
14 KiB

  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. public function testAllPreserveCase()
  20. {
  21. $headers = [
  22. 'fOo' => 'BAR',
  23. 'ETag' => 'xyzzy',
  24. 'Content-MD5' => 'Q2hlY2sgSW50ZWdyaXR5IQ==',
  25. 'P3P' => 'CP="CAO PSA OUR"',
  26. 'WWW-Authenticate' => 'Basic realm="WallyWorld"',
  27. 'X-UA-Compatible' => 'IE=edge,chrome=1',
  28. 'X-XSS-Protection' => '1; mode=block',
  29. ];
  30. $bag = new ResponseHeaderBag($headers);
  31. $allPreservedCase = $bag->allPreserveCase();
  32. foreach (array_keys($headers) as $headerName) {
  33. $this->assertArrayHasKey($headerName, $allPreservedCase, '->allPreserveCase() gets all input keys in original case');
  34. }
  35. }
  36. public function testCacheControlHeader()
  37. {
  38. $bag = new ResponseHeaderBag([]);
  39. $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
  40. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  41. $bag = new ResponseHeaderBag(['Cache-Control' => 'public']);
  42. $this->assertEquals('public', $bag->get('Cache-Control'));
  43. $this->assertTrue($bag->hasCacheControlDirective('public'));
  44. $bag = new ResponseHeaderBag(['ETag' => 'abcde']);
  45. $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
  46. $this->assertTrue($bag->hasCacheControlDirective('private'));
  47. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  48. $this->assertFalse($bag->hasCacheControlDirective('max-age'));
  49. $bag = new ResponseHeaderBag(['Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT']);
  50. $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
  51. $bag = new ResponseHeaderBag([
  52. 'Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT',
  53. 'Cache-Control' => 'max-age=3600',
  54. ]);
  55. $this->assertEquals('max-age=3600, private', $bag->get('Cache-Control'));
  56. $bag = new ResponseHeaderBag(['Last-Modified' => 'abcde']);
  57. $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
  58. $bag = new ResponseHeaderBag(['Etag' => 'abcde', 'Last-Modified' => 'abcde']);
  59. $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
  60. $bag = new ResponseHeaderBag(['cache-control' => 'max-age=100']);
  61. $this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
  62. $bag = new ResponseHeaderBag(['cache-control' => 's-maxage=100']);
  63. $this->assertEquals('s-maxage=100', $bag->get('Cache-Control'));
  64. $bag = new ResponseHeaderBag(['cache-control' => 'private, max-age=100']);
  65. $this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
  66. $bag = new ResponseHeaderBag(['cache-control' => 'public, max-age=100']);
  67. $this->assertEquals('max-age=100, public', $bag->get('Cache-Control'));
  68. $bag = new ResponseHeaderBag();
  69. $bag->set('Last-Modified', 'abcde');
  70. $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
  71. $bag = new ResponseHeaderBag();
  72. $bag->set('Cache-Control', ['public', 'must-revalidate']);
  73. $this->assertCount(1, $bag->get('Cache-Control', null, false));
  74. $this->assertEquals('must-revalidate, public', $bag->get('Cache-Control'));
  75. $bag = new ResponseHeaderBag();
  76. $bag->set('Cache-Control', 'public');
  77. $bag->set('Cache-Control', 'must-revalidate', false);
  78. $this->assertCount(1, $bag->get('Cache-Control', null, false));
  79. $this->assertEquals('must-revalidate, public', $bag->get('Cache-Control'));
  80. }
  81. public function testCacheControlClone()
  82. {
  83. $headers = ['foo' => 'bar'];
  84. $bag1 = new ResponseHeaderBag($headers);
  85. $bag2 = new ResponseHeaderBag($bag1->allPreserveCase());
  86. $this->assertEquals($bag1->allPreserveCase(), $bag2->allPreserveCase());
  87. }
  88. public function testToStringIncludesCookieHeaders()
  89. {
  90. $bag = new ResponseHeaderBag([]);
  91. $bag->setCookie(new Cookie('foo', 'bar'));
  92. $this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
  93. $bag->clearCookie('foo');
  94. $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; Max-Age=0; path=/; httponly', $bag);
  95. }
  96. public function testClearCookieSecureNotHttpOnly()
  97. {
  98. $bag = new ResponseHeaderBag([]);
  99. $bag->clearCookie('foo', '/', null, true, false);
  100. $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; Max-Age=0; path=/; secure', $bag);
  101. }
  102. public function testClearCookieSamesite()
  103. {
  104. $bag = new ResponseHeaderBag([]);
  105. $bag->clearCookie('foo', '/', null, true, false, 'none');
  106. $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; Max-Age=0; path=/; secure; samesite=none', $bag);
  107. }
  108. public function testReplace()
  109. {
  110. $bag = new ResponseHeaderBag([]);
  111. $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
  112. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  113. $bag->replace(['Cache-Control' => 'public']);
  114. $this->assertEquals('public', $bag->get('Cache-Control'));
  115. $this->assertTrue($bag->hasCacheControlDirective('public'));
  116. }
  117. public function testReplaceWithRemove()
  118. {
  119. $bag = new ResponseHeaderBag([]);
  120. $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
  121. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  122. $bag->remove('Cache-Control');
  123. $bag->replace([]);
  124. $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
  125. $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
  126. }
  127. public function testCookiesWithSameNames()
  128. {
  129. $bag = new ResponseHeaderBag();
  130. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
  131. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'foo.bar'));
  132. $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'bar.foo'));
  133. $bag->setCookie(new Cookie('foo', 'bar'));
  134. $this->assertCount(4, $bag->getCookies());
  135. $this->assertEquals('foo=bar; path=/path/foo; domain=foo.bar; httponly', $bag->get('set-cookie'));
  136. $this->assertEquals([
  137. 'foo=bar; path=/path/foo; domain=foo.bar; httponly',
  138. 'foo=bar; path=/path/bar; domain=foo.bar; httponly',
  139. 'foo=bar; path=/path/bar; domain=bar.foo; httponly',
  140. 'foo=bar; path=/; httponly',
  141. ], $bag->get('set-cookie', null, false));
  142. $this->assertSetCookieHeader('foo=bar; path=/path/foo; domain=foo.bar; httponly', $bag);
  143. $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=foo.bar; httponly', $bag);
  144. $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=bar.foo; httponly', $bag);
  145. $this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
  146. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  147. $this->assertArrayHasKey('foo', $cookies['foo.bar']['/path/foo']);
  148. $this->assertArrayHasKey('foo', $cookies['foo.bar']['/path/bar']);
  149. $this->assertArrayHasKey('foo', $cookies['bar.foo']['/path/bar']);
  150. $this->assertArrayHasKey('foo', $cookies['']['/']);
  151. }
  152. public function testRemoveCookie()
  153. {
  154. $bag = new ResponseHeaderBag();
  155. $this->assertFalse($bag->has('set-cookie'));
  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. $this->assertTrue($bag->has('set-cookie'));
  159. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  160. $this->assertArrayHasKey('/path/foo', $cookies['foo.bar']);
  161. $bag->removeCookie('foo', '/path/foo', 'foo.bar');
  162. $this->assertTrue($bag->has('set-cookie'));
  163. $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
  164. $this->assertArrayNotHasKey('/path/foo', $cookies['foo.bar']);
  165. $bag->removeCookie('bar', '/path/bar', 'foo.bar');
  166. $this->assertFalse($bag->has('set-cookie'));
  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. public function testSetCookieHeader()
  185. {
  186. $bag = new ResponseHeaderBag();
  187. $bag->set('set-cookie', 'foo=bar');
  188. $this->assertEquals([new Cookie('foo', 'bar', 0, '/', null, false, false, true)], $bag->getCookies());
  189. $bag->set('set-cookie', 'foo2=bar2', false);
  190. $this->assertEquals([
  191. new Cookie('foo', 'bar', 0, '/', null, false, false, true),
  192. new Cookie('foo2', 'bar2', 0, '/', null, false, false, true),
  193. ], $bag->getCookies());
  194. $bag->remove('set-cookie');
  195. $this->assertEquals([], $bag->getCookies());
  196. }
  197. public function testGetCookiesWithInvalidArgument()
  198. {
  199. $this->expectException('InvalidArgumentException');
  200. $bag = new ResponseHeaderBag();
  201. $bag->getCookies('invalid_argument');
  202. }
  203. public function testMakeDispositionInvalidDisposition()
  204. {
  205. $this->expectException('InvalidArgumentException');
  206. $headers = new ResponseHeaderBag();
  207. $headers->makeDisposition('invalid', 'foo.html');
  208. }
  209. /**
  210. * @dataProvider provideMakeDisposition
  211. */
  212. public function testMakeDisposition($disposition, $filename, $filenameFallback, $expected)
  213. {
  214. $headers = new ResponseHeaderBag();
  215. $this->assertEquals($expected, $headers->makeDisposition($disposition, $filename, $filenameFallback));
  216. }
  217. public function testToStringDoesntMessUpHeaders()
  218. {
  219. $headers = new ResponseHeaderBag();
  220. $headers->set('Location', 'http://www.symfony.com');
  221. $headers->set('Content-type', 'text/html');
  222. (string) $headers;
  223. $allHeaders = $headers->allPreserveCase();
  224. $this->assertEquals(['http://www.symfony.com'], $allHeaders['Location']);
  225. $this->assertEquals(['text/html'], $allHeaders['Content-type']);
  226. }
  227. public function provideMakeDisposition()
  228. {
  229. return [
  230. ['attachment', 'foo.html', 'foo.html', 'attachment; filename="foo.html"'],
  231. ['attachment', 'foo.html', '', 'attachment; filename="foo.html"'],
  232. ['attachment', 'foo bar.html', '', 'attachment; filename="foo bar.html"'],
  233. ['attachment', 'foo "bar".html', '', 'attachment; filename="foo \\"bar\\".html"'],
  234. ['attachment', 'foo%20bar.html', 'foo bar.html', 'attachment; filename="foo bar.html"; filename*=utf-8\'\'foo%2520bar.html'],
  235. ['attachment', 'föö.html', 'foo.html', 'attachment; filename="foo.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html'],
  236. ];
  237. }
  238. /**
  239. * @dataProvider provideMakeDispositionFail
  240. */
  241. public function testMakeDispositionFail($disposition, $filename)
  242. {
  243. $this->expectException('InvalidArgumentException');
  244. $headers = new ResponseHeaderBag();
  245. $headers->makeDisposition($disposition, $filename);
  246. }
  247. public function provideMakeDispositionFail()
  248. {
  249. return [
  250. ['attachment', 'foo%20bar.html'],
  251. ['attachment', 'foo/bar.html'],
  252. ['attachment', '/foo.html'],
  253. ['attachment', 'foo\bar.html'],
  254. ['attachment', '\foo.html'],
  255. ['attachment', 'föö.html'],
  256. ];
  257. }
  258. public function testDateHeaderAddedOnCreation()
  259. {
  260. $now = time();
  261. $bag = new ResponseHeaderBag();
  262. $this->assertTrue($bag->has('Date'));
  263. $this->assertEquals($now, $bag->getDate('Date')->getTimestamp());
  264. }
  265. public function testDateHeaderCanBeSetOnCreation()
  266. {
  267. $someDate = 'Thu, 23 Mar 2017 09:15:12 GMT';
  268. $bag = new ResponseHeaderBag(['Date' => $someDate]);
  269. $this->assertEquals($someDate, $bag->get('Date'));
  270. }
  271. public function testDateHeaderWillBeRecreatedWhenRemoved()
  272. {
  273. $someDate = 'Thu, 23 Mar 2017 09:15:12 GMT';
  274. $bag = new ResponseHeaderBag(['Date' => $someDate]);
  275. $bag->remove('Date');
  276. // a (new) Date header is still present
  277. $this->assertTrue($bag->has('Date'));
  278. $this->assertNotEquals($someDate, $bag->get('Date'));
  279. }
  280. public function testDateHeaderWillBeRecreatedWhenHeadersAreReplaced()
  281. {
  282. $bag = new ResponseHeaderBag();
  283. $bag->replace([]);
  284. $this->assertTrue($bag->has('Date'));
  285. }
  286. private function assertSetCookieHeader($expected, ResponseHeaderBag $actual)
  287. {
  288. $this->assertRegExp('#^Set-Cookie:\s+'.preg_quote($expected, '#').'$#m', str_replace("\r\n", "\n", (string) $actual));
  289. }
  290. }