HeaderUtilsTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. /**
  16. * @dataProvider provideHeaderToSplit
  17. */
  18. public function testSplit(array $expected, string $header, string $separator)
  19. {
  20. $this->assertSame($expected, HeaderUtils::split($header, $separator));
  21. }
  22. public static function provideHeaderToSplit(): array
  23. {
  24. return [
  25. [['foo=123', 'bar'], 'foo=123,bar', ','],
  26. [['foo=123', 'bar'], 'foo=123, bar', ','],
  27. [[['foo=123', 'bar']], 'foo=123; bar', ',;'],
  28. [[['foo=123'], ['bar']], 'foo=123, bar', ',;'],
  29. [['foo', '123, bar'], 'foo=123, bar', '='],
  30. [['foo', '123, bar'], ' foo = 123, bar ', '='],
  31. [[['foo', '123'], ['bar']], 'foo=123, bar', ',='],
  32. [[[['foo', '123']], [['bar'], ['foo', '456']]], 'foo=123, bar;; foo=456', ',;='],
  33. [[[['foo', 'a,b;c=d']]], 'foo="a,b;c=d"', ',;='],
  34. [['foo', 'bar'], 'foo,,,, bar', ','],
  35. [['foo', 'bar'], ',foo, bar,', ','],
  36. [['foo', 'bar'], ' , foo, bar, ', ','],
  37. [['foo bar'], 'foo "bar"', ','],
  38. [['foo bar'], '"foo" bar', ','],
  39. [['foo bar'], '"foo" "bar"', ','],
  40. [[['foo_cookie', 'foo=1&bar=2&baz=3'], ['expires', 'Tue, 22-Sep-2020 06:27:09 GMT'], ['path', '/']], 'foo_cookie=foo=1&bar=2&baz=3; expires=Tue, 22-Sep-2020 06:27:09 GMT; path=/', ';='],
  41. [[['foo_cookie', 'foo=='], ['expires', 'Tue, 22-Sep-2020 06:27:09 GMT'], ['path', '/']], 'foo_cookie=foo==; expires=Tue, 22-Sep-2020 06:27:09 GMT; path=/', ';='],
  42. [[['foo_cookie', 'foo='], ['expires', 'Tue, 22-Sep-2020 06:27:09 GMT'], ['path', '/']], 'foo_cookie=foo=; expires=Tue, 22-Sep-2020 06:27:09 GMT; path=/', ';='],
  43. [[['foo_cookie', 'foo=a=b'], ['expires', 'Tue, 22-Sep-2020 06:27:09 GMT'], ['path', '/']], 'foo_cookie=foo="a=b"; expires=Tue, 22-Sep-2020 06:27:09 GMT; path=/', ';='],
  44. // These are not a valid header values. We test that they parse anyway,
  45. // and that both the valid and invalid parts are returned.
  46. [[], '', ','],
  47. [[], ',,,', ','],
  48. [[['', 'foo'], ['bar', '']], '=foo,bar=', ',='],
  49. [['foo', 'foobar', 'baz'], 'foo, foo"bar", "baz', ','],
  50. [['foo', 'bar, baz'], 'foo, "bar, baz', ','],
  51. [['foo', 'bar, baz\\'], 'foo, "bar, baz\\', ','],
  52. [['foo', 'bar, baz\\'], 'foo, "bar, baz\\\\', ','],
  53. ];
  54. }
  55. public function testCombine()
  56. {
  57. $this->assertSame(['foo' => '123'], HeaderUtils::combine([['foo', '123']]));
  58. $this->assertSame(['foo' => true], HeaderUtils::combine([['foo']]));
  59. $this->assertSame(['foo' => true], HeaderUtils::combine([['Foo']]));
  60. $this->assertSame(['foo' => '123', 'bar' => true], HeaderUtils::combine([['foo', '123'], ['bar']]));
  61. }
  62. public function testToString()
  63. {
  64. $this->assertSame('foo', HeaderUtils::toString(['foo' => true], ','));
  65. $this->assertSame('foo; bar', HeaderUtils::toString(['foo' => true, 'bar' => true], ';'));
  66. $this->assertSame('foo=123', HeaderUtils::toString(['foo' => '123'], ','));
  67. $this->assertSame('foo="1 2 3"', HeaderUtils::toString(['foo' => '1 2 3'], ','));
  68. $this->assertSame('foo="1 2 3", bar', HeaderUtils::toString(['foo' => '1 2 3', 'bar' => true], ','));
  69. }
  70. public function testQuote()
  71. {
  72. $this->assertSame('foo', HeaderUtils::quote('foo'));
  73. $this->assertSame('az09!#$%&\'*.^_`|~-', HeaderUtils::quote('az09!#$%&\'*.^_`|~-'));
  74. $this->assertSame('"foo bar"', HeaderUtils::quote('foo bar'));
  75. $this->assertSame('"foo [bar]"', HeaderUtils::quote('foo [bar]'));
  76. $this->assertSame('"foo \"bar\""', HeaderUtils::quote('foo "bar"'));
  77. $this->assertSame('"foo \\\\ bar"', HeaderUtils::quote('foo \\ bar'));
  78. }
  79. public function testUnquote()
  80. {
  81. $this->assertEquals('foo', HeaderUtils::unquote('foo'));
  82. $this->assertEquals('az09!#$%&\'*.^_`|~-', HeaderUtils::unquote('az09!#$%&\'*.^_`|~-'));
  83. $this->assertEquals('foo bar', HeaderUtils::unquote('"foo bar"'));
  84. $this->assertEquals('foo [bar]', HeaderUtils::unquote('"foo [bar]"'));
  85. $this->assertEquals('foo "bar"', HeaderUtils::unquote('"foo \"bar\""'));
  86. $this->assertEquals('foo "bar"', HeaderUtils::unquote('"foo \"\b\a\r\""'));
  87. $this->assertEquals('foo \\ bar', HeaderUtils::unquote('"foo \\\\ bar"'));
  88. }
  89. public function testMakeDispositionInvalidDisposition()
  90. {
  91. $this->expectException(\InvalidArgumentException::class);
  92. HeaderUtils::makeDisposition('invalid', 'foo.html');
  93. }
  94. /**
  95. * @dataProvider provideMakeDisposition
  96. */
  97. public function testMakeDisposition($disposition, $filename, $filenameFallback, $expected)
  98. {
  99. $this->assertEquals($expected, HeaderUtils::makeDisposition($disposition, $filename, $filenameFallback));
  100. }
  101. public static function provideMakeDisposition()
  102. {
  103. return [
  104. ['attachment', 'foo.html', 'foo.html', 'attachment; filename=foo.html'],
  105. ['attachment', 'foo.html', '', 'attachment; filename=foo.html'],
  106. ['attachment', 'foo bar.html', '', 'attachment; filename="foo bar.html"'],
  107. ['attachment', 'foo "bar".html', '', 'attachment; filename="foo \\"bar\\".html"'],
  108. ['attachment', 'foo%20bar.html', 'foo bar.html', 'attachment; filename="foo bar.html"; filename*=utf-8\'\'foo%2520bar.html'],
  109. ['attachment', 'föö.html', 'foo.html', 'attachment; filename=foo.html; filename*=utf-8\'\'f%C3%B6%C3%B6.html'],
  110. ];
  111. }
  112. /**
  113. * @dataProvider provideMakeDispositionFail
  114. */
  115. public function testMakeDispositionFail($disposition, $filename)
  116. {
  117. $this->expectException(\InvalidArgumentException::class);
  118. HeaderUtils::makeDisposition($disposition, $filename);
  119. }
  120. public static function provideMakeDispositionFail()
  121. {
  122. return [
  123. ['attachment', 'foo%20bar.html'],
  124. ['attachment', 'foo/bar.html'],
  125. ['attachment', '/foo.html'],
  126. ['attachment', 'foo\bar.html'],
  127. ['attachment', '\foo.html'],
  128. ['attachment', 'föö.html'],
  129. ];
  130. }
  131. /**
  132. * @dataProvider provideParseQuery
  133. */
  134. public function testParseQuery(string $query, string $expected = null)
  135. {
  136. $this->assertSame($expected ?? $query, http_build_query(HeaderUtils::parseQuery($query), '', '&'));
  137. }
  138. public static function provideParseQuery()
  139. {
  140. return [
  141. ['a=b&c=d'],
  142. ['a.b=c'],
  143. ['a+b=c'],
  144. ["a\0b=c", 'a='],
  145. ['a%00b=c', 'a=c'],
  146. ['a[b=c', 'a%5Bb=c'],
  147. ['a]b=c', 'a%5Db=c'],
  148. ['a[b]=c', 'a%5Bb%5D=c'],
  149. ['a[b][c.d]=c', 'a%5Bb%5D%5Bc.d%5D=c'],
  150. ['a%5Bb%5D=c'],
  151. ['f[%2525][%26][%3D][p.c]=d', 'f%5B%2525%5D%5B%26%5D%5B%3D%5D%5Bp.c%5D=d'],
  152. ];
  153. }
  154. public function testParseCookie()
  155. {
  156. $query = 'a.b=c; def%5Bg%5D=h';
  157. $this->assertSame($query, http_build_query(HeaderUtils::parseQuery($query, false, ';'), '', '; '));
  158. }
  159. public function testParseQueryIgnoreBrackets()
  160. {
  161. $this->assertSame(['a.b' => ['A', 'B']], HeaderUtils::parseQuery('a.b=A&a.b=B', true));
  162. $this->assertSame(['a.b[]' => ['A']], HeaderUtils::parseQuery('a.b[]=A', true));
  163. $this->assertSame(['a.b[]' => ['A']], HeaderUtils::parseQuery('a.b%5B%5D=A', true));
  164. }
  165. }