No Description

HeaderBagTest.php 7.8KB

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