diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 47cebd2e..e32e21a6 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -45,21 +45,9 @@ headerNames[strtolower($header)]]]> headerNames[strtolower($name)]]]> - - - - - - - - - - - - diff --git a/src/MessageTrait.php b/src/MessageTrait.php index e95d072a..4fcc8812 100644 --- a/src/MessageTrait.php +++ b/src/MessageTrait.php @@ -10,6 +10,7 @@ use function array_map; use function array_merge; use function array_values; +use function assert; use function implode; use function is_array; use function is_resource; @@ -330,9 +331,15 @@ private function setHeaders(array $originalHeaders): void $headerNames = $headers = []; foreach ($originalHeaders as $header => $value) { - $value = $this->filterHeaderValue($value); + /** + * Header names should be cast to a string because (valid) numeric header names will be automatically + * cast to an integer by PHP when used as an array key. + */ + $header = (string) $header; + $value = $this->filterHeaderValue($value); $this->assertHeader($header); + assert($header !== ''); $headerNames[strtolower($header)] = $header; $headers[$header] = $value; diff --git a/test/RequestTest.php b/test/RequestTest.php index 2b957019..f0ea5278 100644 --- a/test/RequestTest.php +++ b/test/RequestTest.php @@ -185,11 +185,15 @@ public function testConstructorRaisesExceptionForInvalidBody(mixed $body): void public static function invalidHeaderTypes(): array { return [ - 'indexed-array' => [[['INVALID']], 'header name'], - 'null' => [['x-invalid-null' => null]], - 'true' => [['x-invalid-true' => true]], - 'false' => [['x-invalid-false' => false]], - 'object' => [['x-invalid-object' => (object) ['INVALID']]], + /** + * Indexed array test is disabled and left as a note because according to RFC 7230, numeric header names + * are valid + */ + //'indexed-array' => [[['INVALID']], 'header name'], Integer Header names are effectively valid + 'null' => [['x-invalid-null' => null]], + 'true' => [['x-invalid-true' => true]], + 'false' => [['x-invalid-false' => false]], + 'object' => [['x-invalid-object' => (object) ['INVALID']]], ]; } diff --git a/test/ResponseTest.php b/test/ResponseTest.php index 1ed1da1a..11a3cef3 100644 --- a/test/ResponseTest.php +++ b/test/ResponseTest.php @@ -276,11 +276,15 @@ public function testConstructorRaisesExceptionForInvalidBody(mixed $body): void public static function invalidHeaderTypes(): array { return [ - 'indexed-array' => [[['INVALID']], 'header name'], - 'null' => [['x-invalid-null' => null]], - 'true' => [['x-invalid-true' => true]], - 'false' => [['x-invalid-false' => false]], - 'object' => [['x-invalid-object' => (object) ['INVALID']]], + /** + * Indexed array test is disabled and left as a note because according to RFC 7230, numeric header names + * are valid + */ + //'indexed-array' => [[['INVALID']], 'header name'], Integer/Numeric header names are valid + 'null' => [['x-invalid-null' => null]], + 'true' => [['x-invalid-true' => true]], + 'false' => [['x-invalid-false' => false]], + 'object' => [['x-invalid-object' => (object) ['INVALID']]], ]; }