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, private')),
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, private')),
49 array('P3P' => 'CP="CAO PSA OUR"'),
50 array('P3P' => array('CP="CAO PSA OUR"'), 'Cache-Control' => array('no-cache, private')),
53 array('WWW-Authenticate' => 'Basic realm="WallyWorld"'),
54 array('WWW-Authenticate' => array('Basic realm="WallyWorld"'), 'Cache-Control' => array('no-cache, private')),
57 array('X-UA-Compatible' => 'IE=edge,chrome=1'),
58 array('X-UA-Compatible' => array('IE=edge,chrome=1'), 'Cache-Control' => array('no-cache, private')),
61 array('X-XSS-Protection' => '1; mode=block'),
62 array('X-XSS-Protection' => array('1; mode=block'), 'Cache-Control' => array('no-cache, private')),
67 public function testCacheControlHeader()
69 $bag = new ResponseHeaderBag(array());
70 $this->assertEquals('no-cache, private', $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 testCacheControlClone()
117 $headers = array('foo' => 'bar');
118 $bag1 = new ResponseHeaderBag($headers);
119 $bag2 = new ResponseHeaderBag($bag1->allPreserveCase());
120 $this->assertEquals($bag1->allPreserveCase(), $bag2->allPreserveCase());
123 public function testToStringIncludesCookieHeaders()
125 $bag = new ResponseHeaderBag(array());
126 $bag->setCookie(new Cookie('foo', 'bar'));
128 $this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
130 $bag->clearCookie('foo');
132 $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/; httponly', $bag);
135 public function testClearCookieSecureNotHttpOnly()
137 $bag = new ResponseHeaderBag(array());
139 $bag->clearCookie('foo', '/', null, true, false);
141 $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/; secure', $bag);
144 public function testReplace()
146 $bag = new ResponseHeaderBag(array());
147 $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
148 $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
150 $bag->replace(array('Cache-Control' => 'public'));
151 $this->assertEquals('public', $bag->get('Cache-Control'));
152 $this->assertTrue($bag->hasCacheControlDirective('public'));
155 public function testReplaceWithRemove()
157 $bag = new ResponseHeaderBag(array());
158 $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
159 $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
161 $bag->remove('Cache-Control');
162 $bag->replace(array());
163 $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
164 $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
167 public function testCookiesWithSameNames()
169 $bag = new ResponseHeaderBag();
170 $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
171 $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'foo.bar'));
172 $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'bar.foo'));
173 $bag->setCookie(new Cookie('foo', 'bar'));
175 $this->assertCount(4, $bag->getCookies());
177 $this->assertSetCookieHeader('foo=bar; path=/path/foo; domain=foo.bar; httponly', $bag);
178 $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=foo.bar; httponly', $bag);
179 $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=bar.foo; httponly', $bag);
180 $this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
182 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
183 $this->assertTrue(isset($cookies['foo.bar']['/path/foo']['foo']));
184 $this->assertTrue(isset($cookies['foo.bar']['/path/bar']['foo']));
185 $this->assertTrue(isset($cookies['bar.foo']['/path/bar']['foo']));
186 $this->assertTrue(isset($cookies['']['/']['foo']));
189 public function testRemoveCookie()
191 $bag = new ResponseHeaderBag();
192 $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
193 $bag->setCookie(new Cookie('bar', 'foo', 0, '/path/bar', 'foo.bar'));
195 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
196 $this->assertTrue(isset($cookies['foo.bar']['/path/foo']));
198 $bag->removeCookie('foo', '/path/foo', 'foo.bar');
200 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
201 $this->assertFalse(isset($cookies['foo.bar']['/path/foo']));
203 $bag->removeCookie('bar', '/path/bar', 'foo.bar');
205 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
206 $this->assertFalse(isset($cookies['foo.bar']));
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->assertTrue(isset($cookies['']['/']));
218 $bag->removeCookie('foo', null);
219 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
220 $this->assertFalse(isset($cookies['']['/']['foo']));
222 $bag->removeCookie('bar', null);
223 $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
224 $this->assertFalse(isset($cookies['']['/']['bar']));
228 * @expectedException \InvalidArgumentException
230 public function testGetCookiesWithInvalidArgument()
232 $bag = new ResponseHeaderBag();
234 $bag->getCookies('invalid_argument');
238 * @expectedException \InvalidArgumentException
240 public function testMakeDispositionInvalidDisposition()
242 $headers = new ResponseHeaderBag();
244 $headers->makeDisposition('invalid', 'foo.html');
248 * @dataProvider provideMakeDisposition
250 public function testMakeDisposition($disposition, $filename, $filenameFallback, $expected)
252 $headers = new ResponseHeaderBag();
254 $this->assertEquals($expected, $headers->makeDisposition($disposition, $filename, $filenameFallback));
257 public function testToStringDoesntMessUpHeaders()
259 $headers = new ResponseHeaderBag();
261 $headers->set('Location', 'http://www.symfony.com');
262 $headers->set('Content-type', 'text/html');
266 $allHeaders = $headers->allPreserveCase();
267 $this->assertEquals(array('http://www.symfony.com'), $allHeaders['Location']);
268 $this->assertEquals(array('text/html'), $allHeaders['Content-type']);
271 public function provideMakeDisposition()
274 array('attachment', 'foo.html', 'foo.html', 'attachment; filename="foo.html"'),
275 array('attachment', 'foo.html', '', 'attachment; filename="foo.html"'),
276 array('attachment', 'foo bar.html', '', 'attachment; filename="foo bar.html"'),
277 array('attachment', 'foo "bar".html', '', 'attachment; filename="foo \\"bar\\".html"'),
278 array('attachment', 'foo%20bar.html', 'foo bar.html', 'attachment; filename="foo bar.html"; filename*=utf-8\'\'foo%2520bar.html'),
279 array('attachment', 'föö.html', 'foo.html', 'attachment; filename="foo.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html'),
284 * @dataProvider provideMakeDispositionFail
285 * @expectedException \InvalidArgumentException
287 public function testMakeDispositionFail($disposition, $filename)
289 $headers = new ResponseHeaderBag();
291 $headers->makeDisposition($disposition, $filename);
294 public function provideMakeDispositionFail()
297 array('attachment', 'foo%20bar.html'),
298 array('attachment', 'foo/bar.html'),
299 array('attachment', '/foo.html'),
300 array('attachment', 'foo\bar.html'),
301 array('attachment', '\foo.html'),
302 array('attachment', 'föö.html'),
306 private function assertSetCookieHeader($expected, ResponseHeaderBag $actual)
308 $this->assertRegExp('#^Set-Cookie:\s+'.preg_quote($expected, '#').'$#m', str_replace("\r\n", "\n", (string) $actual));