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

Commit 11d695b

Browse files
committed
Merge branch 'hotfix/psr-7-compliance'
Close #49
2 parents 320ea38 + 534512c commit 11d695b

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ All notable changes to this project will be documented in this file, in reverse
2727
- [#57](https://github.com/zendframework/zend-diactoros/pull/57) fixes the
2828
behavior of how the `ServerRequestFactory` marshals upload files when they are
2929
represented as a nested associative array.
30+
- [#49](https://github.com/zendframework/zend-diactoros/pull/49) provides several
31+
fixes that ensure that Diactoros complies with the PSR-7 specification:
32+
- `MessageInterface::getHeaderLine()` MUST return a string (that string CAN be
33+
empty). Previously, Diactoros would return `null`.
34+
- If no `Host` header is set, the `$preserveHost` flag MUST be ignored when
35+
calling `withUri()` (previously, Diactoros would not set the `Host` header
36+
if `$preserveHost` was `true`, but no `Host` header was present).
37+
- The request method MUST be a string; it CAN be empty. Previously, Diactoros
38+
would return `null`.
39+
- The request MUST return a `UriInterface` instance from `getUri()`; that
40+
instance CAN be empty. Previously, Diactoros would return `null`; now it
41+
lazy-instantiates an empty `Uri` instance on initialization.
3042

3143
## 1.0.3 - 2015-06-04
3244

src/RequestTrait.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@
2626
* @property array $headers
2727
* @property array $headerNames
2828
* @property StreamInterface $stream
29+
* @method bool hasHeader(string $header)
2930
*/
3031
trait RequestTrait
3132
{
3233
/**
3334
* @var string
3435
*/
35-
private $method;
36+
private $method = '';
3637

3738
/**
3839
* The request-target, if it has been provided or calculated.
@@ -96,8 +97,8 @@ private function initialize($uri = null, $method = null, $body = 'php://memory',
9697
$uri = new Uri($uri);
9798
}
9899

99-
$this->method = $method;
100-
$this->uri = $uri;
100+
$this->method = $method ?: '';
101+
$this->uri = $uri ?: new Uri();
101102
$this->stream = ($body instanceof StreamInterface) ? $body : new Stream($body, 'r');
102103

103104
list($this->headerNames, $headers) = $this->filterHeaders($headers);
@@ -251,7 +252,7 @@ public function withUri(UriInterface $uri, $preserveHost = false)
251252
$new = clone $this;
252253
$new->uri = $uri;
253254

254-
if ($preserveHost) {
255+
if ($preserveHost && $this->hasHeader('Host')) {
255256
return $new;
256257
}
257258

test/RequestTest.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public function setUp()
2626
$this->request = new Request();
2727
}
2828

29-
public function testMethodIsNullByDefault()
29+
public function testMethodIsEmptyByDefault()
3030
{
31-
$this->assertNull($this->request->getMethod());
31+
$this->assertSame('', $this->request->getMethod());
3232
}
3333

3434
public function testMethodMutatorReturnsCloneWithChangedMethod()
@@ -38,9 +38,18 @@ public function testMethodMutatorReturnsCloneWithChangedMethod()
3838
$this->assertEquals('GET', $request->getMethod());
3939
}
4040

41-
public function testUriIsNullByDefault()
41+
public function testReturnsUnpopulatedUriByDefault()
4242
{
43-
$this->assertNull($this->request->getUri());
43+
$uri = $this->request->getUri();
44+
$this->assertInstanceOf('Psr\Http\Message\UriInterface', $uri);
45+
$this->assertInstanceOf('Zend\Diactoros\Uri', $uri);
46+
$this->assertEmpty($uri->getScheme());
47+
$this->assertEmpty($uri->getUserInfo());
48+
$this->assertEmpty($uri->getHost());
49+
$this->assertNull($uri->getPort());
50+
$this->assertEmpty($uri->getPath());
51+
$this->assertEmpty($uri->getQuery());
52+
$this->assertEmpty($uri->getFragment());
4453
}
4554

4655
public function testConstructorRaisesExceptionForInvalidStream()

0 commit comments

Comments
 (0)