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:
Joe Ginley 2020-02-05 18:43:06 -05:00
parent 17c58ae362
commit a1a0dc0f45
3 changed files with 35 additions and 18 deletions

View File

@ -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;

View File

@ -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_") {
$headers[str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($name, 5)))))] = $value; $name = $this->normalizeHeaderName(substr($name, 5));
$headers[$name] = $value;
} elseif ($name === "CONTENT_LENGTH" || $name === "CONTENT_TYPE") {
$name = $this->normalizeHeaderName($name);
$headers[$name] = $value;
} }
} }
return $headers; return $headers;
} }
else {
$headers = apache_request_headers(); /**
if (is_array($headers)) { * @param string $name
return $headers; * @return string
} */
else { private function normalizeHeaderName($name)
return array(); {
} $name = ucwords(strtolower(str_replace("_", " ", $name)));
} return str_replace(" ", "-", $name);
} }
/** /**

View File

@ -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();