95284e21e497f798593f3f7af66457f3f3b2dc25
[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, private')),
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, private')),
47             ),
48             array(
49                 array('P3P' => 'CP="CAO PSA OUR"'),
50                 array('P3P' => array('CP="CAO PSA OUR"'), 'Cache-Control' => array('no-cache, private')),
51             ),
52             array(
53                 array('WWW-Authenticate' => 'Basic realm="WallyWorld"'),
54                 array('WWW-Authenticate' => array('Basic realm="WallyWorld"'), 'Cache-Control' => array('no-cache, private')),
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, private')),
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, private')),
63             ),
64         );
65     }
66
67     public function testCacheControlHeader()
68     {
69         $bag = new ResponseHeaderBag(array());
70         $this->assertEquals('no-cache, private', $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 testCacheControlClone()
116     {
117         $headers = array('foo' => 'bar');
118         $bag1 = new ResponseHeaderBag($headers);
119         $bag2 = new ResponseHeaderBag($bag1->allPreserveCase());
120         $this->assertEquals($bag1->allPreserveCase(), $bag2->allPreserveCase());
121     }
122
123     public function testToStringIncludesCookieHeaders()
124     {
125         $bag = new ResponseHeaderBag(array());
126         $bag->setCookie(new Cookie('foo', 'bar'));
127
128         $this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
129
130         $bag->clearCookie('foo');
131
132         $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/; httponly', $bag);
133     }
134
135     public function testClearCookieSecureNotHttpOnly()
136     {
137         $bag = new ResponseHeaderBag(array());
138
139         $bag->clearCookie('foo', '/', null, true, false);
140
141         $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; path=/; secure', $bag);
142     }
143
144     public function testReplace()
145     {
146         $bag = new ResponseHeaderBag(array());
147         $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
148         $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
149
150         $bag->replace(array('Cache-Control' => 'public'));
151         $this->assertEquals('public', $bag->get('Cache-Control'));
152         $this->assertTrue($bag->hasCacheControlDirective('public'));
153     }
154
155     public function testReplaceWithRemove()
156     {
157         $bag = new ResponseHeaderBag(array());
158         $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
159         $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
160
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'));
165     }
166
167     public function testCookiesWithSameNames()
168     {
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'));
174
175         $this->assertCount(4, $bag->getCookies());
176
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);
181
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']));
187     }
188
189     public function testRemoveCookie()
190     {
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'));
194
195         $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
196         $this->assertTrue(isset($cookies['foo.bar']['/path/foo']));
197
198         $bag->removeCookie('foo', '/path/foo', 'foo.bar');
199
200         $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
201         $this->assertFalse(isset($cookies['foo.bar']['/path/foo']));
202
203         $bag->removeCookie('bar', '/path/bar', 'foo.bar');
204
205         $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
206         $this->assertFalse(isset($cookies['foo.bar']));
207     }
208
209     public function testRemoveCookieWithNullRemove()
210     {
211         $bag = new ResponseHeaderBag();
212         $bag->setCookie(new Cookie('foo', 'bar', 0));
213         $bag->setCookie(new Cookie('bar', 'foo', 0));
214
215         $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
216         $this->assertTrue(isset($cookies['']['/']));
217
218         $bag->removeCookie('foo', null);
219         $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
220         $this->assertFalse(isset($cookies['']['/']['foo']));
221
222         $bag->removeCookie('bar', null);
223         $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
224         $this->assertFalse(isset($cookies['']['/']['bar']));
225     }
226
227     /**
228      * @expectedException \InvalidArgumentException
229      */
230     public function testGetCookiesWithInvalidArgument()
231     {
232         $bag = new ResponseHeaderBag();
233
234         $bag->getCookies('invalid_argument');
235     }
236
237     /**
238      * @expectedException \InvalidArgumentException
239      */
240     public function testMakeDispositionInvalidDisposition()
241     {
242         $headers = new ResponseHeaderBag();
243
244         $headers->makeDisposition('invalid', 'foo.html');
245     }
246
247     /**
248      * @dataProvider provideMakeDisposition
249      */
250     public function testMakeDisposition($disposition, $filename, $filenameFallback, $expected)
251     {
252         $headers = new ResponseHeaderBag();
253
254         $this->assertEquals($expected, $headers->makeDisposition($disposition, $filename, $filenameFallback));
255     }
256
257     public function testToStringDoesntMessUpHeaders()
258     {
259         $headers = new ResponseHeaderBag();
260
261         $headers->set('Location', 'http://www.symfony.com');
262         $headers->set('Content-type', 'text/html');
263
264         (string) $headers;
265
266         $allHeaders = $headers->allPreserveCase();
267         $this->assertEquals(array('http://www.symfony.com'), $allHeaders['Location']);
268         $this->assertEquals(array('text/html'), $allHeaders['Content-type']);
269     }
270
271     public function provideMakeDisposition()
272     {
273         return array(
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'),
280         );
281     }
282
283     /**
284      * @dataProvider provideMakeDispositionFail
285      * @expectedException \InvalidArgumentException
286      */
287     public function testMakeDispositionFail($disposition, $filename)
288     {
289         $headers = new ResponseHeaderBag();
290
291         $headers->makeDisposition($disposition, $filename);
292     }
293
294     public function provideMakeDispositionFail()
295     {
296         return array(
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'),
303         );
304     }
305
306     private function assertSetCookieHeader($expected, ResponseHeaderBag $actual)
307     {
308         $this->assertRegExp('#^Set-Cookie:\s+'.preg_quote($expected, '#').'$#m', str_replace("\r\n", "\n", (string) $actual));
309     }
310 }