4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Cookie;
16 use Symfony\Component\HttpFoundation\ResponseHeaderBag;
19 * @group time-sensitive
21 class ResponseHeaderBagTest extends TestCase
23 public function testAllPreserveCase()
28 'Content-MD5' => 'Q2hlY2sgSW50ZWdyaXR5IQ==',
29 'P3P' => 'CP="CAO PSA OUR"',
30 'WWW-Authenticate' => 'Basic realm="WallyWorld"',
31 'X-UA-Compatible' => 'IE=edge,chrome=1',
32 'X-XSS-Protection' => '1; mode=block',
35 $bag = new ResponseHeaderBag($headers);
36 $allPreservedCase = $bag->allPreserveCase();
38 foreach (array_keys($headers) as $headerName) {
39 $this->assertArrayHasKey($headerName, $allPreservedCase, '->allPreserveCase() gets all input keys in original case');
43 public function testCacheControlHeader()
45 $bag = new ResponseHeaderBag(array());
46 $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
47 $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
49 $bag = new ResponseHeaderBag(array('Cache-Control' => 'public'));
50 $this->assertEquals('public', $bag->get('Cache-Control'));
51 $this->assertTrue($bag->hasCacheControlDirective('public'));
53 $bag = new ResponseHeaderBag(array('ETag' => 'abcde'));
54 $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
55 $this->assertTrue($bag->hasCacheControlDirective('private'));
56 $this->assertTrue($bag->hasCacheControlDirective('must-revalidate'));
57 $this->assertFalse($bag->hasCacheControlDirective('max-age'));
59 $bag = new ResponseHeaderBag(array('Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT'));
60 $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
62 $bag = new ResponseHeaderBag(array(
63 'Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT',
64 'Cache-Control' => 'max-age=3600',
66 $this->assertEquals('max-age=3600, private', $bag->get('Cache-Control'));
68 $bag = new ResponseHeaderBag(array('Last-Modified' => 'abcde'));
69 $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
71 $bag = new ResponseHeaderBag(array('Etag' => 'abcde', 'Last-Modified' => 'abcde'));
72 $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
74 $bag = new ResponseHeaderBag(array('cache-control' => 'max-age=100'));
75 $this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
77 $bag = new ResponseHeaderBag(array('cache-control' => 's-maxage=100'));
78 $this->assertEquals('s-maxage=100', $bag->get('Cache-Control'));
80 $bag = new ResponseHeaderBag(array('cache-control' => 'private, max-age=100'));
81 $this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
83 $bag = new ResponseHeaderBag(array('cache-control' => 'public, max-age=100'));
84 $this->assertEquals('max-age=100, public', $bag->get('Cache-Control'));
86 $bag = new ResponseHeaderBag();
87 $bag->set('Last-Modified', 'abcde');
88 $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
90 $bag = new ResponseHeaderBag();
91 $bag->set('Cache-Control', array('public', 'must-revalidate'));
92 $this->assertCount(1, $bag->get('Cache-Control', null, false));
93 $this->assertEquals('must-revalidate, public', $bag->get('Cache-Control'));
95 $bag = new ResponseHeaderBag();
96 $bag->set('Cache-Control', 'public');
97 $bag->set('Cache-Control', 'must-revalidate', false);
98 $this->assertCount(1, $bag->get('Cache-Control', null, false));
99 $this->assertEquals('must-revalidate, public', $bag->get('Cache-Control'));
102 public function testCacheControlClone()
104 $headers = array('foo' => 'bar');
105 $bag1 = new ResponseHeaderBag($headers);
106 $bag2 = new ResponseHeaderBag($bag1->allPreserveCase());
107 $this->assertEquals($bag1->allPreserveCase(), $bag2->allPreserveCase());
110 public function testToStringIncludesCookieHeaders()
112 $bag = new ResponseHeaderBag(array());
113 $bag->setCookie(new Cookie('foo', 'bar'));
115 $this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
117 $bag->clearCookie('foo');
119 $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; Max-Age=0; path=/; httponly', $bag);
122 public function testClearCookieSecureNotHttpOnly()
124 $bag = new ResponseHeaderBag(array());
126 $bag->clearCookie('foo', '/', null, true, false);
128 $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; Max-Age=0; path=/; secure', $bag);
131 public function testReplace()
133 $bag = new ResponseHeaderBag(array());
134 $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
135 $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
137 $bag->replace(array('Cache-Control' => 'public'));
138 $this->assertEquals('public', $bag->get('Cache-Control'));
139 $this->assertTrue($bag->hasCacheControlDirective('public'));
142 public function testReplaceWithRemove()
144 $bag = new ResponseHeaderBag(array());
145 $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
146 $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
148 $bag->remove('Cache-Control');
149 $bag->replace(array());
150 $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
151 $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
154 public function testCookiesWithSameNames()
156 $bag = new ResponseHeaderBag();
157 $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
158 $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'foo.bar'));
159 $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'bar.foo'));
160 $bag->setCookie(new Cookie('foo', 'bar'));
162 $this->assertCount(4, $bag->getCookies());
163 $this->assertEquals('foo=bar; path=/path/foo; domain=foo.bar; httponly', $bag->get('set-cookie'));
164 $this->assertEquals(array(
165 'foo=bar; path=/path/foo; domain=foo.bar; httponly',
166 'foo=bar; path=/path/bar; domain=foo.bar; httponly',
167 'foo=bar; path=/path/bar; domain=bar.foo; httponly',
168 'foo=bar; path=/; httponly',
169 ), $bag->get('set-cookie', null, false));
171 $this->assertSetCookieHeader('foo=bar; path=/path/foo; domain=foo.bar; httponly', $bag);
172 $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=foo.bar; httponly', $bag);
173 $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=bar.foo; httponly', $bag);
174 $this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
176 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
178 $this->assertArrayHasKey('foo', $cookies['foo.bar']['/path/foo']);
179 $this->assertArrayHasKey('foo', $cookies['foo.bar']['/path/bar']);
180 $this->assertArrayHasKey('foo', $cookies['bar.foo']['/path/bar']);
181 $this->assertArrayHasKey('foo', $cookies['']['/']);
184 public function testRemoveCookie()
186 $bag = new ResponseHeaderBag();
187 $this->assertFalse($bag->has('set-cookie'));
189 $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
190 $bag->setCookie(new Cookie('bar', 'foo', 0, '/path/bar', 'foo.bar'));
191 $this->assertTrue($bag->has('set-cookie'));
193 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
194 $this->assertArrayHasKey('/path/foo', $cookies['foo.bar']);
196 $bag->removeCookie('foo', '/path/foo', 'foo.bar');
197 $this->assertTrue($bag->has('set-cookie'));
199 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
200 $this->assertArrayNotHasKey('/path/foo', $cookies['foo.bar']);
202 $bag->removeCookie('bar', '/path/bar', 'foo.bar');
203 $this->assertFalse($bag->has('set-cookie'));
205 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
206 $this->assertArrayNotHasKey('foo.bar', $cookies);
209 public function testRemoveCookieWithNullRemove()
211 $bag = new ResponseHeaderBag();
212 $bag->setCookie(new Cookie('foo', 'bar', 0));
213 $bag->setCookie(new Cookie('bar', 'foo', 0));
215 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
216 $this->assertArrayHasKey('/', $cookies['']);
218 $bag->removeCookie('foo', null);
219 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
220 $this->assertArrayNotHasKey('foo', $cookies['']['/']);
222 $bag->removeCookie('bar', null);
223 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
224 $this->assertFalse(isset($cookies['']['/']['bar']));
227 public function testSetCookieHeader()
229 $bag = new ResponseHeaderBag();
230 $bag->set('set-cookie', 'foo=bar');
231 $this->assertEquals(array(new Cookie('foo', 'bar', 0, '/', null, false, false, true)), $bag->getCookies());
233 $bag->set('set-cookie', 'foo2=bar2', false);
234 $this->assertEquals(array(
235 new Cookie('foo', 'bar', 0, '/', null, false, false, true),
236 new Cookie('foo2', 'bar2', 0, '/', null, false, false, true),
237 ), $bag->getCookies());
239 $bag->remove('set-cookie');
240 $this->assertEquals(array(), $bag->getCookies());
244 * @expectedException \InvalidArgumentException
246 public function testGetCookiesWithInvalidArgument()
248 $bag = new ResponseHeaderBag();
250 $bag->getCookies('invalid_argument');
254 * @expectedException \InvalidArgumentException
256 public function testMakeDispositionInvalidDisposition()
258 $headers = new ResponseHeaderBag();
260 $headers->makeDisposition('invalid', 'foo.html');
264 * @dataProvider provideMakeDisposition
266 public function testMakeDisposition($disposition, $filename, $filenameFallback, $expected)
268 $headers = new ResponseHeaderBag();
270 $this->assertEquals($expected, $headers->makeDisposition($disposition, $filename, $filenameFallback));
273 public function testToStringDoesntMessUpHeaders()
275 $headers = new ResponseHeaderBag();
277 $headers->set('Location', 'http://www.symfony.com');
278 $headers->set('Content-type', 'text/html');
282 $allHeaders = $headers->allPreserveCase();
283 $this->assertEquals(array('http://www.symfony.com'), $allHeaders['Location']);
284 $this->assertEquals(array('text/html'), $allHeaders['Content-type']);
287 public function provideMakeDisposition()
290 array('attachment', 'foo.html', 'foo.html', 'attachment; filename="foo.html"'),
291 array('attachment', 'foo.html', '', 'attachment; filename="foo.html"'),
292 array('attachment', 'foo bar.html', '', 'attachment; filename="foo bar.html"'),
293 array('attachment', 'foo "bar".html', '', 'attachment; filename="foo \\"bar\\".html"'),
294 array('attachment', 'foo%20bar.html', 'foo bar.html', 'attachment; filename="foo bar.html"; filename*=utf-8\'\'foo%2520bar.html'),
295 array('attachment', 'föö.html', 'foo.html', 'attachment; filename="foo.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html'),
300 * @dataProvider provideMakeDispositionFail
301 * @expectedException \InvalidArgumentException
303 public function testMakeDispositionFail($disposition, $filename)
305 $headers = new ResponseHeaderBag();
307 $headers->makeDisposition($disposition, $filename);
310 public function provideMakeDispositionFail()
313 array('attachment', 'foo%20bar.html'),
314 array('attachment', 'foo/bar.html'),
315 array('attachment', '/foo.html'),
316 array('attachment', 'foo\bar.html'),
317 array('attachment', '\foo.html'),
318 array('attachment', 'föö.html'),
322 public function testDateHeaderAddedOnCreation()
326 $bag = new ResponseHeaderBag();
327 $this->assertTrue($bag->has('Date'));
329 $this->assertEquals($now, $bag->getDate('Date')->getTimestamp());
332 public function testDateHeaderCanBeSetOnCreation()
334 $someDate = 'Thu, 23 Mar 2017 09:15:12 GMT';
335 $bag = new ResponseHeaderBag(array('Date' => $someDate));
337 $this->assertEquals($someDate, $bag->get('Date'));
340 public function testDateHeaderWillBeRecreatedWhenRemoved()
342 $someDate = 'Thu, 23 Mar 2017 09:15:12 GMT';
343 $bag = new ResponseHeaderBag(array('Date' => $someDate));
344 $bag->remove('Date');
346 // a (new) Date header is still present
347 $this->assertTrue($bag->has('Date'));
348 $this->assertNotEquals($someDate, $bag->get('Date'));
351 public function testDateHeaderWillBeRecreatedWhenHeadersAreReplaced()
353 $bag = new ResponseHeaderBag();
354 $bag->replace(array());
356 $this->assertTrue($bag->has('Date'));
359 private function assertSetCookieHeader($expected, ResponseHeaderBag $actual)
361 $this->assertRegExp('#^Set-Cookie:\s+'.preg_quote($expected, '#').'$#m', str_replace("\r\n", "\n", (string) $actual));