Skip to content

Commit

Permalink
Merge pull request #101 from clue-labs/headers-case
Browse files Browse the repository at this point in the history
Fix merging default headers if overwritten with custom case headers
  • Loading branch information
WyriHaximus authored Jun 26, 2017
2 parents c1374e0 + 0a071cc commit b48b088
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/RequestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,24 @@ private function mergeDefaultheaders(array $headers)
$connectionHeaders = ('1.1' === $this->protocolVersion) ? array('Connection' => 'close') : array();
$authHeaders = $this->getAuthHeaders();

return array_merge(
$defaults = array_merge(
array(
'Host' => $this->getHost().$port,
'User-Agent' => 'React/alpha',
),
$connectionHeaders,
$authHeaders,
$headers
$authHeaders
);

// remove all defaults that already exist in $headers
$lower = array_change_key_case($headers, CASE_LOWER);
foreach ($defaults as $key => $_) {
if (isset($lower[strtolower($key)])) {
unset($defaults[$key]);
}
}

return array_merge($defaults, $headers);
}

public function getScheme()
Expand Down
17 changes: 17 additions & 0 deletions tests/RequestDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ public function toStringReturnsHTTPRequestMessageWithHeaders()
$this->assertSame($expected, $requestData->__toString());
}

/** @test */
public function toStringReturnsHTTPRequestMessageWithHeadersInCustomCase()
{
$requestData = new RequestData('GET', 'http://www.example.com', array(
'user-agent' => 'Hello',
'LAST' => 'World'
));

$expected = "GET / HTTP/1.0\r\n" .
"Host: www.example.com\r\n" .
"user-agent: Hello\r\n" .
"LAST: World\r\n" .
"\r\n";

$this->assertSame($expected, $requestData->__toString());
}

/** @test */
public function toStringReturnsHTTPRequestMessageWithProtocolVersionThroughConstructor()
{
Expand Down

0 comments on commit b48b088

Please sign in to comment.