Skip to content
Permalink
Browse files

Implement getHeaders() and hasHeader()

Implement the remaining header methods. Use hasHeader() and
getHeaderLine internally to prevent mutation of the request
as it builds internal state where possible.
  • Loading branch information...
markstory committed Sep 24, 2016
1 parent dcedc0c commit 7e8377664afa09a6e1af1703aabc590eb1bb6fad
Showing with 76 additions and 3 deletions.
  1. +30 −2 src/Network/Request.php
  2. +46 −1 tests/TestCase/Network/RequestTest.php
@@ -367,8 +367,8 @@ protected function _processPost($data)
$data = $this->input();
parse_str($data, $data);
}
if ($this->env('HTTP_X_HTTP_METHOD_OVERRIDE')) {
$data['_method'] = $this->env('HTTP_X_HTTP_METHOD_OVERRIDE');
if ($this->hasHeader('X-Http-Method-Override')) {
$data['_method'] = $this->getHeaderLine('X-Http-Method_Override');
$override = true;
}
$this->_environment['ORIGINAL_REQUEST_METHOD'] = $method;
@@ -983,6 +983,34 @@ public function header($name)
*/
public function getHeaders()
{
$headers = [];
foreach ($this->_environment as $key => $value) {
$name = null;
if (strpos($key, 'HTTP_') === 0) {
$name = substr($key, 5);
}
if (strpos($key, 'CONTENT_') === 0) {
$name = $key;
}
if ($name !== null) {
$name = strtr(strtolower($name), '_', ' ');
$name = strtr(ucwords($name), ' ', '-');
$headers[$name] = (array)$value;
}
}
return $headers;
}
/**
* Check if a header exists in the request.
*
* @param string $name The header you want to get (case-insensitive)
* @return bool Whether or not the header is defined.
*/
public function hasHeader($name)
{
$name = $this->normalizeHeaderName($name);
return array_key_exists($name, $this->_environment);
}
/**
@@ -1282,6 +1282,52 @@ public function testHeader()
$this->assertEquals($request->env('HTTP_CONTENT_MD5'), $request->header('content-md5'));
}
/**
* Test getting headers with psr7 methods
*
* @return void
*/
public function testGetHeaders()
{
$request = new Request(['environment' => [
'HTTP_HOST' => 'localhost',
'CONTENT_TYPE' => 'application/json',
'CONTENT_LENGTH' => 1337,
'HTTP_CONTENT_MD5' => 'abc123',
'HTTP_DOUBLE' => ['a', 'b']
]]);
$headers = $request->getHeaders();
$expected = [
'Host' => ['localhost'],
'Content-Type' => ['application/json'],
'Content-Length' => [1337],
'Content-Md5' => ['abc123'],
'Double' => ['a', 'b'],
];
$this->assertEquals($expected, $headers);
}
/**
* Test hasHeader
*
* @return void
*/
public function testHasHeader()
{
$request = new Request(['environment' => [
'HTTP_HOST' => 'localhost',
'CONTENT_TYPE' => 'application/json',
'CONTENT_LENGTH' => 1337,
'HTTP_CONTENT_MD5' => 'abc123',
'HTTP_DOUBLE' => ['a', 'b']
]]);
$this->assertTrue($request->hasHeader('Host'));
$this->assertTrue($request->hasHeader('Content-Type'));
$this->assertTrue($request->hasHeader('Content-MD5'));
$this->assertTrue($request->hasHeader('Double'));
$this->assertFalse($request->hasHeader('Authorization'));
}
/**
* Test getting headers with psr7 methods
*
@@ -2440,7 +2486,6 @@ public function testGetServerParams()
$expected = $vars + [
'CONTENT_TYPE' => null,
'HTTP_CONTENT_TYPE' => null,
'HTTP_X_HTTP_METHOD_OVERRIDE' => null,
'ORIGINAL_REQUEST_METHOD' => 'PUT',
];
$this->assertSame($expected, $request->getServerParams());

0 comments on commit 7e83776

Please sign in to comment.
You can’t perform that action at this time.