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\ResponseHeaderBag;
16 use Symfony\Component\HttpFoundation\Cookie;
19 * @group time-sensitive
21 class ResponseHeaderBagTest extends TestCase
24 * @dataProvider provideAllPreserveCase
26 public function testAllPreserveCase($headers, $expected)
28 $bag = new ResponseHeaderBag($headers);
30 $this->assertEquals($expected, $bag->allPreserveCase(), '->allPreserveCase() gets all input keys in original case');
33 public function provideAllPreserveCase()
37 array('fOo' => 'BAR'),
38 array('fOo' => array('BAR'), 'Cache-Control' => array('no-cache')),
41 array('ETag' => 'xyzzy'),
42 array('ETag' => array('xyzzy'), 'Cache-Control' => array('private, must-revalidate')),
45 array('Content-MD5' => 'Q2hlY2sgSW50ZWdyaXR5IQ=='),
46 array('Content-MD5' => array('Q2hlY2sgSW50ZWdyaXR5IQ=='), 'Cache-Control' => array('no-cache')),
49 array('P3P' => 'CP="CAO PSA OUR"'),
50 array('P3P' => array('CP="CAO PSA OUR"'), 'Cache-Control' => array('no-cache')),
53 array('WWW-Authenticate' => 'Basic realm="WallyWorld"'),
54 array('WWW-Authenticate' => array('Basic realm="WallyWorld"'), 'Cache-Control' => array('no-cache')),
57 array('X-UA-Compatible' => 'IE=edge,chrome=1'),
58 array('X-UA-Compatible' => array('IE=edge,chrome=1'), 'Cache-Control' => array('no-cache')),
61 array('X-XSS-Protection' => '1; mode=block'),
62 array('X-XSS-Protection' => array('1; mode=block'), 'Cache-Control' => array('no-cache')),
67 public function testCacheControlHeader()
69 $bag = new ResponseHeaderBag(array());
70 $this->assertEquals('no-cache', $bag->get('Cache-Control'));
71 $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
73 $bag = new ResponseHeaderBag(array('Cache-Control' => 'public'));
74 $this->assertEquals('public', $bag->get('Cache-Control'));
75 $this->assertTrue($bag->hasCacheControlDirective('public'));
77 $bag = new ResponseHeaderBag(array('ETag' => 'abcde'));
78 $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
79 $this->assertTrue($bag->hasCacheControlDirective('private'));
80 $this->assertTrue($bag->hasCacheControlDirective('must-revalidate'));
81 $this->assertFalse($bag->hasCacheControlDirective('max-age'));
83 $bag = new ResponseHeaderBag(array('Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT'));
84 $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
86 $bag = new ResponseHeaderBag(array(
87 'Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT',
88 'Cache-Control' => 'max-age=3600',
90 $this->assertEquals('max-age=3600, private', $bag->get('Cache-Control'));
92 $bag = new ResponseHeaderBag(array('Last-Modified' => 'abcde'));
93 $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
95 $bag = new ResponseHeaderBag(array('Etag' => 'abcde', 'Last-Modified' => 'abcde'));
96 $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
98 $bag = new ResponseHeaderBag(array('cache-control' => 'max-age=100'));
99 $this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
101 $bag = new ResponseHeaderBag(array('cache-control' => 's-maxage=100'));
102 $this->assertEquals('s-maxage=100', $bag->get('Cache-Control'));
104 $bag = new ResponseHeaderBag(array('cache-control' => 'private, max-age=100'));
105 $this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
107 $bag = new ResponseHeaderBag(array('cache-control' => 'public, max-age=100'));
108 $this->assertEquals('max-age=100, public', $bag->get('Cache-Control'));
110 $bag = new ResponseHeaderBag();
111 $bag->set('Last-Modified', 'abcde');
112 $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
115 public function testToStringIncludesCookieHeaders()
117 $bag = new ResponseHeaderBag(array());
118 $bag->setCookie(new Cookie('foo', 'bar'));
120 $this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
122 $bag->clearCookie('foo');
124 $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/; httponly', $bag);
127 public function testClearCookieSecureNotHttpOnly()
129 $bag = new ResponseHeaderBag(array());
131 $bag->clearCookie('foo', '/', null, true, false);
133 $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/; secure', $bag);
136 public function testReplace()
138 $bag = new ResponseHeaderBag(array());
139 $this->assertEquals('no-cache', $bag->get('Cache-Control'));
140 $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
142 $bag->replace(array('Cache-Control' => 'public'));
143 $this->assertEquals('public', $bag->get('Cache-Control'));
144 $this->assertTrue($bag->hasCacheControlDirective('public'));
147 public function testReplaceWithRemove()
149 $bag = new ResponseHeaderBag(array());
150 $this->assertEquals('no-cache', $bag->get('Cache-Control'));
151 $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
153 $bag->remove('Cache-Control');
154 $bag->replace(array());
155 $this->assertEquals('no-cache', $bag->get('Cache-Control'));
156 $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
159 public function testCookiesWithSameNames()
161 $bag = new ResponseHeaderBag();
162 $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
163 $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'foo.bar'));
164 $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'bar.foo'));
165 $bag->setCookie(new Cookie('foo', 'bar'));
167 $this->assertCount(4, $bag->getCookies());
169 $this->assertSetCookieHeader('foo=bar; path=/path/foo; domain=foo.bar; httponly', $bag);
170 $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=foo.bar; httponly', $bag);
171 $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=bar.foo; httponly', $bag);
172 $this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
174 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
175 $this->assertTrue(isset($cookies['foo.bar']['/path/foo']['foo']));
176 $this->assertTrue(isset($cookies['foo.bar']['/path/bar']['foo']));
177 $this->assertTrue(isset($cookies['bar.foo']['/path/bar']['foo']));
178 $this->assertTrue(isset($cookies['']['/']['foo']));
181 public function testRemoveCookie()
183 $bag = new ResponseHeaderBag();
184 $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
185 $bag->setCookie(new Cookie('bar', 'foo', 0, '/path/bar', 'foo.bar'));
187 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
188 $this->assertTrue(isset($cookies['foo.bar']['/path/foo']));
190 $bag->removeCookie('foo', '/path/foo', 'foo.bar');
192 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
193 $this->assertFalse(isset($cookies['foo.bar']['/path/foo']));
195 $bag->removeCookie('bar', '/path/bar', 'foo.bar');
197 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
198 $this->assertFalse(isset($cookies['foo.bar']));
201 public function testRemoveCookieWithNullRemove()
203 $bag = new ResponseHeaderBag();
204 $bag->setCookie(new Cookie('foo', 'bar', 0));
205 $bag->setCookie(new Cookie('bar', 'foo', 0));
207 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
208 $this->assertTrue(isset($cookies['']['/']));
210 $bag->removeCookie('foo', null);
211 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
212 $this->assertFalse(isset($cookies['']['/']['foo']));
214 $bag->removeCookie('bar', null);
215 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
216 $this->assertFalse(isset($cookies['']['/']['bar']));
220 * @expectedException \InvalidArgumentException
222 public function testGetCookiesWithInvalidArgument()
224 $bag = new ResponseHeaderBag();
226 $bag->getCookies('invalid_argument');
230 * @expectedException \InvalidArgumentException
232 public function testMakeDispositionInvalidDisposition()
234 $headers = new ResponseHeaderBag();
236 $headers->makeDisposition('invalid', 'foo.html');
240 * @dataProvider provideMakeDisposition
242 public function testMakeDisposition($disposition, $filename, $filenameFallback, $expected)
244 $headers = new ResponseHeaderBag();
246 $this->assertEquals($expected, $headers->makeDisposition($disposition, $filename, $filenameFallback));
249 public function testToStringDoesntMessUpHeaders()
251 $headers = new ResponseHeaderBag();
253 $headers->set('Location', 'http://www.symfony.com');
254 $headers->set('Content-type', 'text/html');
258 $allHeaders = $headers->allPreserveCase();
259 $this->assertEquals(array('http://www.symfony.com'), $allHeaders['Location']);
260 $this->assertEquals(array('text/html'), $allHeaders['Content-type']);
263 public function provideMakeDisposition()
266 array('attachment', 'foo.html', 'foo.html', 'attachment; filename="foo.html"'),
267 array('attachment', 'foo.html', '', 'attachment; filename="foo.html"'),
268 array('attachment', 'foo bar.html', '', 'attachment; filename="foo bar.html"'),
269 array('attachment', 'foo "bar".html', '', 'attachment; filename="foo \\"bar\\".html"'),
270 array('attachment', 'foo%20bar.html', 'foo bar.html', 'attachment; filename="foo bar.html"; filename*=utf-8\'\'foo%2520bar.html'),
271 array('attachment', 'föö.html', 'foo.html', 'attachment; filename="foo.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html'),
276 * @dataProvider provideMakeDispositionFail
277 * @expectedException \InvalidArgumentException
279 public function testMakeDispositionFail($disposition, $filename)
281 $headers = new ResponseHeaderBag();
283 $headers->makeDisposition($disposition, $filename);
286 public function provideMakeDispositionFail()
289 array('attachment', 'foo%20bar.html'),
290 array('attachment', 'foo/bar.html'),
291 array('attachment', '/foo.html'),
292 array('attachment', 'foo\bar.html'),
293 array('attachment', '\foo.html'),
294 array('attachment', 'föö.html'),
298 private function assertSetCookieHeader($expected, ResponseHeaderBag $actual)
300 $this->assertRegExp('#^Set-Cookie:\s+'.preg_quote($expected, '#').'$#m', str_replace("\r\n", "\n", (string) $actual));