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

psr-7 attempt to set the Host header during construction #175

Merged
merged 4 commits into from
Sep 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
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