Version 1
[yaffs-website] / vendor / symfony / http-foundation / Tests / ResponseHeaderBagTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpFoundation\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\ResponseHeaderBag;
16 use Symfony\Component\HttpFoundation\Cookie;
17
18 /**
19  * @group time-sensitive
20  */
21 class ResponseHeaderBagTest extends TestCase
22 {
23     /**
24      * @dataProvider provideAllPreserveCase
25      */
26     public function testAllPreserveCase($headers, $expected)
27     {
28         $bag = new ResponseHeaderBag($headers);
29
30         $this->assertEquals($expected, $bag->allPreserveCase(), '->allPreserveCase() gets all input keys in original case');
31     }
32
33     public function provideAllPreserveCase()
34     {
35         return array(
36             array(
37                 array('fOo' => 'BAR'),
38                 array('fOo' => array('BAR'), 'Cache-Control' => array('no-cache')),
39             ),
40             array(
41                 array('ETag' => 'xyzzy'),
42                 array('ETag' => array('xyzzy'), 'Cache-Control' => array('private, must-revalidate')),
43             ),
44             array(
45                 array('Content-MD5' => 'Q2hlY2sgSW50ZWdyaXR5IQ=='),
46                 array('Content-MD5' => array('Q2hlY2sgSW50ZWdyaXR5IQ=='), 'Cache-Control' => array('no-cache')),
47             ),
48             array(
49                 array('P3P' => 'CP="CAO PSA OUR"'),
50                 array('P3P' => array('CP="CAO PSA OUR"'), 'Cache-Control' => array('no-cache')),
51             ),
52             array(
53                 array('WWW-Authenticate' => 'Basic realm="WallyWorld"'),
54                 array('WWW-Authenticate' => array('Basic realm="WallyWorld"'), 'Cache-Control' => array('no-cache')),
55             ),
56             array(
57                 array('X-UA-Compatible' => 'IE=edge,chrome=1'),
58                 array('X-UA-Compatible' => array('IE=edge,chrome=1'), 'Cache-Control' => array('no-cache')),
59             ),
60             array(
61                 array('X-XSS-Protection' => '1; mode=block'),
62                 array('X-XSS-Protection' => array('1; mode=block'), 'Cache-Control' => array('no-cache')),
63             ),
64         );
65     }
66
67     public function testCacheControlHeader()
68     {
69         $bag = new ResponseHeaderBag(array());
70         $this->assertEquals('no-cache', $bag->get('Cache-Control'));
71         $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
72
73         $bag = new ResponseHeaderBag(array('Cache-Control' => 'public'));
74         $this->assertEquals('public', $bag->get('Cache-Control'));
75         $this->assertTrue($bag->hasCacheControlDirective('public'));
76
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'));
82
83         $bag = new ResponseHeaderBag(array('Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT'));
84         $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
85
86         $bag = new ResponseHeaderBag(array(
87             'Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT',
88             'Cache-Control' => 'max-age=3600',
89         ));
90         $this->assertEquals('max-age=3600, private', $bag->get('Cache-Control'));
91
92         $bag = new ResponseHeaderBag(array('Last-Modified' => 'abcde'));
93         $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
94
95         $bag = new ResponseHeaderBag(array('Etag' => 'abcde', 'Last-Modified' => 'abcde'));
96         $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
97
98         $bag = new ResponseHeaderBag(array('cache-control' => 'max-age=100'));
99         $this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
100
101         $bag = new ResponseHeaderBag(array('cache-control' => 's-maxage=100'));
102         $this->assertEquals('s-maxage=100', $bag->get('Cache-Control'));
103
104         $bag = new ResponseHeaderBag(array('cache-control' => 'private, max-age=100'));
105         $this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
106
107         $bag = new ResponseHeaderBag(array('cache-control' => 'public, max-age=100'));
108         $this->assertEquals('max-age=100, public', $bag->get('Cache-Control'));
109
110         $bag = new ResponseHeaderBag();
111         $bag->set('Last-Modified', 'abcde');
112         $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
113     }
114
115     public function testToStringIncludesCookieHeaders()
116     {
117         $bag = new ResponseHeaderBag(array());
118         $bag->setCookie(new Cookie('foo', 'bar'));
119
120         $this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
121
122         $bag->clearCookie('foo');
123
124         $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/; httponly', $bag);
125     }
126
127     public function testClearCookieSecureNotHttpOnly()
128     {
129         $bag = new ResponseHeaderBag(array());
130
131         $bag->clearCookie('foo', '/', null, true, false);
132
133         $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/; secure', $bag);
134     }
135
136     public function testReplace()
137     {
138         $bag = new ResponseHeaderBag(array());
139         $this->assertEquals('no-cache', $bag->get('Cache-Control'));
140         $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
141
142         $bag->replace(array('Cache-Control' => 'public'));
143         $this->assertEquals('public', $bag->get('Cache-Control'));
144         $this->assertTrue($bag->hasCacheControlDirective('public'));
145     }
146
147     public function testReplaceWithRemove()
148     {
149         $bag = new ResponseHeaderBag(array());
150         $this->assertEquals('no-cache', $bag->get('Cache-Control'));
151         $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
152
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'));
157     }
158
159     public function testCookiesWithSameNames()
160     {
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'));
166
167         $this->assertCount(4, $bag->getCookies());
168
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);
173
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']));
179     }
180
181     public function testRemoveCookie()
182     {
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'));
186
187         $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
188         $this->assertTrue(isset($cookies['foo.bar']['/path/foo']));
189
190         $bag->removeCookie('foo', '/path/foo', 'foo.bar');
191
192         $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
193         $this->assertFalse(isset($cookies['foo.bar']['/path/foo']));
194
195         $bag->removeCookie('bar', '/path/bar', 'foo.bar');
196
197         $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
198         $this->assertFalse(isset($cookies['foo.bar']));
199     }
200
201     public function testRemoveCookieWithNullRemove()
202     {
203         $bag = new ResponseHeaderBag();
204         $bag->setCookie(new Cookie('foo', 'bar', 0));
205         $bag->setCookie(new Cookie('bar', 'foo', 0));
206
207         $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
208         $this->assertTrue(isset($cookies['']['/']));
209
210         $bag->removeCookie('foo', null);
211         $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
212         $this->assertFalse(isset($cookies['']['/']['foo']));
213
214         $bag->removeCookie('bar', null);
215         $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
216         $this->assertFalse(isset($cookies['']['/']['bar']));
217     }
218
219     /**
220      * @expectedException \InvalidArgumentException
221      */
222     public function testGetCookiesWithInvalidArgument()
223     {
224         $bag = new ResponseHeaderBag();
225
226         $bag->getCookies('invalid_argument');
227     }
228
229     /**
230      * @expectedException \InvalidArgumentException
231      */
232     public function testMakeDispositionInvalidDisposition()
233     {
234         $headers = new ResponseHeaderBag();
235
236         $headers->makeDisposition('invalid', 'foo.html');
237     }
238
239     /**
240      * @dataProvider provideMakeDisposition
241      */
242     public function testMakeDisposition($disposition, $filename, $filenameFallback, $expected)
243     {
244         $headers = new ResponseHeaderBag();
245
246         $this->assertEquals($expected, $headers->makeDisposition($disposition, $filename, $filenameFallback));
247     }
248
249     public function testToStringDoesntMessUpHeaders()
250     {
251         $headers = new ResponseHeaderBag();
252
253         $headers->set('Location', 'http://www.symfony.com');
254         $headers->set('Content-type', 'text/html');
255
256         (string) $headers;
257
258         $allHeaders = $headers->allPreserveCase();
259         $this->assertEquals(array('http://www.symfony.com'), $allHeaders['Location']);
260         $this->assertEquals(array('text/html'), $allHeaders['Content-type']);
261     }
262
263     public function provideMakeDisposition()
264     {
265         return array(
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'),
272         );
273     }
274
275     /**
276      * @dataProvider provideMakeDispositionFail
277      * @expectedException \InvalidArgumentException
278      */
279     public function testMakeDispositionFail($disposition, $filename)
280     {
281         $headers = new ResponseHeaderBag();
282
283         $headers->makeDisposition($disposition, $filename);
284     }
285
286     public function provideMakeDispositionFail()
287     {
288         return array(
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'),
295         );
296     }
297
298     private function assertSetCookieHeader($expected, ResponseHeaderBag $actual)
299     {
300         $this->assertRegExp('#^Set-Cookie:\s+'.preg_quote($expected, '#').'$#m', str_replace("\r\n", "\n", (string) $actual));
301     }
302 }