diff --git a/composer.json b/composer.json index 0a8c62e..732406d 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ }, "require-dev": { "phpunit/phpunit": "^7.0", - "nyholm/psr7": "^1.0", + "nyholm/psr7": "^1.3", "nyholm/nsa": "^1.1" }, "autoload": { diff --git a/src/ServerRequestCreator.php b/src/ServerRequestCreator.php index 1e66daa..baf1b2c 100644 --- a/src/ServerRequestCreator.php +++ b/src/ServerRequestCreator.php @@ -65,6 +65,11 @@ public function fromArrays(array $server, array $headers = [], array $cookie = [ $serverRequest = $this->serverRequestFactory->createServerRequest($method, $uri, $server); foreach ($headers as $name => $value) { + // Because PHP automatically casts array keys set with numeric strings to integers, we have to make sure + // that numeric headers will not be sent along as integers, as withAddedHeader can only accept strings. + if (\is_int($name)) { + $name = (string) $name; + } $serverRequest = $serverRequest->withAddedHeader($name, $value); } @@ -237,6 +242,8 @@ private function normalizeNestedFileSpec(array $files = []): array * Create a new uri from server variable. * * @param array $server typically $_SERVER or similar structure + * + * @return UriInterface */ private function createUriFromArray(array $server): UriInterface { diff --git a/tests/ServerRequestCreatorTest.php b/tests/ServerRequestCreatorTest.php index b93a8a6..90bb4e4 100644 --- a/tests/ServerRequestCreatorTest.php +++ b/tests/ServerRequestCreatorTest.php @@ -323,6 +323,16 @@ public function testNormalizeFilesRaisesException() $this->creator->fromArrays(['REQUEST_METHOD' => 'POST'], [], [], [], [], ['test' => 'something']); } + public function testNumericHeaderFromHeaderArray() + { + $server = [ + 'REQUEST_METHOD' => 'GET', + ]; + + $server = $this->creator->fromArrays($server, ['1234' => 'NumericHeader']); + $this->assertEquals(['1234' => ['NumericHeader']], $server->getHeaders()); + } + public function testFromArrays() { $server = [ @@ -336,6 +346,8 @@ public function testFromArrays() 'REQUEST_TIME' => 'Request start time: 1280149029', 'QUERY_STRING' => 'id=10&user=foo', 'DOCUMENT_ROOT' => '/path/to/your/server/root/', + 'HTTP_0' => 'NumericHeaderZero', + 'HTTP_1234' => 'NumericHeader', 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'HTTP_ACCEPT_ENCODING' => 'gzip,deflate', @@ -419,6 +431,8 @@ public function dataGetUriFromGlobals() 'REQUEST_TIME' => 'Request start time: 1280149029', 'QUERY_STRING' => 'id=10&user=foo', 'DOCUMENT_ROOT' => '/path/to/your/server/root/', + 'HTTP_0' => 'NumericHeaderZero', + 'HTTP_1234' => 'NumericHeader', 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'HTTP_ACCEPT_ENCODING' => 'gzip,deflate', @@ -497,6 +511,8 @@ public function testGetUriFromGlobals($expected, $serverParams) public function testMarshalsExpectedHeadersFromServerArray() { $server = [ + 'HTTP_0' => 'NumericHeaderZero', + 'HTTP_1234' => 'NumericHeader', 'HTTP_COOKIE' => 'COOKIE', 'HTTP_AUTHORIZATION' => 'token', 'HTTP_CONTENT_TYPE' => 'application/json', @@ -507,6 +523,8 @@ public function testMarshalsExpectedHeadersFromServerArray() ]; $expected = [ + '0' => 'NumericHeaderZero', + '1234' => 'NumericHeader', 'cookie' => 'COOKIE', 'authorization' => 'token', 'content-type' => 'application/json',