HeaderBagTest.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\Tests\Component\HttpFoundation;
  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. /**
  23. * @covers Symfony\Component\HttpFoundation\HeaderBag::all
  24. */
  25. public function testAll()
  26. {
  27. $bag = new HeaderBag(array('foo' => 'bar'));
  28. $this->assertEquals(array('foo' => array('bar')), $bag->all(), '->all() gets all the input');
  29. $bag = new HeaderBag(array('FOO' => 'BAR'));
  30. $this->assertEquals(array('foo' => array('BAR')), $bag->all(), '->all() gets all the input key are lower case');
  31. }
  32. /**
  33. * @covers Symfony\Component\HttpFoundation\HeaderBag::replace
  34. */
  35. public function testReplace()
  36. {
  37. $bag = new HeaderBag(array('foo' => 'bar'));
  38. $bag->replace(array('NOPE' => 'BAR'));
  39. $this->assertEquals(array('nope' => array('BAR')), $bag->all(), '->replace() replaces the input with the argument');
  40. $this->assertFalse($bag->has('foo'), '->replace() overrides previously set the input');
  41. }
  42. /**
  43. * @covers Symfony\Component\HttpFoundation\HeaderBag::get
  44. */
  45. public function testGet()
  46. {
  47. $bag = new HeaderBag(array('foo' => 'bar', 'fuzz' => 'bizz'));
  48. $this->assertEquals( 'bar', $bag->get('foo'), '->get return current value');
  49. $this->assertEquals( 'bar', $bag->get('FoO'), '->get key in case insensitive');
  50. $this->assertEquals( array('bar'), $bag->get('foo', 'nope', false), '->get return the value as array');
  51. // defaults
  52. $this->assertNull($bag->get('none'), '->get unknown values returns null');
  53. $this->assertEquals( 'default', $bag->get('none', 'default'), '->get unknown values returns default');
  54. $this->assertEquals( array('default'), $bag->get('none', 'default', false), '->get unknown values returns default as array');
  55. $bag->set('foo', 'bor', false);
  56. $this->assertEquals( 'bar', $bag->get('foo'), '->get return first value');
  57. $this->assertEquals( array('bar', 'bor'), $bag->get('foo', 'nope', false), '->get return all values as array');
  58. }
  59. /**
  60. * @covers Symfony\Component\HttpFoundation\HeaderBag::contains
  61. */
  62. public function testContains()
  63. {
  64. $bag = new HeaderBag(array('foo' => 'bar', 'fuzz' => 'bizz'));
  65. $this->assertTrue( $bag->contains('foo', 'bar'), '->contains first value');
  66. $this->assertTrue( $bag->contains('fuzz', 'bizz'), '->contains second value');
  67. $this->assertFalse( $bag->contains('nope', 'nope'), '->contains unknown value');
  68. $this->assertFalse( $bag->contains('foo', 'nope'), '->contains unknown value');
  69. // Multiple values
  70. $bag->set('foo', 'bor', false);
  71. $this->assertTrue( $bag->contains('foo', 'bar'), '->contains first value');
  72. $this->assertTrue( $bag->contains('foo', 'bor'), '->contains second value');
  73. $this->assertFalse( $bag->contains('foo', 'nope'), '->contains unknown value');
  74. }
  75. public function testCacheControlDirectiveAccessors()
  76. {
  77. $bag = new HeaderBag();
  78. $bag->addCacheControlDirective('public');
  79. $this->assertTrue($bag->hasCacheControlDirective('public'));
  80. $this->assertEquals(true, $bag->getCacheControlDirective('public'));
  81. $this->assertEquals('public', $bag->get('cache-control'));
  82. $bag->addCacheControlDirective('max-age', 10);
  83. $this->assertTrue($bag->hasCacheControlDirective('max-age'));
  84. $this->assertEquals(10, $bag->getCacheControlDirective('max-age'));
  85. $this->assertEquals('max-age=10, public', $bag->get('cache-control'));
  86. $bag->removeCacheControlDirective('max-age');
  87. $this->assertFalse($bag->hasCacheControlDirective('max-age'));
  88. }
  89. public function testCacheControlDirectiveParsing()
  90. {
  91. $bag = new HeaderBag(array('cache-control' => 'public, max-age=10'));
  92. $this->assertTrue($bag->hasCacheControlDirective('public'));
  93. $this->assertEquals(true, $bag->getCacheControlDirective('public'));
  94. $this->assertTrue($bag->hasCacheControlDirective('max-age'));
  95. $this->assertEquals(10, $bag->getCacheControlDirective('max-age'));
  96. $bag->addCacheControlDirective('s-maxage', 100);
  97. $this->assertEquals('max-age=10, public, s-maxage=100', $bag->get('cache-control'));
  98. }
  99. public function testCacheControlDirectiveOverrideWithReplace()
  100. {
  101. $bag = new HeaderBag(array('cache-control' => 'private, max-age=100'));
  102. $bag->replace(array('cache-control' => 'public, max-age=10'));
  103. $this->assertTrue($bag->hasCacheControlDirective('public'));
  104. $this->assertEquals(true, $bag->getCacheControlDirective('public'));
  105. $this->assertTrue($bag->hasCacheControlDirective('max-age'));
  106. $this->assertEquals(10, $bag->getCacheControlDirective('max-age'));
  107. }
  108. }