From 79be20c8268f3bb87655555a333ad75b99b56b2c Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Sun, 12 Apr 2015 14:17:12 -0400 Subject: [PATCH] ServerRequest::getServerRequest reads method, request target, and protocol version from request. --- src/Message/Request.php | 6 +- src/Message/ServerRequest.php | 10 +++ test/tests/unit/Message/ServerRequestTest.php | 78 +++++++++++++++++++ 3 files changed, 91 insertions(+), 3 deletions(-) diff --git a/src/Message/Request.php b/src/Message/Request.php index 4423673..e43f40a 100644 --- a/src/Message/Request.php +++ b/src/Message/Request.php @@ -8,11 +8,11 @@ use Psr\Http\Message\UriInterface; class Request extends Message implements RequestInterface { /** @var string */ - private $method = "GET"; + protected $method = "GET"; /** @var string */ - private $requestTarget; + protected $requestTarget; /** @var UriInterface */ - private $uri; + protected $uri; // ------------------------------------------------------------------------ // Psr\Http\Message\RequestInterface diff --git a/src/Message/ServerRequest.php b/src/Message/ServerRequest.php index bf5597b..08ad4f0 100644 --- a/src/Message/ServerRequest.php +++ b/src/Message/ServerRequest.php @@ -299,6 +299,16 @@ class ServerRequest extends Request implements ServerRequestInterface if (isset($_SERVER["QUERY_STRING"])) { parse_str($_SERVER["QUERY_STRING"], $this->queryParams); } + if (isset($_SERVER["SERVER_PROTOCOL"]) && $_SERVER["SERVER_PROTOCOL"] === "HTTP/1.0") { + // The default is 1.1, so only update if 1.0 + $this->protcolVersion = "1.0"; + } + if (isset($_SERVER["REQUEST_METHOD"])) { + $this->method = $_SERVER["REQUEST_METHOD"]; + } + if (isset($_SERVER["REQUEST_URI"])) { + $this->requestTarget = $_SERVER["REQUEST_URI"]; + } $headers = $this->getServerRequestHeaders(); foreach ($headers as $key => $value) { $this->headers[$key] = $value; diff --git a/test/tests/unit/Message/ServerRequestTest.php b/test/tests/unit/Message/ServerRequestTest.php index ad37698..7bd9f14 100644 --- a/test/tests/unit/Message/ServerRequestTest.php +++ b/test/tests/unit/Message/ServerRequestTest.php @@ -321,6 +321,84 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase $headers = $request->getHeaders(); $this->assertNotNull($headers); } + + /** + * @covers WellRESTed\Message\ServerRequest::readFromServerRequest + * @preserveGlobalState disabled + * @dataProvider protocolVersionProvider + */ + public function testReadsProtocolVersionFromFromRequest($expectedProtocol, $serverProtocol) + { + $_SERVER = [ + "HTTP_HOST" => "localhost", + "SERVER_PROTOCOL" => $serverProtocol, + "REQUEST_METHOD" => "GET" + ]; + $request = ServerRequest::getServerRequest(); + $this->assertEquals($expectedProtocol, $request->getProtocolVersion()); + } + + public function protocolVersionProvider() + { + return [ + ["1.1", "HTTP/1.1"], + ["1.0", "HTTP/1.0"], + ["1.1", null], + ["1.1", "INVALID"] + ]; + } + + /** + * @covers WellRESTed\Message\ServerRequest::readFromServerRequest + * @preserveGlobalState disabled + * @dataProvider methodProvider + */ + public function testReadsMethodFromFromRequest($exectedMethod, $serverMethod) + { + $_SERVER = [ + "HTTP_HOST" => "localhost", + "REQUEST_METHOD" => $serverMethod + ]; + $request = ServerRequest::getServerRequest(); + $this->assertEquals($exectedMethod, $request->getMethod()); + } + + public function methodProvider() + { + return [ + ["GET", "GET"], + ["POST", "POST"], + ["DELETE", "DELETE"], + ["PUT", "PUT"], + ["OPTIONS", "OPTIONS"], + ["GET", null] + ]; + } + + /** + * @covers WellRESTed\Message\ServerRequest::readFromServerRequest + * @preserveGlobalState disabled + * @dataProvider requestTargetProvider + */ + public function testReadsRequestTargetFromServer($exectedRequestTarget, $serverRequestUri) + { + $_SERVER = [ + "HTTP_HOST" => "localhost", + "REQUEST_URI" => $serverRequestUri + ]; + $request = ServerRequest::getServerRequest(); + $this->assertEquals($exectedRequestTarget, $request->getRequestTarget()); + } + + public function requestTargetProvider() + { + return [ + ["/", "/"], + ["/hello", "/hello"], + ["/my/path.txt", "/my/path.txt"], + ["/", null] + ]; + } } // ----------------------------------------------------------------------------