ServerRequest::getServerRequest builds URI
This commit is contained in:
parent
4f667f1dda
commit
1b0fccfe0e
|
|
@ -328,6 +328,7 @@ class ServerRequest extends Request implements ServerRequestInterface
|
|||
$this->cookieParams = $_COOKIE;
|
||||
$this->readUploadedFiles($_FILES);
|
||||
$this->queryParams = [];
|
||||
$this->uri = $this->readUri();
|
||||
if (isset($_SERVER["QUERY_STRING"])) {
|
||||
parse_str($_SERVER["QUERY_STRING"], $this->queryParams);
|
||||
}
|
||||
|
|
@ -338,9 +339,6 @@ class ServerRequest extends Request implements ServerRequestInterface
|
|||
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;
|
||||
|
|
@ -379,6 +377,28 @@ class ServerRequest extends Request implements ServerRequestInterface
|
|||
$this->uploadedFiles = $uploadedFiles;
|
||||
}
|
||||
|
||||
protected function readUri()
|
||||
{
|
||||
$uri = "";
|
||||
|
||||
$scheme = "http";
|
||||
if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] && $_SERVER["HTTPS"] !== "off") {
|
||||
$scheme = "https";
|
||||
}
|
||||
|
||||
if (isset($_SERVER["HTTP_HOST"])) {
|
||||
$authority = $_SERVER["HTTP_HOST"];
|
||||
$uri .= "$scheme://$authority";
|
||||
}
|
||||
|
||||
// Path and query string
|
||||
if (isset($_SERVER["REQUEST_URI"])) {
|
||||
$uri .= $_SERVER["REQUEST_URI"];
|
||||
}
|
||||
|
||||
return new Uri($uri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a reference to the singleton instance of the Request derived
|
||||
* from the server's information about the request sent to the server.
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ namespace WellRESTed\Test\Unit\Message;
|
|||
|
||||
use WellRESTed\Message\ServerRequest;
|
||||
use WellRESTed\Message\UploadedFile;
|
||||
use WellRESTed\Message\Uri;
|
||||
|
||||
/**
|
||||
* @uses WellRESTed\Message\ServerRequest
|
||||
|
|
@ -601,6 +602,57 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertEquals("Molly", $attributes["cat"]);
|
||||
$this->assertEquals("Bear", $attributes["dog"]);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// URI
|
||||
|
||||
/**
|
||||
* @covers WellRESTed\Message\ServerRequest::getServerRequest
|
||||
* @covers WellRESTed\Message\ServerRequest::getServerRequestHeaders
|
||||
* @covers WellRESTed\Message\ServerRequest::readFromServerRequest
|
||||
* @covers WellRESTed\Message\ServerRequest::readUri
|
||||
* @preserveGlobalState disabled
|
||||
* @dataProvider uriProvider
|
||||
*/
|
||||
public function testGetServerRequestProvidesUri($expected, $server)
|
||||
{
|
||||
$_SERVER = $server;
|
||||
$request = ServerRequest::getServerRequest();
|
||||
$this->assertEquals($expected, $request->getUri());
|
||||
}
|
||||
|
||||
public function uriProvider()
|
||||
{
|
||||
return [
|
||||
[
|
||||
new Uri("http://localhost/path"),
|
||||
[
|
||||
"HTTPS" => "off",
|
||||
"HTTP_HOST" => "localhost",
|
||||
"REQUEST_URI" => "/path",
|
||||
"QUERY_STRING" => ""
|
||||
]
|
||||
],
|
||||
[
|
||||
new Uri("https://foo.com/path/to/stuff?cat=molly"),
|
||||
[
|
||||
"HTTPS" => "1",
|
||||
"HTTP_HOST" => "foo.com",
|
||||
"REQUEST_URI" => "/path/to/stuff?cat=molly",
|
||||
"QUERY_STRING" => "cat=molly"
|
||||
]
|
||||
],
|
||||
[
|
||||
new Uri("http://foo.com:8080/path/to/stuff?cat=molly"),
|
||||
[
|
||||
"HTTP" => "1",
|
||||
"HTTP_HOST" => "foo.com:8080",
|
||||
"REQUEST_URI" => "/path/to/stuff?cat=molly",
|
||||
"QUERY_STRING" => "cat=molly"
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
|
|
|||
Loading…
Reference in New Issue