ServerRequest creates stream wrapping php://input for body on creation

This commit is contained in:
PJ Dietz 2015-04-01 19:51:18 -04:00
parent a5cb481d79
commit 4502df5c1c
3 changed files with 72 additions and 111 deletions

View File

@ -10,9 +10,9 @@ abstract class Message implements MessageInterface
/** @var HeaderCollection */
protected $headers;
/** @var StreamableInterface */
private $body;
protected $body;
/** @var string */
private $protcolVersion = "1.1";
protected $protcolVersion = "1.1";
public function __construct()
{

View File

@ -3,16 +3,11 @@
namespace WellRESTed\Message;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamableInterface;
use WellRESTed\Stream\StreamStream;
class ServerRequest extends Request implements ServerRequestInterface
{
/**
* Singleton instance derived from reading the request sent to the server.
*
* @var self
* @static
*/
static private $serverRequest;
/** @var array */
private $attributes;
/** @var array */
@ -294,6 +289,28 @@ class ServerRequest extends Request implements ServerRequestInterface
parent::__clone();
}
protected function readFromServerRequest(array $attributes = null)
{
$this->attributes = $attributes ?: [];
$this->serverParams = $_SERVER;
$this->cookieParams = $_COOKIE;
$this->fileParams = $_FILES;
$this->queryParams = [];
if (isset($_SERVER["QUERY_STRING"])) {
parse_str($_SERVER["QUERY_STRING"], $this->queryParams);
}
$headers = $this->getServerRequestHeaders();
foreach ($headers as $key => $value) {
$this->headers[$key] = $value;
}
$this->body = $this->getStreamForBody();
$contentType = $this->getHeader("Content-type");
if ($contentType === "application/x-www-form-urlencoded" || $contentType === "multipart/form-data") {
$this->parsedBody = $_POST;
}
}
/**
* Return a reference to the singleton instance of the Request derived
* from the server's information about the request sent to the server.
@ -303,32 +320,29 @@ class ServerRequest extends Request implements ServerRequestInterface
*/
public static function getServerRequest(array $attributes = null)
{
$request = new self();
$request->attributes = $attributes ?: [];
$request->serverParams = $_SERVER;
$request->cookieParams = $_COOKIE;
$request->fileParams = $_FILES;
$request->queryParams = [];
if (isset($_SERVER["QUERY_STRING"])) {
parse_str($_SERVER["QUERY_STRING"], $request->queryParams);
}
$headers = self::getServerRequestHeaders();
foreach ($headers as $key => $value) {
$request->headers[$key] = $value;
}
$contentType = $request->getHeader("Content-type");
if ($contentType === "application/x-www-form-urlencoded" || $contentType === "multipart/form-data") {
$request->parsedBody = $_POST;
}
$request = new static();
$request->readFromServerRequest($attributes);
return $request;
}
/**
* Return a stream representing the request's body.
*
* Override this method to use a specific StreamableInterface implementation.
*
* @return StreamableInterface
*/
protected function getStreamForBody()
{
return new StreamStream(fopen("php://input", "r"));
}
/**
* Read and return all request headers from the request issued to the server.
*
* @return array Associative array of headers
*/
private static function getServerRequestHeaders()
protected function getServerRequestHeaders()
{
// Prefer apache_request_headers is available.
if (function_exists("apache_request_headers")) {

View File

@ -4,12 +4,17 @@ namespace WellRESTed\Test\Unit\Message;
use WellRESTed\Message\ServerRequest;
/**
* @uses WellRESTed\Message\ServerRequest
* @uses WellRESTed\Message\Request
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
* @uses WellRESTed\Stream\StreamStream
*/
class ServerRequestTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers WellRESTed\Message\ServerRequest::__construct
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testCreatesInstance()
{
@ -20,11 +25,8 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\ServerRequest::getServerRequest
* @covers WellRESTed\Message\ServerRequest::getServerRequestHeaders
* @uses WellRESTed\Message\ServerRequest::__construct
* @uses WellRESTed\Message\ServerRequest::__clone
* @uses WellRESTed\Message\Request
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
* @covers WellRESTed\Message\ServerRequest::readFromServerRequest
* @covers WellRESTed\Message\ServerRequest::getStreamForBody
* @preserveGlobalState disabled
*/
public function testGetServerRequestReadsFromRequest()
@ -50,7 +52,8 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
$_POST = [
"dog" => "Bear"
];
$request = ServerRequest::getServerRequest(["guinea_pig" => "Claude"]);
$attributes = ["guinea_pig" => "Claude"];
$request = ServerRequest::getServerRequest($attributes);
$this->assertNotNull($request);
return $request;
}
@ -97,9 +100,6 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\ServerRequest::getHeader
* @uses WellRESTed\Message\Request
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
* @depends testGetServerRequestReadsFromRequest
*/
public function testServerRequestProvidesHeaders($request)
@ -108,11 +108,16 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
$this->assertEquals("application/json", $request->getHeader("Accept"));
}
public function testServerRequestProvidesBody()
{
$body = $this->prophesize('Psr\Http\Message\StreamableInterface');
MockServerRequestTest::$bodyStream = $body->reveal();
$request = MockServerRequestTest::getServerRequest();
$this->assertSame($body->reveal(), $request->getBody());
}
/**
* @covers WellRESTed\Message\ServerRequest::getAttribute
* @uses WellRESTed\Message\Request
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
* @depends testGetServerRequestReadsFromRequest
*/
public function testServerRequestProvidesAttributesIfPassed($request)
@ -123,10 +128,6 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\ServerRequest::withCookieParams
* @uses WellRESTed\Message\ServerRequest::getCookieParams
* @uses WellRESTed\Message\ServerRequest::__clone
* @uses WellRESTed\Message\Request
* @uses WellRESTed\Message\Message
* @depends testGetServerRequestReadsFromRequest
*/
public function testWithCookieParamsCreatesNewInstance($request1)
@ -141,10 +142,6 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\ServerRequest::withQueryParams
* @uses WellRESTed\Message\ServerRequest::getQueryParams
* @uses WellRESTed\Message\ServerRequest::__clone
* @uses WellRESTed\Message\Request
* @uses WellRESTed\Message\Message
* @depends testGetServerRequestReadsFromRequest
*/
public function testWithQueryParamsCreatesNewInstance($request1)
@ -159,11 +156,6 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\ServerRequest::withParsedBody
* @uses WellRESTed\Message\ServerRequest::getParsedBody
* @uses WellRESTed\Message\ServerRequest::getQueryParams
* @uses WellRESTed\Message\ServerRequest::__clone
* @uses WellRESTed\Message\Request
* @uses WellRESTed\Message\Message
* @depends testGetServerRequestReadsFromRequest
*/
public function testWithParsedBodyCreatesNewInstance($request1)
@ -183,12 +175,6 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\ServerRequest::getServerRequest
* @covers WellRESTed\Message\ServerRequest::getParsedBody
* @uses WellRESTed\Message\ServerRequest::__construct
* @uses WellRESTed\Message\ServerRequest::__clone
* @uses WellRESTed\Message\ServerRequest::getServerRequestHeaders
* @uses WellRESTed\Message\Request
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
* @preserveGlobalState disabled
* @dataProvider formContentTypeProvider
*/
@ -217,12 +203,6 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\ServerRequest::__clone
* @uses WellRESTed\Message\ServerRequest::__construct
* @uses WellRESTed\Message\ServerRequest::withParsedBody
* @uses WellRESTed\Message\ServerRequest::getParsedBody
* @uses WellRESTed\Message\Request
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testCloneMakesDeepCopiesOfParsedBody()
{
@ -240,13 +220,6 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\ServerRequest::withAttribute
* @covers WellRESTed\Message\ServerRequest::getAttribute
* @uses WellRESTed\Message\ServerRequest::__construct
* @uses WellRESTed\Message\ServerRequest::__clone
* @uses WellRESTed\Message\ServerRequest::withParsedBody
* @uses WellRESTed\Message\ServerRequest::getParsedBody
* @uses WellRESTed\Message\Request
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testWithAttributeCreatesNewInstance()
{
@ -257,14 +230,6 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\ServerRequest::withAttribute
* @uses WellRESTed\Message\ServerRequest::getAttribute
* @uses WellRESTed\Message\ServerRequest::__construct
* @uses WellRESTed\Message\ServerRequest::__clone
* @uses WellRESTed\Message\ServerRequest::withParsedBody
* @uses WellRESTed\Message\ServerRequest::getParsedBody
* @uses WellRESTed\Message\Request
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testWithAttributePreserversOtherAttributes()
{
@ -277,10 +242,6 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\ServerRequest::getAttribute
* @uses WellRESTed\Message\ServerRequest::__construct
* @uses WellRESTed\Message\Request
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testGetAttributeReturnsDefaultIfNotSet()
{
@ -290,15 +251,6 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\ServerRequest::withoutAttribute
* @uses WellRESTed\Message\ServerRequest::withAttribute
* @uses WellRESTed\Message\ServerRequest::getAttribute
* @uses WellRESTed\Message\ServerRequest::__construct
* @uses WellRESTed\Message\ServerRequest::__clone
* @uses WellRESTed\Message\ServerRequest::withParsedBody
* @uses WellRESTed\Message\ServerRequest::getParsedBody
* @uses WellRESTed\Message\Request
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testWithoutAttributeCreatesNewInstance()
{
@ -310,15 +262,6 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\ServerRequest::withoutAttribute
* @uses WellRESTed\Message\ServerRequest::withAttribute
* @uses WellRESTed\Message\ServerRequest::getAttribute
* @uses WellRESTed\Message\ServerRequest::__construct
* @uses WellRESTed\Message\ServerRequest::__clone
* @uses WellRESTed\Message\ServerRequest::withParsedBody
* @uses WellRESTed\Message\ServerRequest::getParsedBody
* @uses WellRESTed\Message\Request
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testWithoutAttributePreservesOtherAttributes()
{
@ -332,14 +275,6 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
/**
* @covers WellRESTed\Message\ServerRequest::getAttributes
* @uses WellRESTed\Message\ServerRequest::withAttribute
* @uses WellRESTed\Message\ServerRequest::__construct
* @uses WellRESTed\Message\ServerRequest::__clone
* @uses WellRESTed\Message\ServerRequest::withParsedBody
* @uses WellRESTed\Message\ServerRequest::getParsedBody
* @uses WellRESTed\Message\Request
* @uses WellRESTed\Message\Message
* @uses WellRESTed\Message\HeaderCollection
*/
public function testGetAttributesReturnsAllAttributes()
{
@ -351,3 +286,15 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
$this->assertEquals("Bear", $attributes["dog"]);
}
}
// ----------------------------------------------------------------------------
class MockServerRequestTest extends ServerRequest
{
public static $bodyStream;
protected function getStreamForBody()
{
return self::$bodyStream;
}
}