Open Monograph Press  3.3.0
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 
13 
14 use PHPUnit\Framework\TestCase;
17 
21 class ResponseHeaderBagTest extends TestCase
22 {
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 
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).'; max-age=-31536001; path=/; httponly', $bag);
133  }
134 
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).'; max-age=-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  $this->assertEquals('foo=bar; path=/path/foo; domain=foo.bar; httponly', $bag->get('set-cookie'));
177  $this->assertEquals(array(
178  'foo=bar; path=/path/foo; domain=foo.bar; httponly',
179  'foo=bar; path=/path/bar; domain=foo.bar; httponly',
180  'foo=bar; path=/path/bar; domain=bar.foo; httponly',
181  'foo=bar; path=/; httponly',
182  ), $bag->get('set-cookie', null, false));
183 
184  $this->assertSetCookieHeader('foo=bar; path=/path/foo; domain=foo.bar; httponly', $bag);
185  $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=foo.bar; httponly', $bag);
186  $this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=bar.foo; httponly', $bag);
187  $this->assertSetCookieHeader('foo=bar; path=/; httponly', $bag);
188 
189  $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
190 
191  $this->assertTrue(isset($cookies['foo.bar']['/path/foo']['foo']));
192  $this->assertTrue(isset($cookies['foo.bar']['/path/bar']['foo']));
193  $this->assertTrue(isset($cookies['bar.foo']['/path/bar']['foo']));
194  $this->assertTrue(isset($cookies['']['/']['foo']));
195  }
196 
197  public function testRemoveCookie()
198  {
199  $bag = new ResponseHeaderBag();
200  $this->assertFalse($bag->has('set-cookie'));
201 
202  $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
203  $bag->setCookie(new Cookie('bar', 'foo', 0, '/path/bar', 'foo.bar'));
204  $this->assertTrue($bag->has('set-cookie'));
205 
206  $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
207  $this->assertTrue(isset($cookies['foo.bar']['/path/foo']));
208 
209  $bag->removeCookie('foo', '/path/foo', 'foo.bar');
210  $this->assertTrue($bag->has('set-cookie'));
211 
212  $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
213  $this->assertFalse(isset($cookies['foo.bar']['/path/foo']));
214 
215  $bag->removeCookie('bar', '/path/bar', 'foo.bar');
216  $this->assertFalse($bag->has('set-cookie'));
217 
218  $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
219  $this->assertFalse(isset($cookies['foo.bar']));
220  }
221 
223  {
224  $bag = new ResponseHeaderBag();
225  $bag->setCookie(new Cookie('foo', 'bar', 0));
226  $bag->setCookie(new Cookie('bar', 'foo', 0));
227 
228  $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
229  $this->assertTrue(isset($cookies['']['/']));
230 
231  $bag->removeCookie('foo', null);
232  $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
233  $this->assertFalse(isset($cookies['']['/']['foo']));
234 
235  $bag->removeCookie('bar', null);
236  $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
237  $this->assertFalse(isset($cookies['']['/']['bar']));
238  }
239 
240  public function testSetCookieHeader()
241  {
242  $bag = new ResponseHeaderBag();
243  $bag->set('set-cookie', 'foo=bar');
244  $this->assertEquals(array(new Cookie('foo', 'bar', 0, '/', null, false, false, true)), $bag->getCookies());
245 
246  $bag->set('set-cookie', 'foo2=bar2', false);
247  $this->assertEquals(array(
248  new Cookie('foo', 'bar', 0, '/', null, false, false, true),
249  new Cookie('foo2', 'bar2', 0, '/', null, false, false, true),
250  ), $bag->getCookies());
251 
252  $bag->remove('set-cookie');
253  $this->assertEquals(array(), $bag->getCookies());
254  }
255 
260  {
261  $bag = new ResponseHeaderBag();
262 
263  $bag->getCookies('invalid_argument');
264  }
265 
270  {
271  $headers = new ResponseHeaderBag();
272 
273  $headers->makeDisposition('invalid', 'foo.html');
274  }
275 
279  public function testMakeDisposition($disposition, $filename, $filenameFallback, $expected)
280  {
281  $headers = new ResponseHeaderBag();
282 
283  $this->assertEquals($expected, $headers->makeDisposition($disposition, $filename, $filenameFallback));
284  }
285 
287  {
288  $headers = new ResponseHeaderBag();
289 
290  $headers->set('Location', 'http://www.symfony.com');
291  $headers->set('Content-type', 'text/html');
292 
293  (string) $headers;
294 
295  $allHeaders = $headers->allPreserveCase();
296  $this->assertEquals(array('http://www.symfony.com'), $allHeaders['Location']);
297  $this->assertEquals(array('text/html'), $allHeaders['Content-type']);
298  }
299 
300  public function provideMakeDisposition()
301  {
302  return array(
303  array('attachment', 'foo.html', 'foo.html', 'attachment; filename="foo.html"'),
304  array('attachment', 'foo.html', '', 'attachment; filename="foo.html"'),
305  array('attachment', 'foo bar.html', '', 'attachment; filename="foo bar.html"'),
306  array('attachment', 'foo "bar".html', '', 'attachment; filename="foo \\"bar\\".html"'),
307  array('attachment', 'foo%20bar.html', 'foo bar.html', 'attachment; filename="foo bar.html"; filename*=utf-8\'\'foo%2520bar.html'),
308  array('attachment', 'föö.html', 'foo.html', 'attachment; filename="foo.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html'),
309  );
310  }
311 
316  public function testMakeDispositionFail($disposition, $filename)
317  {
318  $headers = new ResponseHeaderBag();
319 
320  $headers->makeDisposition($disposition, $filename);
321  }
322 
323  public function provideMakeDispositionFail()
324  {
325  return array(
326  array('attachment', 'foo%20bar.html'),
327  array('attachment', 'foo/bar.html'),
328  array('attachment', '/foo.html'),
329  array('attachment', 'foo\bar.html'),
330  array('attachment', '\foo.html'),
331  array('attachment', 'föö.html'),
332  );
333  }
334 
335  private function assertSetCookieHeader($expected, ResponseHeaderBag $actual)
336  {
337  $this->assertRegExp('#^Set-Cookie:\s+'.preg_quote($expected, '#').'$#m', str_replace("\r\n", "\n", (string) $actual));
338  }
339 }
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest\testRemoveCookieWithNullRemove
testRemoveCookieWithNullRemove()
Definition: ResponseHeaderBagTest.php:222
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest\testClearCookieSecureNotHttpOnly
testClearCookieSecureNotHttpOnly()
Definition: ResponseHeaderBagTest.php:135
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest\provideAllPreserveCase
provideAllPreserveCase()
Definition: ResponseHeaderBagTest.php:33
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest\testReplace
testReplace()
Definition: ResponseHeaderBagTest.php:144
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest\testMakeDisposition
testMakeDisposition($disposition, $filename, $filenameFallback, $expected)
Definition: ResponseHeaderBagTest.php:279
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest\provideMakeDisposition
provideMakeDisposition()
Definition: ResponseHeaderBagTest.php:300
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest
Definition: ResponseHeaderBagTest.php:21
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest\testToStringIncludesCookieHeaders
testToStringIncludesCookieHeaders()
Definition: ResponseHeaderBagTest.php:123
Symfony\Component\HttpFoundation\ResponseHeaderBag\COOKIES_ARRAY
const COOKIES_ARRAY
Definition: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:22
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest\testCacheControlClone
testCacheControlClone()
Definition: ResponseHeaderBagTest.php:115
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest\testRemoveCookie
testRemoveCookie()
Definition: ResponseHeaderBagTest.php:197
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest\testMakeDispositionInvalidDisposition
testMakeDispositionInvalidDisposition()
Definition: ResponseHeaderBagTest.php:269
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest\provideMakeDispositionFail
provideMakeDispositionFail()
Definition: ResponseHeaderBagTest.php:323
Symfony\Component\HttpFoundation\ResponseHeaderBag
Definition: lib/vendor/symfony/http-foundation/ResponseHeaderBag.php:19
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest\testSetCookieHeader
testSetCookieHeader()
Definition: ResponseHeaderBagTest.php:240
Symfony\Component\HttpFoundation\Cookie
Definition: lib/vendor/symfony/http-foundation/Cookie.php:19
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest\testReplaceWithRemove
testReplaceWithRemove()
Definition: ResponseHeaderBagTest.php:155
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest\testAllPreserveCase
testAllPreserveCase($headers, $expected)
Definition: ResponseHeaderBagTest.php:26
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest\testCookiesWithSameNames
testCookiesWithSameNames()
Definition: ResponseHeaderBagTest.php:167
Symfony\Component\HttpFoundation\Tests
Definition: AcceptHeaderItemTest.php:12
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest\testToStringDoesntMessUpHeaders
testToStringDoesntMessUpHeaders()
Definition: ResponseHeaderBagTest.php:286
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest\testMakeDispositionFail
testMakeDispositionFail($disposition, $filename)
Definition: ResponseHeaderBagTest.php:316
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest\testCacheControlHeader
testCacheControlHeader()
Definition: ResponseHeaderBagTest.php:67
Symfony\Component\HttpFoundation\Tests\ResponseHeaderBagTest\testGetCookiesWithInvalidArgument
testGetCookiesWithInvalidArgument()
Definition: ResponseHeaderBagTest.php:259