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