From ad75894d023b5fe33f2960c72c681140c45b5f05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 5 Oct 2021 09:58:50 +0200 Subject: [PATCH] Add extensive tests for Header::normalize() --- tests/HeaderTest.php | 64 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/tests/HeaderTest.php b/tests/HeaderTest.php index 953498c7..206ed4f0 100644 --- a/tests/HeaderTest.php +++ b/tests/HeaderTest.php @@ -63,9 +63,67 @@ public function testParseParams($header, $result): void self::assertSame($result, Psr7\Header::parse($header)); } - public function testParsesArrayHeaders(): void + public function normalizeProvider(): array { - $header = ['a, b', 'c', 'd, e']; - self::assertSame(['a', 'b', 'c', 'd', 'e'], Psr7\Header::normalize($header)); + 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) space 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'], + ] + ]; + } + + /** + * @dataProvider normalizeProvider + */ + public function testNormalize($header, $result): void + { + self::assertSame($result, Psr7\Header::normalize([$header])); + self::assertSame($result, Psr7\Header::normalize($header)); } }