Skip to content
Permalink
Browse files

Finish implementing withUri() and the PSR7 interface.

I missed a parameter and some behavior the first time I implemented
withUri(). This shores up that gap and finishes the PSR7 implementation
in Network/Request.
  • Loading branch information...
markstory committed Sep 26, 2016
1 parent 199deca commit df0d378c27b7b93e4b20b9e96f1013c1546cbe43
Showing with 64 additions and 2 deletions.
  1. +17 −2 src/Network/Request.php
  2. +47 −0 tests/TestCase/Network/RequestTest.php
@@ -21,6 +21,7 @@
use Cake\Network\Exception\MethodNotAllowedException;
use Cake\Utility\Hash;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\UriInterface;
@@ -32,7 +33,7 @@
* A class that helps wrap Request information and particulars about a single request.
* Provides methods commonly used to introspect on the request headers and request body.
*/
class Request implements ArrayAccess
class Request implements ArrayAccess, ServerRequestInterface
{
/**
@@ -1942,13 +1943,27 @@ public function getUri()
* and `url` attributes.
*
* @param \Psr\Http\Message\UriInterface $uri The new request uri
* @param bool $preserveHost Whether or not the host should be retained.
* @return static
*/
public function withUri(UriInterface $uri)
public function withUri(UriInterface $uri, $preserveHost = false)
{
$new = clone $this;
$new->uri = $uri;
if ($preserveHost && $this->hasHeader('Host')) {
return $new;
}
$host = $uri->getHost();
if (!$host) {
return $new;
}
if ($uri->getPort()) {
$host .= ':' . $uri->getPort();
}
$new->_environment['HTTP_HOST'] = $host;
return $new;
}
@@ -20,6 +20,7 @@
use Cake\Network\Session;
use Cake\TestSuite\TestCase;
use Zend\Diactoros\UploadedFile;
use Zend\Diactoros\Uri;
/**
* TestRequest
@@ -2895,6 +2896,9 @@ public function testGetUri()
public function testWithUri()
{
$request = new Request([
'environment' => [
'HTTP_HOST' => 'example.com',
],
'url' => 'articles/view/3'
]);
$uri = $this->getMockBuilder('Psr\Http\Message\UriInterface')->getMock();
@@ -2904,6 +2908,49 @@ public function testWithUri()
$this->assertSame($uri, $new->getUri());
$this->assertSame('articles/view/3', $new->url);
$this->assertSame('articles/view/3', $request->url);
$this->assertSame('example.com', $new->getHeaderLine('Host'));
}
/**
* Test withUri() and preserveHost
*
* @return void
*/
public function testWithUriPreserveHost()
{
$request = new Request([
'environment' => [
'HTTP_HOST' => 'localhost'
],
'url' => 'articles/view/3'
]);
$uri = new Uri();
$uri = $uri->withHost('example.com')
->withPort(123)
->withPath('articles/view/3');
$new = $request->withUri($uri, false);
$this->assertNotSame($new, $request);
$this->assertSame('example.com:123', $new->getHeaderLine('Host'));
}
/**
* Test withUri() and preserveHost missing the host header
*
* @return void
*/
public function testWithUriPreserveHostNoHostHeader()
{
$request = new Request([
'url' => 'articles/view/3'
]);
$uri = new Uri();
$uri = $uri->withHost('example.com')
->withPort(123)
->withPath('articles/view/3');
$new = $request->withUri($uri, false);
$this->assertSame('example.com:123', $new->getHeaderLine('Host'));
}
/**

0 comments on commit df0d378

Please sign in to comment.
You can’t perform that action at this time.