HeaderUtilsTest.php 4.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\HeaderUtils;
  13. class HeaderUtilsTest extends TestCase
  14. {
  15. public function testSplit()
  16. {
  17. $this->assertSame(array('foo=123', 'bar'), HeaderUtils::split('foo=123,bar', ','));
  18. $this->assertSame(array('foo=123', 'bar'), HeaderUtils::split('foo=123, bar', ','));
  19. $this->assertSame(array(array('foo=123', 'bar')), HeaderUtils::split('foo=123; bar', ',;'));
  20. $this->assertSame(array(array('foo=123'), array('bar')), HeaderUtils::split('foo=123, bar', ',;'));
  21. $this->assertSame(array('foo', '123, bar'), HeaderUtils::split('foo=123, bar', '='));
  22. $this->assertSame(array('foo', '123, bar'), HeaderUtils::split(' foo = 123, bar ', '='));
  23. $this->assertSame(array(array('foo', '123'), array('bar')), HeaderUtils::split('foo=123, bar', ',='));
  24. $this->assertSame(array(array(array('foo', '123')), array(array('bar'), array('foo', '456'))), HeaderUtils::split('foo=123, bar; foo=456', ',;='));
  25. $this->assertSame(array(array(array('foo', 'a,b;c=d'))), HeaderUtils::split('foo="a,b;c=d"', ',;='));
  26. $this->assertSame(array('foo', 'bar'), HeaderUtils::split('foo,,,, bar', ','));
  27. $this->assertSame(array('foo', 'bar'), HeaderUtils::split(',foo, bar,', ','));
  28. $this->assertSame(array('foo', 'bar'), HeaderUtils::split(' , foo, bar, ', ','));
  29. $this->assertSame(array('foo bar'), HeaderUtils::split('foo "bar"', ','));
  30. $this->assertSame(array('foo bar'), HeaderUtils::split('"foo" bar', ','));
  31. $this->assertSame(array('foo bar'), HeaderUtils::split('"foo" "bar"', ','));
  32. // These are not a valid header values. We test that they parse anyway,
  33. // and that both the valid and invalid parts are returned.
  34. $this->assertSame(array(), HeaderUtils::split('', ','));
  35. $this->assertSame(array(), HeaderUtils::split(',,,', ','));
  36. $this->assertSame(array('foo', 'bar', 'baz'), HeaderUtils::split('foo, "bar", "baz', ','));
  37. $this->assertSame(array('foo', 'bar, baz'), HeaderUtils::split('foo, "bar, baz', ','));
  38. $this->assertSame(array('foo', 'bar, baz\\'), HeaderUtils::split('foo, "bar, baz\\', ','));
  39. $this->assertSame(array('foo', 'bar, baz\\'), HeaderUtils::split('foo, "bar, baz\\\\', ','));
  40. }
  41. public function testCombine()
  42. {
  43. $this->assertSame(array('foo' => '123'), HeaderUtils::combine(array(array('foo', '123'))));
  44. $this->assertSame(array('foo' => true), HeaderUtils::combine(array(array('foo'))));
  45. $this->assertSame(array('foo' => true), HeaderUtils::combine(array(array('Foo'))));
  46. $this->assertSame(array('foo' => '123', 'bar' => true), HeaderUtils::combine(array(array('foo', '123'), array('bar'))));
  47. }
  48. public function testToString()
  49. {
  50. $this->assertSame('foo', HeaderUtils::toString(array('foo' => true), ','));
  51. $this->assertSame('foo; bar', HeaderUtils::toString(array('foo' => true, 'bar' => true), ';'));
  52. $this->assertSame('foo=123', HeaderUtils::toString(array('foo' => '123'), ','));
  53. $this->assertSame('foo="1 2 3"', HeaderUtils::toString(array('foo' => '1 2 3'), ','));
  54. $this->assertSame('foo="1 2 3", bar', HeaderUtils::toString(array('foo' => '1 2 3', 'bar' => true), ','));
  55. }
  56. public function testQuote()
  57. {
  58. $this->assertSame('foo', HeaderUtils::quote('foo'));
  59. $this->assertSame('az09!#$%&\'*.^_`|~-', HeaderUtils::quote('az09!#$%&\'*.^_`|~-'));
  60. $this->assertSame('"foo bar"', HeaderUtils::quote('foo bar'));
  61. $this->assertSame('"foo [bar]"', HeaderUtils::quote('foo [bar]'));
  62. $this->assertSame('"foo \"bar\""', HeaderUtils::quote('foo "bar"'));
  63. $this->assertSame('"foo \\\\ bar"', HeaderUtils::quote('foo \\ bar'));
  64. }
  65. public function testUnquote()
  66. {
  67. $this->assertEquals('foo', HeaderUtils::unquote('foo'));
  68. $this->assertEquals('az09!#$%&\'*.^_`|~-', HeaderUtils::unquote('az09!#$%&\'*.^_`|~-'));
  69. $this->assertEquals('foo bar', HeaderUtils::unquote('"foo bar"'));
  70. $this->assertEquals('foo [bar]', HeaderUtils::unquote('"foo [bar]"'));
  71. $this->assertEquals('foo "bar"', HeaderUtils::unquote('"foo \"bar\""'));
  72. $this->assertEquals('foo "bar"', HeaderUtils::unquote('"foo \"\b\a\r\""'));
  73. $this->assertEquals('foo \\ bar', HeaderUtils::unquote('"foo \\\\ bar"'));
  74. }
  75. }