1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\HttpFoundation\Tests;
13:
14: use Symfony\Component\HttpFoundation\ResponseHeaderBag;
15: use Symfony\Component\HttpFoundation\Cookie;
16:
17: class ResponseHeaderBagTest extends \PHPUnit_Framework_TestCase
18: {
19: 20: 21: 22:
23: public function testAllPreserveCase($headers, $expected)
24: {
25: $bag = new ResponseHeaderBag($headers);
26:
27: $this->assertEquals($expected, $bag->allPreserveCase(), '->allPreserveCase() gets all input keys in original case');
28: }
29:
30: public function provideAllPreserveCase()
31: {
32: return array(
33: array(
34: array('fOo' => 'BAR'),
35: array('fOo' => array('BAR'), 'Cache-Control' => array('no-cache')),
36: ),
37: array(
38: array('ETag' => 'xyzzy'),
39: array('ETag' => array('xyzzy'), 'Cache-Control' => array('private, must-revalidate')),
40: ),
41: array(
42: array('Content-MD5' => 'Q2hlY2sgSW50ZWdyaXR5IQ=='),
43: array('Content-MD5' => array('Q2hlY2sgSW50ZWdyaXR5IQ=='), 'Cache-Control' => array('no-cache')),
44: ),
45: array(
46: array('P3P' => 'CP="CAO PSA OUR"'),
47: array('P3P' => array('CP="CAO PSA OUR"'), 'Cache-Control' => array('no-cache')),
48: ),
49: array(
50: array('WWW-Authenticate' => 'Basic realm="WallyWorld"'),
51: array('WWW-Authenticate' => array('Basic realm="WallyWorld"'), 'Cache-Control' => array('no-cache')),
52: ),
53: array(
54: array('X-UA-Compatible' => 'IE=edge,chrome=1'),
55: array('X-UA-Compatible' => array('IE=edge,chrome=1'), 'Cache-Control' => array('no-cache')),
56: ),
57: array(
58: array('X-XSS-Protection' => '1; mode=block'),
59: array('X-XSS-Protection' => array('1; mode=block'), 'Cache-Control' => array('no-cache')),
60: ),
61: );
62: }
63:
64: public function testCacheControlHeader()
65: {
66: $bag = new ResponseHeaderBag(array());
67: $this->assertEquals('no-cache', $bag->get('Cache-Control'));
68: $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
69:
70: $bag = new ResponseHeaderBag(array('Cache-Control' => 'public'));
71: $this->assertEquals('public', $bag->get('Cache-Control'));
72: $this->assertTrue($bag->hasCacheControlDirective('public'));
73:
74: $bag = new ResponseHeaderBag(array('ETag' => 'abcde'));
75: $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
76: $this->assertTrue($bag->hasCacheControlDirective('private'));
77: $this->assertTrue($bag->hasCacheControlDirective('must-revalidate'));
78: $this->assertFalse($bag->hasCacheControlDirective('max-age'));
79:
80: $bag = new ResponseHeaderBag(array('Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT'));
81: $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
82:
83: $bag = new ResponseHeaderBag(array(
84: 'Expires' => 'Wed, 16 Feb 2011 14:17:43 GMT',
85: 'Cache-Control' => 'max-age=3600',
86: ));
87: $this->assertEquals('max-age=3600, private', $bag->get('Cache-Control'));
88:
89: $bag = new ResponseHeaderBag(array('Last-Modified' => 'abcde'));
90: $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
91:
92: $bag = new ResponseHeaderBag(array('Etag' => 'abcde', 'Last-Modified' => 'abcde'));
93: $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
94:
95: $bag = new ResponseHeaderBag(array('cache-control' => 'max-age=100'));
96: $this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
97:
98: $bag = new ResponseHeaderBag(array('cache-control' => 's-maxage=100'));
99: $this->assertEquals('s-maxage=100', $bag->get('Cache-Control'));
100:
101: $bag = new ResponseHeaderBag(array('cache-control' => 'private, max-age=100'));
102: $this->assertEquals('max-age=100, private', $bag->get('Cache-Control'));
103:
104: $bag = new ResponseHeaderBag(array('cache-control' => 'public, max-age=100'));
105: $this->assertEquals('max-age=100, public', $bag->get('Cache-Control'));
106:
107: $bag = new ResponseHeaderBag();
108: $bag->set('Last-Modified', 'abcde');
109: $this->assertEquals('private, must-revalidate', $bag->get('Cache-Control'));
110: }
111:
112: public function testToStringIncludesCookieHeaders()
113: {
114: $bag = new ResponseHeaderBag(array());
115: $bag->setCookie(new Cookie('foo', 'bar'));
116:
117: $this->assertContains("Set-Cookie: foo=bar; path=/; httponly", explode("\r\n", $bag->__toString()));
118:
119: $bag->clearCookie('foo');
120:
121: $this->assertContains("Set-Cookie: foo=deleted; expires=".gmdate("D, d-M-Y H:i:s T", time() - 31536001)."; path=/; httponly", explode("\r\n", $bag->__toString()));
122: }
123:
124: public function testClearCookieSecureNotHttpOnly()
125: {
126: $bag = new ResponseHeaderBag(array());
127:
128: $bag->clearCookie('foo', '/', null, true, false);
129:
130: $this->assertContains("Set-Cookie: foo=deleted; expires=".gmdate("D, d-M-Y H:i:s T", time() - 31536001)."; path=/; secure", explode("\r\n", $bag->__toString()));
131: }
132:
133: public function testReplace()
134: {
135: $bag = new ResponseHeaderBag(array());
136: $this->assertEquals('no-cache', $bag->get('Cache-Control'));
137: $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
138:
139: $bag->replace(array('Cache-Control' => 'public'));
140: $this->assertEquals('public', $bag->get('Cache-Control'));
141: $this->assertTrue($bag->hasCacheControlDirective('public'));
142: }
143:
144: public function testReplaceWithRemove()
145: {
146: $bag = new ResponseHeaderBag(array());
147: $this->assertEquals('no-cache', $bag->get('Cache-Control'));
148: $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
149:
150: $bag->remove('Cache-Control');
151: $bag->replace(array());
152: $this->assertEquals('no-cache', $bag->get('Cache-Control'));
153: $this->assertTrue($bag->hasCacheControlDirective('no-cache'));
154: }
155:
156: public function testCookiesWithSameNames()
157: {
158: $bag = new ResponseHeaderBag();
159: $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
160: $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'foo.bar'));
161: $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/bar', 'bar.foo'));
162: $bag->setCookie(new Cookie('foo', 'bar'));
163:
164: $this->assertCount(4, $bag->getCookies());
165:
166: $headers = explode("\r\n", $bag->__toString());
167: $this->assertContains("Set-Cookie: foo=bar; path=/path/foo; domain=foo.bar; httponly", $headers);
168: $this->assertContains("Set-Cookie: foo=bar; path=/path/foo; domain=foo.bar; httponly", $headers);
169: $this->assertContains("Set-Cookie: foo=bar; path=/path/bar; domain=bar.foo; httponly", $headers);
170: $this->assertContains("Set-Cookie: foo=bar; path=/; httponly", $headers);
171:
172: $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
173: $this->assertTrue(isset($cookies['foo.bar']['/path/foo']['foo']));
174: $this->assertTrue(isset($cookies['foo.bar']['/path/bar']['foo']));
175: $this->assertTrue(isset($cookies['bar.foo']['/path/bar']['foo']));
176: $this->assertTrue(isset($cookies['']['/']['foo']));
177: }
178:
179: public function testRemoveCookie()
180: {
181: $bag = new ResponseHeaderBag();
182: $bag->setCookie(new Cookie('foo', 'bar', 0, '/path/foo', 'foo.bar'));
183: $bag->setCookie(new Cookie('bar', 'foo', 0, '/path/bar', 'foo.bar'));
184:
185: $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
186: $this->assertTrue(isset($cookies['foo.bar']['/path/foo']));
187:
188: $bag->removeCookie('foo', '/path/foo', 'foo.bar');
189:
190: $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
191: $this->assertFalse(isset($cookies['foo.bar']['/path/foo']));
192:
193: $bag->removeCookie('bar', '/path/bar', 'foo.bar');
194:
195: $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
196: $this->assertFalse(isset($cookies['foo.bar']));
197: }
198:
199: public function testRemoveCookieWithNullRemove()
200: {
201: $bag = new ResponseHeaderBag();
202: $bag->setCookie(new Cookie('foo', 'bar', 0));
203: $bag->setCookie(new Cookie('bar', 'foo', 0));
204:
205: $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
206: $this->assertTrue(isset($cookies['']['/']));
207:
208: $bag->removeCookie('foo', null);
209: $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
210: $this->assertFalse(isset($cookies['']['/']['foo']));
211:
212: $bag->removeCookie('bar', null);
213: $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
214: $this->assertFalse(isset($cookies['']['/']['bar']));
215: }
216:
217: 218: 219:
220: public function testGetCookiesWithInvalidArgument()
221: {
222: $bag = new ResponseHeaderBag();
223:
224: $cookies = $bag->getCookies('invalid_argument');
225: }
226:
227: 228: 229:
230: public function testMakeDispositionInvalidDisposition()
231: {
232: $headers = new ResponseHeaderBag();
233:
234: $headers->makeDisposition('invalid', 'foo.html');
235: }
236:
237: 238: 239:
240: public function testMakeDisposition($disposition, $filename, $filenameFallback, $expected)
241: {
242: $headers = new ResponseHeaderBag();
243:
244: $this->assertEquals($expected, $headers->makeDisposition($disposition, $filename, $filenameFallback));
245: }
246:
247: public function testToStringDoesntMessUpHeaders()
248: {
249: $headers = new ResponseHeaderBag();
250:
251: $headers->set('Location', 'http://www.symfony.com');
252: $headers->set('Content-type', 'text/html');
253:
254: (string) $headers;
255:
256: $allHeaders = $headers->allPreserveCase();
257: $this->assertEquals(array('http://www.symfony.com'), $allHeaders['Location']);
258: $this->assertEquals(array('text/html'), $allHeaders['Content-type']);
259: }
260:
261: public function provideMakeDisposition()
262: {
263: return array(
264: array('attachment', 'foo.html', 'foo.html', 'attachment; filename="foo.html"'),
265: array('attachment', 'foo.html', '', 'attachment; filename="foo.html"'),
266: array('attachment', 'foo bar.html', '', 'attachment; filename="foo bar.html"'),
267: array('attachment', 'foo "bar".html', '', 'attachment; filename="foo \\"bar\\".html"'),
268: array('attachment', 'foo%20bar.html', 'foo bar.html', 'attachment; filename="foo bar.html"; filename*=utf-8\'\'foo%2520bar.html'),
269: array('attachment', 'föö.html', 'foo.html', 'attachment; filename="foo.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html'),
270: );
271: }
272:
273: 274: 275: 276:
277: public function testMakeDispositionFail($disposition, $filename)
278: {
279: $headers = new ResponseHeaderBag();
280:
281: $headers->makeDisposition($disposition, $filename);
282: }
283:
284: public function provideMakeDispositionFail()
285: {
286: return array(
287: array('attachment', 'foo%20bar.html'),
288: array('attachment', 'foo/bar.html'),
289: array('attachment', '/foo.html'),
290: array('attachment', 'foo\bar.html'),
291: array('attachment', '\foo.html'),
292: array('attachment', 'föö.html'),
293: );
294: }
295: }
296: