Skip to content
Permalink
Browse files

Start implementing getHeader() and getHeaderLine()

  • Loading branch information...
markstory committed Sep 20, 2016
1 parent a6a3760 commit 6501fd927635ddfe060168307dabee99325dd94d
Showing with 132 additions and 0 deletions.
  1. +86 −0 src/Network/Request.php
  2. +46 −0 tests/TestCase/Network/RequestTest.php
@@ -959,6 +959,92 @@ public function header($name)
return $this->env($name);
}
/**
* Get all headers in the request.
*
* Returns an associative array where the header names are
* the keys and the values are a list of header values.
*
* While header names are not case-sensitive, getHeaders() will preserve the
* exact case in which headers were originally specified.
*
* @return array An associative array of headers and their values.
*/
public function getHeaders()
{
}
/**
* Get a single header from the request.
*
* Return the header value as an array. If the header
* is not present an empty array will be returned.
*
* @param string $name The header you want to get (case-insensitive)
* @return array An associative array of headers and their values.
* If the header doesn't exist, an empty array will be returned.
*/
public function getHeader($name)
{
$name = str_replace('-', '_', strtoupper($name));
if (!in_array($name, ['CONTENT_LENGTH', 'CONTENT_TYPE'])) {
$name = 'HTTP_' . $name;
}
if (isset($this->_environment[$name])) {
return (array)$this->_environment[$name];
}
return [];
}
/**
* Get a single header as a string from the request.
*
* Return an array of header values
*
* @param string $name The header you want to get (case-insensitive)
* @return string Header values collapsed into a comma separated string.
*/
public function getHeaderLine($name)
{
$value = $this->getHeader($name);
return implode(', ', $value);
}
/**
* Get a modified request with the provided header.
*
* @param string $name The header name.
* @param string|array $value The header value
* @return static
*/
public function withHeader($name, $value)
{
}
/**
* Get a modified request with the provided header.
*
* Existing header values will be retained. The provided value
* will be appended into the existing values.
*
* @param string $name The header name.
* @param string|array $value The header value
* @return static
*/
public function withAddedHeader($name, $value)
{
}
/**
* Get a modified request without a provided header.
*
* @param string $name The header name.
* @return static
*/
public function withoutHeader($name)
{
}
/**
* Get the HTTP method used for this request.
*
@@ -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 testGetHeader()
{
$request = new Request(['environment' => [
'HTTP_HOST' => 'localhost',
'CONTENT_TYPE' => 'application/json',
'CONTENT_LENGTH' => 1337,
'HTTP_CONTENT_MD5' => 'abc123',
'HTTP_DOUBLE' => ['a', 'b']
]]);
$this->assertEquals([], $request->getHeader('Not-there'));
$expected = [$request->env('HTTP_HOST')];
$this->assertEquals($expected, $request->getHeader('Host'));
$this->assertEquals($expected, $request->getHeader('host'));
$this->assertEquals($expected, $request->getHeader('HOST'));
$this->assertEquals(['a', 'b'], $request->getHeader('Double'));
}
/**
* Test getting headers with psr7 methods
*
* @return void
*/
public function testGetHeaderLine()
{
$request = new Request(['environment' => [
'HTTP_HOST' => 'localhost',
'CONTENT_TYPE' => 'application/json',
'CONTENT_LENGTH' => 1337,
'HTTP_CONTENT_MD5' => 'abc123',
'HTTP_DOUBLE' => ['a', 'b']
]]);
$this->assertEquals('', $request->getHeaderLine('Authorization'));
$expected = $request->env('CONTENT_LENGTH');
$this->assertEquals($expected, $request->getHeaderLine('Content-Length'));
$this->assertEquals($expected, $request->getHeaderLine('content-Length'));
$this->assertEquals($expected, $request->getHeaderLine('ConTent-LenGth'));
$this->assertEquals('a, b', $request->getHeaderLine('Double'));
}
/**
* Test accepts() with and without parameters
*

0 comments on commit 6501fd9

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