HeaderBagTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Tests;
  11. use Symfony\Component\HttpFoundation\HeaderBag;
  12. class HeaderBagTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testConstructor()
  15. {
  16. $bag = new HeaderBag(array('foo' => 'bar'));
  17. $this->assertTrue($bag->has('foo'));
  18. }
  19. public function testToStringNull()
  20. {
  21. $bag = new HeaderBag();
  22. $this->assertEquals('', $bag->__toString());
  23. }
  24. public function testToStringNotNull()
  25. {
  26. $bag = new HeaderBag(array('foo' => 'bar'));
  27. $this->assertEquals("Foo: bar\r\n", $bag->__toString());
  28. }
  29. public function testKeys()
  30. {
  31. $bag = new HeaderBag(array('foo' => 'bar'));
  32. $keys = $bag->keys();
  33. $this->assertEquals('foo', $keys[0]);
  34. }
  35. public function testGetDate()
  36. {
  37. $bag = new HeaderBag(array('foo' => 'Tue, 4 Sep 2012 20:00:00 +0200'));
  38. $headerDate = $bag->getDate('foo');
  39. $this->assertInstanceOf('DateTime', $headerDate);
  40. }
  41. /**
  42. * @expectedException \RuntimeException
  43. */
  44. public function testGetDateException()
  45. {
  46. $bag = new HeaderBag(array('foo' => 'Tue'));
  47. $headerDate = $bag->getDate('foo');
  48. }
  49. public function testGetCacheControlHeader()
  50. {
  51. $bag = new HeaderBag();
  52. $bag->addCacheControlDirective('public', '#a');
  53. $this->assertTrue($bag->hasCacheControlDirective('public'));
  54. $this->assertEquals('#a', $bag->getCacheControlDirective('public'));
  55. }
  56. public function testAll()
  57. {
  58. $bag = new HeaderBag(array('foo' => 'bar'));
  59. $this->assertEquals(array('foo' => array('bar')), $bag->all(), '->all() gets all the input');
  60. $bag = new HeaderBag(array('FOO' => 'BAR'));
  61. $this->assertEquals(array('foo' => array('BAR')), $bag->all(), '->all() gets all the input key are lower case');
  62. }
  63. public function testReplace()
  64. {
  65. $bag = new HeaderBag(array('foo' => 'bar'));
  66. $bag->replace(array('NOPE' => 'BAR'));
  67. $this->assertEquals(array('nope' => array('BAR')), $bag->all(), '->replace() replaces the input with the argument');
  68. $this->assertFalse($bag->has('foo'), '->replace() overrides previously set the input');
  69. }
  70. public function testGet()
  71. {
  72. $bag = new HeaderBag(array('foo' => 'bar', 'fuzz' => 'bizz'));
  73. $this->assertEquals('bar', $bag->get('foo'), '->get return current value');
  74. $this->assertEquals('bar', $bag->get('FoO'), '->get key in case insensitive');
  75. $this->assertEquals(array('bar'), $bag->get('foo', 'nope', false), '->get return the value as array');
  76. // defaults
  77. $this->assertNull($bag->get('none'), '->get unknown values returns null');
  78. $this->assertEquals('default', $bag->get('none', 'default'), '->get unknown values returns default');
  79. $this->assertEquals(array('default'), $bag->get('none', 'default', false), '->get unknown values returns default as array');
  80. $bag->set('foo', 'bor', false);
  81. $this->assertEquals('bar', $bag->get('foo'), '->get return first value');
  82. $this->assertEquals(array('bar', 'bor'), $bag->get('foo', 'nope', false), '->get return all values as array');
  83. }
  84. public function testSetAssociativeArray()
  85. {
  86. $bag = new HeaderBag();
  87. $bag->set('foo', array('bad-assoc-index' => 'value'));
  88. $this->assertSame('value', $bag->get('foo'));
  89. $this->assertEquals(array('value'), $bag->get('foo', 'nope', false), 'assoc indices of multi-valued headers are ignored');
  90. }
  91. public function testContains()
  92. {
  93. $bag = new HeaderBag(array('foo' => 'bar', 'fuzz' => 'bizz'));
  94. $this->assertTrue($bag->contains('foo', 'bar'), '->contains first value');
  95. $this->assertTrue($bag->contains('fuzz', 'bizz'), '->contains second value');
  96. $this->assertFalse($bag->contains('nope', 'nope'), '->contains unknown value');
  97. $this->assertFalse($bag->contains('foo', 'nope'), '->contains unknown value');
  98. // Multiple values
  99. $bag->set('foo', 'bor', false);
  100. $this->assertTrue($bag->contains('foo', 'bar'), '->contains first value');
  101. $this->assertTrue($bag->contains('foo', 'bor'), '->contains second value');
  102. $this->assertFalse($bag->contains('foo', 'nope'), '->contains unknown value');
  103. }
  104. public function testCacheControlDirectiveAccessors()
  105. {
  106. $bag = new HeaderBag();
  107. $bag->addCacheControlDirective('public');
  108. $this->assertTrue($bag->hasCacheControlDirective('public'));
  109. $this->assertTrue($bag->getCacheControlDirective('public'));
  110. $this->assertEquals('public', $bag->get('cache-control'));
  111. $bag->addCacheControlDirective('max-age', 10);
  112. $this->assertTrue($bag->hasCacheControlDirective('max-age'));
  113. $this->assertEquals(10, $bag->getCacheControlDirective('max-age'));
  114. $this->assertEquals('max-age=10, public', $bag->get('cache-control'));
  115. $bag->removeCacheControlDirective('max-age');
  116. $this->assertFalse($bag->hasCacheControlDirective('max-age'));
  117. }
  118. public function testCacheControlDirectiveParsing()
  119. {
  120. $bag = new HeaderBag(array('cache-control' => 'public, max-age=10'));
  121. $this->assertTrue($bag->hasCacheControlDirective('public'));
  122. $this->assertTrue($bag->getCacheControlDirective('public'));
  123. $this->assertTrue($bag->hasCacheControlDirective('max-age'));
  124. $this->assertEquals(10, $bag->getCacheControlDirective('max-age'));
  125. $bag->addCacheControlDirective('s-maxage', 100);
  126. $this->assertEquals('max-age=10, public, s-maxage=100', $bag->get('cache-control'));
  127. }
  128. public function testCacheControlDirectiveParsingQuotedZero()
  129. {
  130. $bag = new HeaderBag(array('cache-control' => 'max-age="0"'));
  131. $this->assertTrue($bag->hasCacheControlDirective('max-age'));
  132. $this->assertEquals(0, $bag->getCacheControlDirective('max-age'));
  133. }
  134. public function testCacheControlDirectiveOverrideWithReplace()
  135. {
  136. $bag = new HeaderBag(array('cache-control' => 'private, max-age=100'));
  137. $bag->replace(array('cache-control' => 'public, max-age=10'));
  138. $this->assertTrue($bag->hasCacheControlDirective('public'));
  139. $this->assertTrue($bag->getCacheControlDirective('public'));
  140. $this->assertTrue($bag->hasCacheControlDirective('max-age'));
  141. $this->assertEquals(10, $bag->getCacheControlDirective('max-age'));
  142. }
  143. public function testGetIterator()
  144. {
  145. $headers = array('foo' => 'bar', 'hello' => 'world', 'third' => 'charm');
  146. $headerBag = new HeaderBag($headers);
  147. $i = 0;
  148. foreach ($headerBag as $key => $val) {
  149. ++$i;
  150. $this->assertEquals(array($headers[$key]), $val);
  151. }
  152. $this->assertEquals(count($headers), $i);
  153. }
  154. public function testCount()
  155. {
  156. $headers = array('foo' => 'bar', 'HELLO' => 'WORLD');
  157. $headerBag = new HeaderBag($headers);
  158. $this->assertEquals(count($headers), count($headerBag));
  159. }
  160. }