Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for numeric headers #36

Merged
merged 2 commits into from
Jun 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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