HeaderUtilsTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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(['foo=123', 'bar'], HeaderUtils::split('foo=123,bar', ','));
  18. $this->assertSame(['foo=123', 'bar'], HeaderUtils::split('foo=123, bar', ','));
  19. $this->assertSame([['foo=123', 'bar']], HeaderUtils::split('foo=123; bar', ',;'));
  20. $this->assertSame([['foo=123'], ['bar']], HeaderUtils::split('foo=123, bar', ',;'));
  21. $this->assertSame(['foo', '123, bar'], HeaderUtils::split('foo=123, bar', '='));
  22. $this->assertSame(['foo', '123, bar'], HeaderUtils::split(' foo = 123, bar ', '='));
  23. $this->assertSame([['foo', '123'], ['bar']], HeaderUtils::split('foo=123, bar', ',='));
  24. $this->assertSame([[['foo', '123']], [['bar'], ['foo', '456']]], HeaderUtils::split('foo=123, bar; foo=456', ',;='));
  25. $this->assertSame([[['foo', 'a,b;c=d']]], HeaderUtils::split('foo="a,b;c=d"', ',;='));
  26. $this->assertSame(['foo', 'bar'], HeaderUtils::split('foo,,,, bar', ','));
  27. $this->assertSame(['foo', 'bar'], HeaderUtils::split(',foo, bar,', ','));
  28. $this->assertSame(['foo', 'bar'], HeaderUtils::split(' , foo, bar, ', ','));
  29. $this->assertSame(['foo bar'], HeaderUtils::split('foo "bar"', ','));
  30. $this->assertSame(['foo bar'], HeaderUtils::split('"foo" bar', ','));
  31. $this->assertSame(['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([], HeaderUtils::split('', ','));
  35. $this->assertSame([], HeaderUtils::split(',,,', ','));
  36. $this->assertSame(['foo', 'bar', 'baz'], HeaderUtils::split('foo, "bar", "baz', ','));
  37. $this->assertSame(['foo', 'bar, baz'], HeaderUtils::split('foo, "bar, baz', ','));
  38. $this->assertSame(['foo', 'bar, baz\\'], HeaderUtils::split('foo, "bar, baz\\', ','));
  39. $this->assertSame(['foo', 'bar, baz\\'], HeaderUtils::split('foo, "bar, baz\\\\', ','));
  40. }
  41. public function testCombine()
  42. {
  43. $this->assertSame(['foo' => '123'], HeaderUtils::combine([['foo', '123']]));
  44. $this->assertSame(['foo' => true], HeaderUtils::combine([['foo']]));
  45. $this->assertSame(['foo' => true], HeaderUtils::combine([['Foo']]));
  46. $this->assertSame(['foo' => '123', 'bar' => true], HeaderUtils::combine([['foo', '123'], ['bar']]));
  47. }
  48. public function testToString()
  49. {
  50. $this->assertSame('foo', HeaderUtils::toString(['foo' => true], ','));
  51. $this->assertSame('foo; bar', HeaderUtils::toString(['foo' => true, 'bar' => true], ';'));
  52. $this->assertSame('foo=123', HeaderUtils::toString(['foo' => '123'], ','));
  53. $this->assertSame('foo="1 2 3"', HeaderUtils::toString(['foo' => '1 2 3'], ','));
  54. $this->assertSame('foo="1 2 3", bar', HeaderUtils::toString(['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. /**
  76. * @expectedException \InvalidArgumentException
  77. */
  78. public function testMakeDispositionInvalidDisposition()
  79. {
  80. HeaderUtils::makeDisposition('invalid', 'foo.html');
  81. }
  82. /**
  83. * @dataProvider provideMakeDisposition
  84. */
  85. public function testMakeDisposition($disposition, $filename, $filenameFallback, $expected)
  86. {
  87. $this->assertEquals($expected, HeaderUtils::makeDisposition($disposition, $filename, $filenameFallback));
  88. }
  89. public function provideMakeDisposition()
  90. {
  91. return [
  92. ['attachment', 'foo.html', 'foo.html', 'attachment; filename=foo.html'],
  93. ['attachment', 'foo.html', '', 'attachment; filename=foo.html'],
  94. ['attachment', 'foo bar.html', '', 'attachment; filename="foo bar.html"'],
  95. ['attachment', 'foo "bar".html', '', 'attachment; filename="foo \\"bar\\".html"'],
  96. ['attachment', 'foo%20bar.html', 'foo bar.html', 'attachment; filename="foo bar.html"; filename*=utf-8\'\'foo%2520bar.html'],
  97. ['attachment', 'föö.html', 'foo.html', 'attachment; filename=foo.html; filename*=utf-8\'\'f%C3%B6%C3%B6.html'],
  98. ];
  99. }
  100. /**
  101. * @dataProvider provideMakeDispositionFail
  102. * @expectedException \InvalidArgumentException
  103. */
  104. public function testMakeDispositionFail($disposition, $filename)
  105. {
  106. HeaderUtils::makeDisposition($disposition, $filename);
  107. }
  108. public function provideMakeDispositionFail()
  109. {
  110. return [
  111. ['attachment', 'foo%20bar.html'],
  112. ['attachment', 'foo/bar.html'],
  113. ['attachment', '/foo.html'],
  114. ['attachment', 'foo\bar.html'],
  115. ['attachment', '\foo.html'],
  116. ['attachment', 'föö.html'],
  117. ];
  118. }
  119. }