HeaderBagTest.php 7.3 KB

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