Update to match revisions to PSR-7

This commit is contained in:
PJ Dietz 2015-04-12 18:13:17 -04:00
parent 79be20c826
commit 4dba068f3d
7 changed files with 158 additions and 253 deletions

View File

@ -4,6 +4,7 @@ namespace WellRESTed\Message;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\StreamableInterface;
use WellRESTed\Stream\NullStream;
abstract class Message implements MessageInterface
{
@ -17,6 +18,7 @@ abstract class Message implements MessageInterface
public function __construct()
{
$this->headers = new HeaderCollection();
$this->body = new NullStream();
}
// ------------------------------------------------------------------------
@ -101,23 +103,49 @@ abstract class Message implements MessageInterface
}
/**
* Retrieve a header by the given case-insensitive name, as a string.
* Retrieves a message header value by the given case-insensitive name.
*
* This method returns an array of all the header values of the given
* case-insensitive header name.
*
* If the header does not appear in the message, this method MUST return an
* empty array.
*
* @param string $name Case-insensitive header field name.
* @return string[] An array of string values as provided for the given
* header. If the header does not appear in the message, this method MUST
* return an empty array.
*/
public function getHeader($name)
{
if (isset($this->headers[$name])) {
return $this->headers[$name];
} else {
return [];
}
}
/**
* Retrieves the line for a single header, with the header values as a
* comma-separated string.
*
* This method returns all of the header values of the given
* case-insensitive header name as a string concatenated together using
* a comma.
*
* NOTE: Not all header values may be appropriately represented using
* comma concatenation. For such headers, use getHeaderLines() instead
* comma concatenation. For such headers, use getHeader() instead
* and supply your own delimiter when concatenating.
*
* If the header did not appear in the message, this method should return
* If the header does not appear in the message, this method MUST return
* a null value.
*
* @param string $name Case-insensitive header field name.
* @return string|null
* @return string|null A string of values as provided for the given header
* concatenated together using a comma. If the header does not appear in
* the message, this method MUST return a null value.
*/
public function getHeader($name)
public function getHeaderLine($name)
{
if (isset($this->headers[$name])) {
return join(", ", $this->headers[$name]);
@ -126,24 +154,6 @@ abstract class Message implements MessageInterface
}
}
/**
* Retrieves a header by the given case-insensitive name as an array of strings.
*
* If the header did not appear in the message, this method should return an
* empty array.
*
* @param string $name Case-insensitive header field name.
* @return string[]
*/
public function getHeaderLines($name)
{
if (isset($this->headers[$name])) {
return $this->headers[$name];
} else {
return [];
}
}
/**
* Create a new instance with the provided header, replacing any existing
* values of any headers with the same case-insensitive name.

View File

@ -50,18 +50,20 @@ class Request extends Message implements RequestInterface
* This method acts exactly like MessageInterface::getHeader(), with
* one behavioral change: if the Host header is requested, but has
* not been previously set, the method MUST attempt to pull the host
* segment of the composed URI, if present.
* component of the composed URI, if present.
*
* @see MessageInterface::getHeader()
* @see UriInterface::getHost()
* @param string $name Case-insensitive header field name.
* @return string
* @return string[] An array of string values as provided for the given
* header. If the header does not appear in the message, this method MUST
* return an empty array.
*/
public function getHeader($name)
{
$header = parent::getHeader($name);
if ($header === null && (strtolower($name) === "host") && isset($this->uri)) {
$header = $this->uri->getHost();
if ($header === [] && (strtolower($name) === "host") && isset($this->uri)) {
$header = [$this->uri->getHost()];
}
return $header;
}
@ -70,25 +72,29 @@ class Request extends Message implements RequestInterface
* Extends MessageInterface::getHeaderLines() to provide request-specific
* behavior.
*
* Retrieves a header by the given case-insensitive name as an array of strings.
* This method returns all of the header values of the given
* case-insensitive header name as a string concatenated together using
* a comma.
*
* This method acts exactly like MessageInterface::getHeaderLines(), with
* one behavioral change: if the Host header is requested, but has
* not been previously set, the method MUST attempt to pull the host
* segment of the composed URI, if present.
* component of the composed URI, if present.
*
* @see MessageInterface::getHeaderLines()
* @see MessageInterface::getHeaderLine()
* @see UriInterface::getHost()
* @param string $name Case-insensitive header field name.
* @return string[]
* @return string|null A string of values as provided for the given header
* concatenated together using a comma. If the header does not appear in
* the message, this method MUST return a null value.
*/
public function getHeaderLines($name)
public function getHeaderLine($name)
{
$headerLines = parent::getHeaderLines($name);
if (!$headerLines && (strtolower($name) === "host") && isset($this->uri)) {
$headerLines = [$this->uri->getHost()];
$headerLine = parent::getHeaderLine($name);
if ($headerLine === null && (strtolower($name) === "host") && isset($this->uri)) {
$headerLine = $this->uri->getHost();
}
return $headerLines;
return $headerLine;
}
/**
@ -193,17 +199,31 @@ class Request extends Message implements RequestInterface
}
/**
* Create a new instance with the provided URI.
* Returns an instance with the provided URI.
*
* This method will update the Host header of the returned request by
* default if the URI contains a host component. If the URI does not
* contain a host component, any pre-existing Host header will be carried
* over to the returned request.
*
* You can opt-in to preserving the original state of the Host header by
* setting `$preserveHost` to `true`. When `$preserveHost` is set to
* `true`, the returned request will not update the Host header of the
* returned message -- even if the message contains no Host header. This
* means that a call to `getHeader('Host')` on the original request MUST
* equal the return value of a call to `getHeader('Host')` on the returned
* request.
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return a new instance that has the
* immutability of the message, and MUST return an instance that has the
* new UriInterface instance.
*
* @link http://tools.ietf.org/html/rfc3986#section-4.3
* @param UriInterface $uri New request URI to use.
* @param bool $preserveHost Preserve the original state of the Host header.
* @return self
*/
public function withUri(UriInterface $uri)
public function withUri(UriInterface $uri, $preserveHost = false)
{
$request = clone $this;
$request->uri = $uri;

View File

@ -316,7 +316,7 @@ class ServerRequest extends Request implements ServerRequestInterface
$this->body = $this->getStreamForBody();
$contentType = $this->getHeader("Content-type");
if ($contentType === "application/x-www-form-urlencoded" || $contentType === "multipart/form-data") {
if ($contentType === ["application/x-www-form-urlencoded"] || $contentType === ["multipart/form-data"]) {
$this->parsedBody = $_POST;
}
}

View File

@ -2,130 +2,102 @@
namespace WellRESTed\Test\Unit\Message;
/**
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
class MessageTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\HeaderCollection
*/
public function testCreatesInstance()
{
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$this->assertNotNull($message);
}
/**
* @covers WellRESTed\Message\Message::getProtocolVersion
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\HeaderCollection
*/
public function testReturnsProtocolVersion1Point1ByDefault()
{
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$this->assertEquals("1.1", $message->getProtocolVersion());
}
/**
* @covers WellRESTed\Message\Message::getProtocolVersion
* @uses WellRESTed\Message\Message::withProtocolVersion
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\Message::__clone
* @uses WellRESTed\Message\HeaderCollection
*/
public function testReturnsProtocolVersion()
{
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$message = $message->withProtocolVersion("1.0");
$this->assertEquals("1.0", $message->getProtocolVersion());
}
/**
* @covers WellRESTed\Message\Message::withProtocolVersion
* @uses WellRESTed\Message\Message::getProtocolVersion
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\Message::__clone
* @uses WellRESTed\Message\HeaderCollection
*/
public function testReplacesProtocolVersion()
{
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$message = $message->withProtocolVersion("1.0");
$this->assertEquals("1.0", $message->getProtocolVersion());
}
/**
* @covers WellRESTed\Message\Message::withHeader
* @uses WellRESTed\Message\Message::getHeader
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\Message::__clone
* @uses WellRESTed\Message\HeaderCollection
*/
public function testWithHeaderSetsHeader()
{
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$message = $message->withHeader("Content-type", "application/json");
$this->assertEquals("application/json", $message->getHeader("Content-type"));
$this->assertEquals(["application/json"], $message->getHeader("Content-type"));
}
/**
* @covers WellRESTed\Message\Message::withHeader
* @uses WellRESTed\Message\Message::getHeaderLines
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\Message::__clone
* @uses WellRESTed\Message\HeaderCollection
*/
public function testWithHeaderReplacesValue()
{
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$message = $message->withHeader("Set-Cookie", "cat=Molly");
$message = $message->withHeader("Set-Cookie", "dog=Bear");
$cookies = $message->getHeaderLines("Set-Cookie");
$cookies = $message->getHeader("Set-Cookie");
$this->assertNotContains("cat=Molly", $cookies);
$this->assertContains("dog=Bear", $cookies);
}
/**
* @covers WellRESTed\Message\Message::withAddedHeader
* @uses WellRESTed\Message\Message::getHeader
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\Message::__clone
* @uses WellRESTed\Message\HeaderCollection
*/
public function testWithAddedHeaderSetsHeader()
{
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$message = $message->withAddedHeader("Content-type", "application/json");
$this->assertEquals("application/json", $message->getHeader("Content-type"));
$this->assertEquals(["application/json"], $message->getHeader("Content-type"));
}
/**
* @covers WellRESTed\Message\Message::withAddedHeader
* @uses WellRESTed\Message\Message::getHeaderLines
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\Message::__clone
* @uses WellRESTed\Message\HeaderCollection
*/
public function testWithAddedHeaderAppendsValue()
{
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$message = $message->withAddedHeader("Set-Cookie", "cat=Molly");
$message = $message->withAddedHeader("Set-Cookie", "dog=Bear");
$cookies = $message->getHeaderLines("Set-Cookie");
$cookies = $message->getHeader("Set-Cookie");
$this->assertContains("cat=Molly", $cookies);
$this->assertContains("dog=Bear", $cookies);
}
/**
* @covers WellRESTed\Message\Message::withoutHeader
* @uses WellRESTed\Message\Message::withHeader
* @uses WellRESTed\Message\Message::hasHeader
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\Message::__clone
* @uses WellRESTed\Message\HeaderCollection
*/
public function testWithoutHeaderRemovesHeader()
{
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$message = $message->withHeader("Content-type", "application/json");
$message = $message->withoutHeader("Content-type");
$this->assertFalse($message->hasHeader("Content-type"));
@ -133,110 +105,79 @@ class MessageTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\Message::getHeader
* @uses WellRESTed\Message\Message::withAddedHeader
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\Message::__clone
* @uses WellRESTed\Message\HeaderCollection
*/
public function testGetHeaderReturnsSingleHeader()
{
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$message = $message->withAddedHeader("Content-type", "application/json");
$this->assertEquals("application/json", $message->getHeader("Content-type"));
$this->assertEquals(["application/json"], $message->getHeader("Content-type"));
}
/**
* @covers WellRESTed\Message\Message::getHeader
* @uses WellRESTed\Message\Message::withAddedHeader
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\Message::__clone
* @uses WellRESTed\Message\HeaderCollection
* @covers WellRESTed\Message\Message::getHeaderLine
*/
public function testGetHeaderReturnsMultipleHeadersJoinedByCommas()
{
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$message = $message->withAddedHeader("X-name", "cat=Molly");
$message = $message->withAddedHeader("X-name", "dog=Bear");
$this->assertEquals("cat=Molly, dog=Bear", $message->getHeader("X-name"));
$this->assertEquals("cat=Molly, dog=Bear", $message->getHeaderLine("X-name"));
}
/**
* @covers WellRESTed\Message\Message::getHeaderLine
*/
public function testGetHeaderLineReturnsNullForUnsetHeader()
{
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$this->assertNull($message->getHeaderLine("X-not-set"));
}
/**
* @covers WellRESTed\Message\Message::getHeader
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\HeaderCollection
*/
public function testGetHeaderReturnsNullForUnsetHeader()
public function testGetHeaderReturnsMultipleValuesForHeader()
{
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$this->assertNull($message->getHeader("X-not-set"));
}
/**
* @covers WellRESTed\Message\Message::getHeaderLines
* @uses WellRESTed\Message\Message::withAddedHeader
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\Message::__clone
* @uses WellRESTed\Message\HeaderCollection
*/
public function testGetHeaderLinesReturnsMultipleValuesForHeader()
{
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$message = $message->withAddedHeader("X-name", "cat=Molly");
$message = $message->withAddedHeader("X-name", "dog=Bear");
$this->assertEquals(["cat=Molly", "dog=Bear"], $message->getHeaderLines("X-name"));
$this->assertEquals(["cat=Molly", "dog=Bear"], $message->getHeader("X-name"));
}
/**
* @covers WellRESTed\Message\Message::getHeaderLines
* @uses WellRESTed\Message\Message::withAddedHeader
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\Message::__clone
* @uses WellRESTed\Message\HeaderCollection
* @covers WellRESTed\Message\Message::getHeader
*/
public function testGetHeaderLinesReturnsEmptyArrayForUnsetHeader()
public function testGetHeaderReturnsEmptyArrayForUnsetHeader()
{
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$this->assertEquals([], $message->getHeaderLines("X-name"));
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$this->assertEquals([], $message->getHeader("X-name"));
}
/**
* @covers WellRESTed\Message\Message::hasHeader
* @uses WellRESTed\Message\Message::withHeader
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\Message::__clone
* @uses WellRESTed\Message\HeaderCollection
*/
public function testHasHeaderReturnsTrueWhenHeaderIsSet()
{
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$message = $message->withHeader("Content-type", "application/json");
$this->assertTrue($message->hasHeader("Content-type"));
}
/**
* @covers WellRESTed\Message\Message::hasHeader
* @uses WellRESTed\Message\Message::withHeader
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\Message::__clone
* @uses WellRESTed\Message\HeaderCollection
*/
public function testHasHeaderReturnsFalseWhenHeaderIsNotSet()
{
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$this->assertFalse($message->hasHeader("Content-type"));
}
/**
* @covers WellRESTed\Message\Message::getHeaders
* @uses WellRESTed\Message\Message::withHeader
* @uses WellRESTed\Message\Message::withAddedHeader
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\Message::__clone
* @uses WellRESTed\Message\HeaderCollection
*/
public function testGetHeadersReturnOriginalHeaderNamesAsKeys()
{
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$message = $message->withHeader("Set-Cookie", "cat=Molly");
$message = $message->withAddedHeader("Set-Cookie", "dog=Bear");
$message = $message->withHeader("Content-type", "application/json");
@ -253,15 +194,10 @@ class MessageTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\Message::getHeaders
* @uses WellRESTed\Message\Message::withHeader
* @uses WellRESTed\Message\Message::withAddedHeader
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\Message::__clone
* @uses WellRESTed\Message\HeaderCollection
*/
public function testGetHeadersReturnOriginalHeaderNamesAndValues()
{
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$message = $message->withHeader("Set-Cookie", "cat=Molly");
$message = $message->withAddedHeader("Set-Cookie", "dog=Bear");
$message = $message->withHeader("Content-type", "application/json");
@ -288,45 +224,37 @@ class MessageTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\Message::getBody
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\HeaderCollection
* @uses WellRESTed\Stream\NullStream
*/
public function testGetBodyReturnsNullByDefalt()
public function testGetBodyReturnsEmptyStreamByDefault()
{
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$this->assertNull($message->getBody());
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$this->assertEquals("", (string) $message->getBody());
}
/**
* @covers WellRESTed\Message\Message::getBody
* @covers WellRESTed\Message\Message::withBody
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\Message::__clone
* @uses WellRESTed\Message\HeaderCollection
*/
public function testGetBodyReturnsAttachedStream()
{
$stream = $this->prophesize("\\Psr\\Http\\Message\\StreamableInterface");
$stream = $this->prophesize('\Psr\Http\Message\StreamableInterface');
$stream = $stream->reveal();
$message = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$message = $message->withBody($stream);
$this->assertSame($stream, $message->getBody());
}
/**
* @covers WellRESTed\Message\Message::__clone
* @uses WellRESTed\Message\Message::__construct
* @uses WellRESTed\Message\Message::withHeader
* @uses WellRESTed\Message\Message::getHeader
* @uses WellRESTed\Message\HeaderCollection
*/
public function testCloneMakesDeepCopyOfHeaders()
{
$message1 = $this->getMockForAbstractClass("\\WellRESTed\\Message\\Message");
$message1 = $this->getMockForAbstractClass('\WellRESTed\Message\Message');
$message1 = $message1->withHeader("Content-type", "text/plain");
$message2 = $message1->withHeader("Content-type", "application/json");
$this->assertEquals("text/plain", $message1->getHeader("Content-type"));
$this->assertEquals("application/json", $message2->getHeader("Content-type"));
$this->assertEquals(["text/plain"], $message1->getHeader("Content-type"));
$this->assertEquals(["application/json"], $message2->getHeader("Content-type"));
}
}

View File

@ -4,18 +4,20 @@ namespace WellRESTed\Test\Unit\Message;
use WellRESTed\Message\Request;
/**
* @uses WellRESTed\Message\Request
* @uses WellRESTed\Message\Request
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
class RequestTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers WellRESTed\Message\Request::getHeaders
* @uses WellRESTed\Message\Request::withUri
* @uses WellRESTed\Message\Request::__clone
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testGetHeadersReturnsHostFromUri()
{
$uri = $this->prophesize("\\Psr\\Http\\Message\\UriInterface");
$uri = $this->prophesize('\Psr\Http\Message\UriInterface');
$uri->getHost()->willReturn("localhost");
$request = new Request();
@ -27,15 +29,11 @@ class RequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\Request::getHeaders
* @uses WellRESTed\Message\Request::withUri
* @uses WellRESTed\Message\Request::__clone
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testGetHeadersPrefersExplicitHostHeader()
{
$uri = $this->prophesize("\\Psr\\Http\\Message\\UriInterface");
$uri->getHost()->willReturn("localhot");
$uri = $this->prophesize('\Psr\Http\Message\UriInterface');
$uri->getHost()->willReturn("localhost");
$request = new Request();
$request = $request->withUri($uri->reveal());
@ -47,82 +45,60 @@ class RequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\Request::getHeader
* @uses WellRESTed\Message\Request::getRequestTarget
* @uses WellRESTed\Message\Request::withUri
* @uses WellRESTed\Message\Request::__clone
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testGetHeaderReturnsHostFromUri()
{
$uri = $this->prophesize("\\Psr\\Http\\Message\\UriInterface");
$uri = $this->prophesize('\Psr\Http\Message\UriInterface');
$uri->getHost()->willReturn("localhost");
$request = new Request();
$request = $request->withUri($uri->reveal());
$this->assertEquals("localhost", $request->getHeader("host"));
$this->assertEquals(["localhost"], $request->getHeader("host"));
}
/**
* @covers WellRESTed\Message\Request::getHeader
* @uses WellRESTed\Message\Request::getRequestTarget
* @uses WellRESTed\Message\Request::withUri
* @uses WellRESTed\Message\Request::__clone
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testGetHeaderPrefersExplicitHostHeader()
{
$uri = $this->prophesize("\\Psr\\Http\\Message\\UriInterface");
$uri = $this->prophesize('\Psr\Http\Message\UriInterface');
$uri->getHost()->willReturn("localhot");
$request = new Request();
$request = $request->withUri($uri->reveal());
$request = $request->withHeader("Host", "www.mysite.com");
$this->assertEquals("www.mysite.com", $request->getHeader("host"));
$this->assertEquals(["www.mysite.com"], $request->getHeader("host"));
}
/**
* @covers WellRESTed\Message\Request::getHeaderLines
* @uses WellRESTed\Message\Request::withUri
* @uses WellRESTed\Message\Request::__clone
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
* @covers WellRESTed\Message\Request::getHeaderLine
*/
public function testGetHeaderLinesReturnsHostFromUri()
public function testGetHeaderLineReturnsHostFromUri()
{
$uri = $this->prophesize("\\Psr\\Http\\Message\\UriInterface");
$uri->getHost()->willReturn("localhot");
$uri = $this->prophesize('\Psr\Http\Message\UriInterface');
$uri->getHost()->willReturn("localhost");
$request = new Request();
$request = $request->withUri($uri->reveal());
$this->assertEquals(["localhot"], $request->getHeaderLines("host"));
$this->assertEquals("localhost", $request->getHeaderLine("host"));
}
/**
* @covers WellRESTed\Message\Request::getHeaderLines
* @uses WellRESTed\Message\Request::withUri
* @uses WellRESTed\Message\Request::__clone
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
* @covers WellRESTed\Message\Request::getHeaderLine
*/
public function testGetHeaderLinesPrefersExplicitHostHeader()
public function testGetHeaderLinePrefersExplicitHostHeader()
{
$uri = $this->prophesize("\\Psr\\Http\\Message\\UriInterface");
$uri->getHost()->willReturn("localhot");
$uri = $this->prophesize('\Psr\Http\Message\UriInterface');
$uri->getHost()->willReturn("localhost");
$request = new Request();
$request = $request->withUri($uri->reveal());
$request = $request->withHeader("Host", "www.mysite.com");
$this->assertEquals(["www.mysite.com"], $request->getHeaderLines("host"));
$this->assertEquals("www.mysite.com", $request->getHeaderLine("host"));
}
/**
* @covers WellRESTed\Message\Request::getRequestTarget
* @uses WellRESTed\Message\Request::withRequestTarget
* @uses WellRESTed\Message\Request::__clone
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testGetRequestTargetPrefersConreteRequestTarget()
{
@ -133,14 +109,10 @@ class RequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\Request::getRequestTarget
* @uses WellRESTed\Message\Request::withUri
* @uses WellRESTed\Message\Request::__clone
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testGetRequestTargetUsesOriginFormOfUri()
{
$uri = $this->prophesize("\\Psr\\Http\\Message\\UriInterface");
$uri = $this->prophesize('\Psr\Http\Message\UriInterface');
$uri->getPath()->willReturn("/my/path");
$uri->getQuery()->willReturn("cat=Molly&dog=Bear");
@ -151,8 +123,6 @@ class RequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\Request::getRequestTarget
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testGetRequestTargetReturnsSlashByDefault()
{
@ -162,8 +132,6 @@ class RequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\Request::getMethod
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testGetMethodReturnsGetByDefault()
{
@ -174,9 +142,6 @@ class RequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\Request::withMethod
* @covers WellRESTed\Message\Request::getMethod
* @uses WellRESTed\Message\Request::__clone
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testWithMethodCreatesNewInstance()
{
@ -188,9 +153,6 @@ class RequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\Request::withRequestTarget
* @covers WellRESTed\Message\Request::getRequestTarget
* @uses WellRESTed\Message\Request::__clone
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testWithRequestTargetCreatesNewInstance()
{
@ -202,13 +164,10 @@ class RequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\Request::withUri
* @covers WellRESTed\Message\Request::getUri
* @uses WellRESTed\Message\Request::__clone
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testWithUriCreatesNewInstance()
{
$uri = $this->prophesize("\\Psr\\Http\\Message\\UriInterface");
$uri = $this->prophesize('\Psr\Http\Message\UriInterface');
$uri = $uri->reveal();
$request = new Request();
@ -218,20 +177,13 @@ class RequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\Request::__clone
* @uses WellRESTed\Message\Request::getUri
* @uses WellRESTed\Message\Request::withUri
* @uses WellRESTed\Message\Request::getHeader
* @uses WellRESTed\Message\Request::withHeader
* @uses WellRESTed\Message\Request::getRequestTarget
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testWithUriPreservesOriginalRequest()
{
$uri1 = $this->prophesize("\\Psr\\Http\\Message\\UriInterface");
$uri1 = $this->prophesize('\Psr\Http\Message\UriInterface');
$uri1 = $uri1->reveal();
$uri2 = $this->prophesize("\\Psr\\Http\\Message\\UriInterface");
$uri2 = $this->prophesize('\Psr\Http\Message\UriInterface');
$uri2 = $uri2->reveal();
$request1 = new Request();
@ -242,9 +194,9 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$request2 = $request2->withHeader("Accept", "text/plain");
$this->assertEquals($uri1, $request1->getUri());
$this->assertEquals("application/json", $request1->getHeader("Accept"));
$this->assertEquals(["application/json"], $request1->getHeader("Accept"));
$this->assertEquals($uri2, $request2->getUri());
$this->assertEquals("text/plain", $request2->getHeader("Accept"));
$this->assertEquals(["text/plain"], $request2->getHeader("Accept"));
}
}

View File

@ -4,14 +4,16 @@ namespace WellRESTed\Test\Unit\Message;
use WellRESTed\Message\Response;
/**
* @uses WellRESTed\Message\Response
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
class ResponseTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers WellRESTed\Message\Response::withStatus
* @covers WellRESTed\Message\Response::getStatusCode
* @uses WellRESTed\Message\Response::__clone
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testCreatesNewInstanceWithStatusCode()
{
@ -23,9 +25,6 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\Response::withStatus
* @covers WellRESTed\Message\Response::getReasonPhrase
* @uses WellRESTed\Message\Response::__clone
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
* @dataProvider statusProvider
*/
public function testCreatesNewInstanceWithReasonPhrase($code, $reasonPhrase, $expected)
@ -83,9 +82,6 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\Response::withStatus
* @covers WellRESTed\Message\Response::getStatusCode
* @uses WellRESTed\Message\Response::__clone
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testWithStatusCodePreservesOriginalResponse()
{
@ -96,11 +92,10 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
$response2 = $response1->withStatus(404);
$response2 = $response2->withHeader("Content-type", "text/plain");
$this->assertEquals(200, $response1->getStatusCode());
$this->assertEquals("application/json", $response1->getHeader("Content-type"));
$this->assertEquals(["application/json"], $response1->getHeader("Content-type"));
$this->assertEquals(404, $response2->getStatusCode());
$this->assertEquals("text/plain", $response2->getHeader("Content-type"));
$this->assertEquals(["text/plain"], $response2->getHeader("Content-type"));
}
}

View File

@ -105,7 +105,7 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
public function testServerRequestProvidesHeaders($request)
{
/** @var ServerRequest $request */
$this->assertEquals("application/json", $request->getHeader("Accept"));
$this->assertEquals(["application/json"], $request->getHeader("Accept"));
}
public function testServerRequestProvidesBody()