Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 6c09f50

Browse files
committed
Merge pull request #175 from pine3ree/patch-3
psr-7 attempt to set the Host header during construction
2 parents 430a19d + 278b96e commit 6c09f50

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/RequestTrait.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ private function initialize($uri = null, $method = null, $body = 'php://memory',
6969
list($this->headerNames, $headers) = $this->filterHeaders($headers);
7070
$this->assertHeaders($headers);
7171
$this->headers = $headers;
72+
73+
// per PSR-7: attempt to set the Host header from a provided URI if no
74+
// Host header is provided
75+
if (! $this->hasHeader('Host') && $this->uri->getHost()) {
76+
$this->headerNames['host'] = 'Host';
77+
$this->headers['Host'] = [$this->getHostFromUri()];
78+
}
7279
}
7380

7481
/**

test/RequestTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,19 @@ public function testGetHostHeaderLineReturnsEmptyStringIfUriDoesNotContainHost()
395395
$this->assertEmpty($request->getHeaderLine('host'));
396396
}
397397

398+
public function testHostHeaderSetFromUriOnCreationIfNoHostHeaderSpecified()
399+
{
400+
$request = new Request('http://www.example.com');
401+
$this->assertTrue($request->hasHeader('Host'));
402+
$this->assertEquals('www.example.com', $request->getHeaderLine('host'));
403+
}
404+
405+
public function testHostHeaderNotSetFromUriOnCreationIfHostHeaderSpecified()
406+
{
407+
$request = new Request('http://www.example.com', null, 'php://memory', ['Host' => 'www.test.com']);
408+
$this->assertEquals('www.test.com', $request->getHeaderLine('host'));
409+
}
410+
398411
public function testPassingPreserveHostFlagWhenUpdatingUriDoesNotUpdateHostHeader()
399412
{
400413
$request = (new Request())

0 commit comments

Comments
 (0)