Skip to content

Commit

Permalink
Start implementing getHeader() and getHeaderLine()
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Sep 20, 2016
1 parent a6a3760 commit 6501fd9
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/Network/Request.php
Expand Up @@ -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.
*
Expand Down
46 changes: 46 additions & 0 deletions 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 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
*
Expand Down

0 comments on commit 6501fd9

Please sign in to comment.