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