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 header names #149

Merged
merged 2 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions src/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ public function withBody(StreamInterface $body): self
private function setHeaders(array $headers): void
{
foreach ($headers as $header => $value) {
if (\is_int($header)) {
nickdnk marked this conversation as resolved.
Show resolved Hide resolved
// If a header name was set to a numeric string, PHP will cast the key to an int.
// We must cast it back to a string in order to comply with validation.
$header = (string) $header;
}
$value = $this->validateAndTrimHeader($header, $value);
$normalized = self::lowercase($header);
if (isset($this->headerNames[$normalized])) {
Expand Down
42 changes: 42 additions & 0 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,48 @@ public function testSupportNumericHeaders()
$this->assertSame('200', $r->getHeaderLine('Content-Length'));
}

public function testSupportNumericHeaderNames()
{
$r = new Request(
'GET', '', [
'200' => 'NumericHeaderValue',
'0' => 'NumericHeaderValueZero',
]
);

$this->assertSame(
[
'200' => ['NumericHeaderValue'],
'0' => ['NumericHeaderValueZero'],
],
$r->getHeaders()
);

$this->assertSame(['NumericHeaderValue'], $r->getHeader('200'));
$this->assertSame('NumericHeaderValue', $r->getHeaderLine('200'));

$r = $r->withHeader('300', 'NumericHeaderValue2')
->withAddedHeader('200', ['A', 'B']);

$this->assertSame(
[
'200' => ['NumericHeaderValue', 'A', 'B'],
'0' => ['NumericHeaderValueZero'],
'300' => ['NumericHeaderValue2'],
],
$r->getHeaders()
);

$r = $r->withoutHeader('300');
$this->assertSame(
[
'200' => ['NumericHeaderValue', 'A', 'B'],
'0' => ['NumericHeaderValueZero'],
],
$r->getHeaders()
);
}

public function testAddsPortToHeader()
{
$r = new Request('GET', 'http://foo.com:8124/bar');
Expand Down