Skip to content

Commit

Permalink
Implement getHeaders() and hasHeader()
Browse files Browse the repository at this point in the history
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 7e83776
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
32 changes: 30 additions & 2 deletions src/Network/Request.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
47 changes: 46 additions & 1 deletion tests/TestCase/Network/RequestTest.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 7e83776

Please sign in to comment.