HeaderTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. declare(strict_types=1);
  3. namespace GuzzleHttp\Tests\Psr7;
  4. use GuzzleHttp\Psr7;
  5. use PHPUnit\Framework\TestCase;
  6. class HeaderTest extends TestCase
  7. {
  8. public function parseParamsProvider(): array
  9. {
  10. $res1 = [
  11. [
  12. '<http:/.../front.jpeg>',
  13. 'rel' => 'front',
  14. 'type' => 'image/jpeg',
  15. ],
  16. [
  17. '<http://.../back.jpeg>',
  18. 'rel' => 'back',
  19. 'type' => 'image/jpeg',
  20. ],
  21. ];
  22. return [
  23. [
  24. '<http:/.../front.jpeg>; rel="front"; type="image/jpeg", <http://.../back.jpeg>; rel=back; type="image/jpeg"',
  25. $res1,
  26. ],
  27. [
  28. '<http:/.../front.jpeg>; rel="front"; type="image/jpeg",<http://.../back.jpeg>; rel=back; type="image/jpeg"',
  29. $res1,
  30. ],
  31. [
  32. 'foo="baz"; bar=123, boo, test="123", foobar="foo;bar"',
  33. [
  34. ['foo' => 'baz', 'bar' => '123'],
  35. ['boo'],
  36. ['test' => '123'],
  37. ['foobar' => 'foo;bar'],
  38. ],
  39. ],
  40. [
  41. '<http://.../side.jpeg?test=1>; rel="side"; type="image/jpeg",<http://.../side.jpeg?test=2>; rel=side; type="image/jpeg"',
  42. [
  43. ['<http://.../side.jpeg?test=1>', 'rel' => 'side', 'type' => 'image/jpeg'],
  44. ['<http://.../side.jpeg?test=2>', 'rel' => 'side', 'type' => 'image/jpeg'],
  45. ],
  46. ],
  47. [
  48. '',
  49. [],
  50. ],
  51. ];
  52. }
  53. /**
  54. * @dataProvider parseParamsProvider
  55. */
  56. public function testParseParams($header, $result): void
  57. {
  58. self::assertSame($result, Psr7\Header::parse($header));
  59. }
  60. public function normalizeProvider(): array
  61. {
  62. return [
  63. [
  64. '',
  65. [],
  66. ],
  67. [
  68. ['a, b', 'c', 'd, e'],
  69. ['a', 'b', 'c', 'd', 'e'],
  70. ],
  71. // Example 'accept-encoding'
  72. [
  73. 'gzip, br',
  74. ['gzip', 'br'],
  75. ],
  76. // https://httpwg.org/specs/rfc7231.html#rfc.section.5.3.2
  77. [
  78. 'text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c',
  79. ['text/plain; q=0.5', 'text/html', 'text/x-dvi; q=0.8', 'text/x-c'],
  80. ],
  81. // Example 'If-None-Match' with comma within an ETag
  82. [
  83. '"foo", "foo,bar", "bar"',
  84. ['"foo"', '"foo,bar"', '"bar"'],
  85. ],
  86. // https://httpwg.org/specs/rfc7234.html#cache.control.extensions
  87. [
  88. 'private, community="UCI"',
  89. ['private', 'community="UCI"'],
  90. ],
  91. // The Cache-Control example with a comma within a community
  92. [
  93. 'private, community="Guzzle,Psr7"',
  94. ['private', 'community="Guzzle,Psr7"'],
  95. ],
  96. // The Cache-Control example with an escaped space (quoted-pair) within a community
  97. [
  98. 'private, community="Guzzle\\ Psr7"',
  99. ['private', 'community="Guzzle\\ Psr7"'],
  100. ],
  101. // The Cache-Control example with an escaped quote (quoted-pair) within a community
  102. [
  103. 'private, community="Guzzle\\"Psr7"',
  104. ['private', 'community="Guzzle\\"Psr7"'],
  105. ],
  106. // The Cache-Control example with an escaped quote (quoted-pair) and a comma within a community
  107. [
  108. 'private, community="Guzzle\\",Psr7"',
  109. ['private', 'community="Guzzle\\",Psr7"'],
  110. ],
  111. // The Cache-Control example with an escaped backslash (quoted-pair) within a community
  112. [
  113. 'private, community="Guzzle\\\\Psr7"',
  114. ['private', 'community="Guzzle\\\\Psr7"'],
  115. ],
  116. // The Cache-Control example with an escaped backslash (quoted-pair) within a community
  117. [
  118. 'private, community="Guzzle\\\\", Psr7',
  119. ['private', 'community="Guzzle\\\\"', 'Psr7'],
  120. ],
  121. // https://httpwg.org/specs/rfc7230.html#rfc.section.7
  122. [
  123. 'foo ,bar,',
  124. ['foo', 'bar'],
  125. ],
  126. // https://httpwg.org/specs/rfc7230.html#rfc.section.7
  127. [
  128. 'foo , ,bar,charlie ',
  129. ['foo', 'bar', 'charlie'],
  130. ],
  131. [
  132. "<https://example.gitlab.com>; rel=\"first\",\n<https://example.gitlab.com>; rel=\"next\",\n<https://example.gitlab.com>; rel=\"prev\",\n<https://example.gitlab.com>; rel=\"last\",",
  133. ['<https://example.gitlab.com>; rel="first"', '<https://example.gitlab.com>; rel="next"', '<https://example.gitlab.com>; rel="prev"', '<https://example.gitlab.com>; rel="last"'],
  134. ],
  135. ];
  136. }
  137. /**
  138. * @dataProvider normalizeProvider
  139. */
  140. public function testNormalize($header, $result): void
  141. {
  142. self::assertSame($result, Psr7\Header::normalize([$header]));
  143. self::assertSame($result, Psr7\Header::normalize($header));
  144. }
  145. /**
  146. * @dataProvider normalizeProvider
  147. */
  148. public function testSplitList($header, $result): void
  149. {
  150. self::assertSame($result, Psr7\Header::splitList($header));
  151. }
  152. public function testSplitListRejectsNestedArrays(): void
  153. {
  154. $this->expectException(\TypeError::class);
  155. Psr7\Header::splitList([['foo']]);
  156. }
  157. public function testSplitListArrayContainingNonStrings(): void
  158. {
  159. $this->expectException(\TypeError::class);
  160. Psr7\Header::splitList(['foo', 'bar', 1, false]);
  161. }
  162. public function testSplitListRejectsNonStrings(): void
  163. {
  164. $this->expectException(\TypeError::class);
  165. Psr7\Header::splitList(false);
  166. }
  167. }