Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

HeaderBagTest.php 7.6 KiB

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