4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\HeaderBag;
17 class HeaderBagTest extends TestCase
19 public function testConstructor()
21 $bag = new HeaderBag(array('foo' => 'bar'));
22 $this->assertTrue($bag->has('foo'));
25 public function testToStringNull()
27 $bag = new HeaderBag();
28 $this->assertEquals('', $bag->__toString());
31 public function testToStringNotNull()
33 $bag = new HeaderBag(array('foo' => 'bar'));
34 $this->assertEquals("Foo: bar\r\n", $bag->__toString());
37 public function testKeys()
39 $bag = new HeaderBag(array('foo' => 'bar'));
41 $this->assertEquals('foo', $keys[0]);
44 public function testGetDate()
46 $bag = new HeaderBag(array('foo' => 'Tue, 4 Sep 2012 20:00:00 +0200'));
47 $headerDate = $bag->getDate('foo');
48 $this->assertInstanceOf('DateTime', $headerDate);
52 * @expectedException \RuntimeException
54 public function testGetDateException()
56 $bag = new HeaderBag(array('foo' => 'Tue'));
57 $headerDate = $bag->getDate('foo');
60 public function testGetCacheControlHeader()
62 $bag = new HeaderBag();
63 $bag->addCacheControlDirective('public', '#a');
64 $this->assertTrue($bag->hasCacheControlDirective('public'));
65 $this->assertEquals('#a', $bag->getCacheControlDirective('public'));
68 public function testAll()
70 $bag = new HeaderBag(array('foo' => 'bar'));
71 $this->assertEquals(array('foo' => array('bar')), $bag->all(), '->all() gets all the input');
73 $bag = new HeaderBag(array('FOO' => 'BAR'));
74 $this->assertEquals(array('foo' => array('BAR')), $bag->all(), '->all() gets all the input key are lower case');
77 public function testReplace()
79 $bag = new HeaderBag(array('foo' => 'bar'));
81 $bag->replace(array('NOPE' => 'BAR'));
82 $this->assertEquals(array('nope' => array('BAR')), $bag->all(), '->replace() replaces the input with the argument');
83 $this->assertFalse($bag->has('foo'), '->replace() overrides previously set the input');
86 public function testGet()
88 $bag = new HeaderBag(array('foo' => 'bar', 'fuzz' => 'bizz'));
89 $this->assertEquals('bar', $bag->get('foo'), '->get return current value');
90 $this->assertEquals('bar', $bag->get('FoO'), '->get key in case insensitive');
91 $this->assertEquals(array('bar'), $bag->get('foo', 'nope', false), '->get return the value as array');
94 $this->assertNull($bag->get('none'), '->get unknown values returns null');
95 $this->assertEquals('default', $bag->get('none', 'default'), '->get unknown values returns default');
96 $this->assertEquals(array('default'), $bag->get('none', 'default', false), '->get unknown values returns default as array');
98 $bag->set('foo', 'bor', false);
99 $this->assertEquals('bar', $bag->get('foo'), '->get return first value');
100 $this->assertEquals(array('bar', 'bor'), $bag->get('foo', 'nope', false), '->get return all values as array');
103 public function testSetAssociativeArray()
105 $bag = new HeaderBag();
106 $bag->set('foo', array('bad-assoc-index' => 'value'));
107 $this->assertSame('value', $bag->get('foo'));
108 $this->assertEquals(array('value'), $bag->get('foo', 'nope', false), 'assoc indices of multi-valued headers are ignored');
111 public function testContains()
113 $bag = new HeaderBag(array('foo' => 'bar', 'fuzz' => 'bizz'));
114 $this->assertTrue($bag->contains('foo', 'bar'), '->contains first value');
115 $this->assertTrue($bag->contains('fuzz', 'bizz'), '->contains second value');
116 $this->assertFalse($bag->contains('nope', 'nope'), '->contains unknown value');
117 $this->assertFalse($bag->contains('foo', 'nope'), '->contains unknown value');
120 $bag->set('foo', 'bor', false);
121 $this->assertTrue($bag->contains('foo', 'bar'), '->contains first value');
122 $this->assertTrue($bag->contains('foo', 'bor'), '->contains second value');
123 $this->assertFalse($bag->contains('foo', 'nope'), '->contains unknown value');
126 public function testCacheControlDirectiveAccessors()
128 $bag = new HeaderBag();
129 $bag->addCacheControlDirective('public');
131 $this->assertTrue($bag->hasCacheControlDirective('public'));
132 $this->assertTrue($bag->getCacheControlDirective('public'));
133 $this->assertEquals('public', $bag->get('cache-control'));
135 $bag->addCacheControlDirective('max-age', 10);
136 $this->assertTrue($bag->hasCacheControlDirective('max-age'));
137 $this->assertEquals(10, $bag->getCacheControlDirective('max-age'));
138 $this->assertEquals('max-age=10, public', $bag->get('cache-control'));
140 $bag->removeCacheControlDirective('max-age');
141 $this->assertFalse($bag->hasCacheControlDirective('max-age'));
144 public function testCacheControlDirectiveParsing()
146 $bag = new HeaderBag(array('cache-control' => 'public, max-age=10'));
147 $this->assertTrue($bag->hasCacheControlDirective('public'));
148 $this->assertTrue($bag->getCacheControlDirective('public'));
150 $this->assertTrue($bag->hasCacheControlDirective('max-age'));
151 $this->assertEquals(10, $bag->getCacheControlDirective('max-age'));
153 $bag->addCacheControlDirective('s-maxage', 100);
154 $this->assertEquals('max-age=10, public, s-maxage=100', $bag->get('cache-control'));
157 public function testCacheControlDirectiveParsingQuotedZero()
159 $bag = new HeaderBag(array('cache-control' => 'max-age="0"'));
160 $this->assertTrue($bag->hasCacheControlDirective('max-age'));
161 $this->assertEquals(0, $bag->getCacheControlDirective('max-age'));
164 public function testCacheControlDirectiveOverrideWithReplace()
166 $bag = new HeaderBag(array('cache-control' => 'private, max-age=100'));
167 $bag->replace(array('cache-control' => 'public, max-age=10'));
168 $this->assertTrue($bag->hasCacheControlDirective('public'));
169 $this->assertTrue($bag->getCacheControlDirective('public'));
171 $this->assertTrue($bag->hasCacheControlDirective('max-age'));
172 $this->assertEquals(10, $bag->getCacheControlDirective('max-age'));
175 public function testGetIterator()
177 $headers = array('foo' => 'bar', 'hello' => 'world', 'third' => 'charm');
178 $headerBag = new HeaderBag($headers);
181 foreach ($headerBag as $key => $val) {
183 $this->assertEquals(array($headers[$key]), $val);
186 $this->assertEquals(count($headers), $i);
189 public function testCount()
191 $headers = array('foo' => 'bar', 'HELLO' => 'WORLD');
192 $headerBag = new HeaderBag($headers);
194 $this->assertEquals(count($headers), count($headerBag));