Skip to content

Commit 7058179

Browse files
committed
Fix merging default headers if overwritten with custom case headers
1 parent caf646e commit 7058179

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/RequestData.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,24 @@ private function mergeDefaultheaders(array $headers)
2323
$connectionHeaders = ('1.1' === $this->protocolVersion) ? array('Connection' => 'close') : array();
2424
$authHeaders = $this->getAuthHeaders();
2525

26-
return array_merge(
26+
$defaults = array_merge(
2727
array(
2828
'Host' => $this->getHost().$port,
2929
'User-Agent' => 'React/alpha',
3030
),
3131
$connectionHeaders,
32-
$authHeaders,
33-
$headers
32+
$authHeaders
3433
);
34+
35+
// remove all defaults that already exist in $headers
36+
$lower = array_change_key_case($headers, CASE_LOWER);
37+
foreach ($defaults as $key => $_) {
38+
if (isset($lower[strtolower($key)])) {
39+
unset($defaults[$key]);
40+
}
41+
}
42+
43+
return array_merge($defaults, $headers);
3544
}
3645

3746
public function getScheme()

tests/RequestDataTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ public function toStringReturnsHTTPRequestMessageWithProtocolVersion()
3434
$this->assertSame($expected, $requestData->__toString());
3535
}
3636

37+
/** @test */
38+
public function toStringReturnsHTTPRequestMessageWithHeadersInCustomCase()
39+
{
40+
$requestData = new RequestData('GET', 'http://www.example.com', array(
41+
'user-agent' => 'Hello',
42+
'LAST' => 'World'
43+
));
44+
45+
$expected = "GET / HTTP/1.0\r\n" .
46+
"Host: www.example.com\r\n" .
47+
"user-agent: Hello\r\n" .
48+
"LAST: World\r\n" .
49+
"\r\n";
50+
51+
$this->assertSame($expected, $requestData->__toString());
52+
}
53+
3754
/** @test */
3855
public function toStringReturnsHTTPRequestMessageWithProtocolVersionThroughConstructor()
3956
{

0 commit comments

Comments
 (0)