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

Commit

Permalink
Merge pull request #175 from pine3ree/patch-3
Browse files Browse the repository at this point in the history
psr-7 attempt to set the Host header during construction
  • Loading branch information
weierophinney committed Sep 7, 2016
2 parents 430a19d + 278b96e commit 6c09f50
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ private function initialize($uri = null, $method = null, $body = 'php://memory',
list($this->headerNames, $headers) = $this->filterHeaders($headers);
$this->assertHeaders($headers);
$this->headers = $headers;

// per PSR-7: attempt to set the Host header from a provided URI if no
// Host header is provided
if (! $this->hasHeader('Host') && $this->uri->getHost()) {
$this->headerNames['host'] = 'Host';
$this->headers['Host'] = [$this->getHostFromUri()];
}
}

/**
Expand Down
13 changes: 13 additions & 0 deletions test/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,19 @@ public function testGetHostHeaderLineReturnsEmptyStringIfUriDoesNotContainHost()
$this->assertEmpty($request->getHeaderLine('host'));
}

public function testHostHeaderSetFromUriOnCreationIfNoHostHeaderSpecified()
{
$request = new Request('http://www.example.com');
$this->assertTrue($request->hasHeader('Host'));
$this->assertEquals('www.example.com', $request->getHeaderLine('host'));
}

public function testHostHeaderNotSetFromUriOnCreationIfHostHeaderSpecified()
{
$request = new Request('http://www.example.com', null, 'php://memory', ['Host' => 'www.test.com']);
$this->assertEquals('www.test.com', $request->getHeaderLine('host'));
}

public function testPassingPreserveHostFlagWhenUpdatingUriDoesNotUpdateHostHeader()
{
$request = (new Request())
Expand Down

0 comments on commit 6c09f50

Please sign in to comment.