Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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\Cookie;
16 use Symfony\Component\HttpFoundation\ResponseHeaderBag;
17
18 /**
19  * @group time-sensitive
20  */
21 class ResponseHeaderBagTest extends TestCase
22 {
23     public function testAllPreserveCase()
24     {
25         $headers = array(
26             'fOo' => 'BAR',
27             'ETag' => 'xyzzy',
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',
33         );
34
35         $bag = new ResponseHeaderBag($headers);
36         $allPreservedCase = $bag->allPreserveCase();
37
38         foreach (array_keys($headers) as $headerName) {
39             $this->assertArrayHasKey($headerName, $allPreservedCase, '->allPreserveCase() gets all input keys in original case');
40         }
41     }
42
43     public function testCacheControlHeader()
44     {
45         $bag = new ResponseHeaderBag(array());
46         $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
47         $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
48
49         $bag = new ResponseHeaderBag(array('Cache-Control' => 'public'));
50         $this->assertEquals('public', $bag->get('Cache-Control'));
51         $this->assertTrue($bag->hasCacheControlDirective('public'));
52
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'));
58
59         $bag = new ResponseHeaderBag(array('Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT'));
60         $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
61
62         $bag = new ResponseHeaderBag(array(
63             'Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT',
64             'Cache-Control' => 'max-age=3600',
65         ));
66         $this->assertEquals('max-age=3600, private', $bag->get('Cache-Control'));
67
68         $bag = new ResponseHeaderBag(array('Last-Modified' => 'abcde'));
69         $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
70
71         $bag = new ResponseHeaderBag(array('Etag' => 'abcde', 'Last-Modified' => 'abcde'));
72         $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
73
74         $bag = new ResponseHeaderBag(array('cache-control' => 'max-age=100'));
75         $this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
76
77         $bag = new ResponseHeaderBag(array('cache-control' => 's-maxage=100'));
78         $this->assertEquals('s-maxage=100', $bag->get('Cache-Control'));
79
80         $bag = new ResponseHeaderBag(array('cache-control' => 'private, max-age=100'));
81         $this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
82
83         $bag = new ResponseHeaderBag(array('cache-control' => 'public, max-age=100'));
84         $this->assertEquals('max-age=100, public', $bag->get('Cache-Control'));
85
86         $bag = new ResponseHeaderBag();
87         $bag->set('Last-Modified', 'abcde');
88         $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
89
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'));
94
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'));
100     }
101
102     public function testCacheControlClone()
103     {
104         $headers = array('foo' => 'bar');
105         $bag1 = new ResponseHeaderBag($headers);
106         $bag2 = new ResponseHeaderBag($bag1->allPreserveCase());
107         $this->assertEquals($bag1->allPreserveCase(), $bag2->allPreserveCase());
108     }
109
110     public function testToStringIncludesCookieHeaders()
111     {
112         $bag = new ResponseHeaderBag(array());
113         $bag->setCookie(new Cookie('foo', 'bar'));
114
115         $this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
116
117         $bag->clearCookie('foo');
118
119         $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; Max-Age=0; path=/; httponly', $bag);
120     }
121
122     public function testClearCookieSecureNotHttpOnly()
123     {
124         $bag = new ResponseHeaderBag(array());
125
126         $bag->clearCookie('foo', '/', null, true, false);
127
128         $this->assertSetCookieHeader('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; Max-Age=0; path=/; secure', $bag);
129     }
130
131     public function testReplace()
132     {
133         $bag = new ResponseHeaderBag(array());
134         $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
135         $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
136
137         $bag->replace(array('Cache-Control' => 'public'));
138         $this->assertEquals('public', $bag->get('Cache-Control'));
139         $this->assertTrue($bag->hasCacheControlDirective('public'));
140     }
141
142     public function testReplaceWithRemove()
143     {
144         $bag = new ResponseHeaderBag(array());
145         $this->assertEquals('no-cache, private', $bag->get('Cache-Control'));
146         $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
147
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'));
152     }
153
154     public function testCookiesWithSameNames()
155     {
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'));
161
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));
170
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);
175
176         $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
177
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['']['/']);
182     }
183
184     public function testRemoveCookie()
185     {
186         $bag = new ResponseHeaderBag();
187         $this->assertFalse($bag->has('set-cookie'));
188
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'));
192
193         $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
194         $this->assertArrayHasKey('/path/foo', $cookies['foo.bar']);
195
196         $bag->removeCookie('foo', '/path/foo', 'foo.bar');
197         $this->assertTrue($bag->has('set-cookie'));
198
199         $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
200         $this->assertArrayNotHasKey('/path/foo', $cookies['foo.bar']);
201
202         $bag->removeCookie('bar', '/path/bar', 'foo.bar');
203         $this->assertFalse($bag->has('set-cookie'));
204
205         $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
206         $this->assertArrayNotHasKey('foo.bar', $cookies);
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->assertArrayHasKey('/', $cookies['']);
217
218         $bag->removeCookie('foo', null);
219         $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
220         $this->assertArrayNotHasKey('foo', $cookies['']['/']);
221
222         $bag->removeCookie('bar', null);
223         $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
224         $this->assertFalse(isset($cookies['']['/']['bar']));
225     }
226
227     public function testSetCookieHeader()
228     {
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());
232
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());
238
239         $bag->remove('set-cookie');
240         $this->assertEquals(array(), $bag->getCookies());
241     }
242
243     /**
244      * @expectedException \InvalidArgumentException
245      */
246     public function testGetCookiesWithInvalidArgument()
247     {
248         $bag = new ResponseHeaderBag();
249
250         $bag->getCookies('invalid_argument');
251     }
252
253     /**
254      * @expectedException \InvalidArgumentException
255      */
256     public function testMakeDispositionInvalidDisposition()
257     {
258         $headers = new ResponseHeaderBag();
259
260         $headers->makeDisposition('invalid', 'foo.html');
261     }
262
263     /**
264      * @dataProvider provideMakeDisposition
265      */
266     public function testMakeDisposition($disposition, $filename, $filenameFallback, $expected)
267     {
268         $headers = new ResponseHeaderBag();
269
270         $this->assertEquals($expected, $headers->makeDisposition($disposition, $filename, $filenameFallback));
271     }
272
273     public function testToStringDoesntMessUpHeaders()
274     {
275         $headers = new ResponseHeaderBag();
276
277         $headers->set('Location', 'http://www.symfony.com');
278         $headers->set('Content-type', 'text/html');
279
280         (string) $headers;
281
282         $allHeaders = $headers->allPreserveCase();
283         $this->assertEquals(array('http://www.symfony.com'), $allHeaders['Location']);
284         $this->assertEquals(array('text/html'), $allHeaders['Content-type']);
285     }
286
287     public function provideMakeDisposition()
288     {
289         return array(
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'),
296         );
297     }
298
299     /**
300      * @dataProvider provideMakeDispositionFail
301      * @expectedException \InvalidArgumentException
302      */
303     public function testMakeDispositionFail($disposition, $filename)
304     {
305         $headers = new ResponseHeaderBag();
306
307         $headers->makeDisposition($disposition, $filename);
308     }
309
310     public function provideMakeDispositionFail()
311     {
312         return array(
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'),
319         );
320     }
321
322     public function testDateHeaderAddedOnCreation()
323     {
324         $now = time();
325
326         $bag = new ResponseHeaderBag();
327         $this->assertTrue($bag->has('Date'));
328
329         $this->assertEquals($now, $bag->getDate('Date')->getTimestamp());
330     }
331
332     public function testDateHeaderCanBeSetOnCreation()
333     {
334         $someDate = 'Thu, 23 Mar 2017 09:15:12 GMT';
335         $bag = new ResponseHeaderBag(array('Date' => $someDate));
336
337         $this->assertEquals($someDate, $bag->get('Date'));
338     }
339
340     public function testDateHeaderWillBeRecreatedWhenRemoved()
341     {
342         $someDate = 'Thu, 23 Mar 2017 09:15:12 GMT';
343         $bag = new ResponseHeaderBag(array('Date' => $someDate));
344         $bag->remove('Date');
345
346         // a (new) Date header is still present
347         $this->assertTrue($bag->has('Date'));
348         $this->assertNotEquals($someDate, $bag->get('Date'));
349     }
350
351     public function testDateHeaderWillBeRecreatedWhenHeadersAreReplaced()
352     {
353         $bag = new ResponseHeaderBag();
354         $bag->replace(array());
355
356         $this->assertTrue($bag->has('Date'));
357     }
358
359     private function assertSetCookieHeader($expected, ResponseHeaderBag $actual)
360     {
361         $this->assertRegExp('#^Set-Cookie:\s+'.preg_quote($expected, '#').'$#m', str_replace("\r\n", "\n", (string) $actual));
362     }
363 }