Added server request test against content headers.
Updated getServerRequestHeaders to be more efficient and accurate. Wrong variable used in dependency injection documentation.
This commit is contained in:
parent
17c58ae362
commit
a1a0dc0f45
|
|
@ -14,7 +14,7 @@ Imaging we have a ``FooHandler`` that depends on a ``BarInterface``, and ``BazIn
|
||||||
private $bar;
|
private $bar;
|
||||||
private $baz;
|
private $baz;
|
||||||
|
|
||||||
public function __construct(BarInterface $bar, BazInterface $bar)
|
public function __construct(BarInterface $bar, BazInterface $baz)
|
||||||
{
|
{
|
||||||
$this->bar = $bar;
|
$this->bar = $bar;
|
||||||
$this->baz = $baz;
|
$this->baz = $baz;
|
||||||
|
|
|
||||||
|
|
@ -479,25 +479,28 @@ class ServerRequest extends Request implements ServerRequestInterface
|
||||||
*/
|
*/
|
||||||
protected function getServerRequestHeaders()
|
protected function getServerRequestHeaders()
|
||||||
{
|
{
|
||||||
if (!function_exists('apache_get_version')) {
|
// http://www.php.net/manual/en/function.getallheaders.php#84262
|
||||||
// http://www.php.net/manual/en/function.getallheaders.php#84262
|
$headers = array();
|
||||||
$headers = array();
|
foreach ($_SERVER as $name => $value) {
|
||||||
foreach ($_SERVER as $name => $value) {
|
if (substr($name, 0, 5) === "HTTP_") {
|
||||||
if (substr($name, 0, 5) === "HTTP_") {
|
$name = $this->normalizeHeaderName(substr($name, 5));
|
||||||
$headers[str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($name, 5)))))] = $value;
|
$headers[$name] = $value;
|
||||||
}
|
} elseif ($name === "CONTENT_LENGTH" || $name === "CONTENT_TYPE") {
|
||||||
}
|
$name = $this->normalizeHeaderName($name);
|
||||||
return $headers;
|
$headers[$name] = $value;
|
||||||
}
|
|
||||||
else {
|
|
||||||
$headers = apache_request_headers();
|
|
||||||
if (is_array($headers)) {
|
|
||||||
return $headers;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return array();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return $headers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function normalizeHeaderName($name)
|
||||||
|
{
|
||||||
|
$name = ucwords(strtolower(str_replace("_", " ", $name)));
|
||||||
|
return str_replace(" ", "-", $name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -121,6 +121,20 @@ class ServerRequestTest extends TestCase
|
||||||
$this->assertEquals(["application/json"], $request->getHeader("Accept"));
|
$this->assertEquals(["application/json"], $request->getHeader("Accept"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @backupGlobals enabled
|
||||||
|
*/
|
||||||
|
public function testGetServerRequestReadsContentHeaders()
|
||||||
|
{
|
||||||
|
$_SERVER = [
|
||||||
|
"CONTENT_LENGTH" => "1024",
|
||||||
|
"CONTENT_TYPE" => "application/json"
|
||||||
|
];
|
||||||
|
$request = ServerRequest::getServerRequest();
|
||||||
|
$this->assertEquals("1024", $request->getHeaderLine("Content-length"));
|
||||||
|
$this->assertEquals("application/json", $request->getHeaderLine("Content-type"));
|
||||||
|
}
|
||||||
|
|
||||||
public function testGetServerRequestReadsBody()
|
public function testGetServerRequestReadsBody()
|
||||||
{
|
{
|
||||||
$body = new NullStream();
|
$body = new NullStream();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue