Skip to content

Commit

Permalink
Added support for numeric headers (#36)
Browse files Browse the repository at this point in the history
* Added support for numeric headers

* PHPdoc
require ^1.3 of psr7 for tests to pass
  • Loading branch information
nickdnk authored Jun 6, 2020
1 parent e6a526e commit 60dcbc2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"require-dev": {
"phpunit/phpunit": "^7.0",
"nyholm/psr7": "^1.0",
"nyholm/psr7": "^1.3",
"nyholm/nsa": "^1.1"
},
"autoload": {
Expand Down
7 changes: 7 additions & 0 deletions src/ServerRequestCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
{
Expand Down
18 changes: 18 additions & 0 deletions tests/ServerRequestCreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -507,6 +523,8 @@ public function testMarshalsExpectedHeadersFromServerArray()
];

$expected = [
'0' => 'NumericHeaderZero',
'1234' => 'NumericHeader',
'cookie' => 'COOKIE',
'authorization' => 'token',
'content-type' => 'application/json',
Expand Down

0 comments on commit 60dcbc2

Please sign in to comment.