Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement Request::header()
  • Loading branch information
markstory committed Dec 21, 2012
1 parent a5ceed1 commit 8c65b15
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
52 changes: 52 additions & 0 deletions lib/Cake/Network/Http/Request.php
Expand Up @@ -66,10 +66,62 @@ public function url($url = null) {
return $this;
}

/**
* Get/Set headers into the request.
*
* You can get the value of a header, or set one/many headers.
* Headers are set / fetched in a case insensitive way.
*
* ### Getting headers
*
* `$request->header('Content-Type');`
*
* ### Setting one header
*
* `$request->header('Content-Type', 'application/json');`
*
* ### Setting multiple headers
*
* `$request->header(['Connection' => 'close', 'User-Agent' => 'CakePHP']);`
*
* @param string|array $name The name to get, or array of multiple values to set.
* @param string $value The value to set for the header.
* @return mixed Either $this when setting or header value when getting.
*/
public function header($name = null, $value = null) {
if ($value === null && is_string($name)) {
$name = $this->_normalizeHeader($name);
return isset($this->_headers[$name]) ? $this->_headers[$name] : null;
}
if ($value !== null && !is_array($name)) {
$name = [$name => $value];
}
foreach ($name as $key => $val) {
$key = $this->_normalizeHeader($key);
$this->_headers[$key] = $val;
}
return $this;
}

/**
* Normalize header names to Camel-Case form.
*
* @param string $name The header name to normalize.
* @return string Normalized header name.
*/
protected function _normalizeHeader($name) {
$parts = explode('-', $name);
$parts = array_map('strtolower', $parts);
$parts = array_map('ucfirst', $parts);
return implode('-', $parts);
}

/**
* Get/set the content or body for the request.
*
* @param string|null $content The content for the request. Leave null for get
* @return mixed Either $this or the content value.
*/
public function content($content = null) {
if ($content === null) {
return $this->_content;
Expand Down
28 changes: 19 additions & 9 deletions lib/Cake/Test/TestCase/Network/Http/RequestTest.php
Expand Up @@ -75,16 +75,26 @@ public function testContent() {
* @return void
*/
public function testHeader() {
$this->markTestIncomplete();
}
$request = new Request();
$type = 'application/json';
$result = $request->header('Content-Type', $type);
$this->assertSame($result, $request, 'Should return self');

/**
* test headers being case insenstive
*
* @return void
*/
public function testHeaderInsensitive() {
$this->markTestIncomplete();
$result = $request->header('content-type');
$this->assertEquals($type, $result, 'lowercase does not work');

$result = $request->header('ConTent-typE');
$this->assertEquals($type, $result, 'Funny casing does not work');

$result = $request->header([
'Connection' => 'close',
'user-agent' => 'CakePHP'
]);
$this->assertSame($result, $request, 'Should return self');

$this->assertEquals('close', $request->header('connection'));
$this->assertEquals('CakePHP', $request->header('USER-AGENT'));
$this->assertNull($request->header('not set'));
}

}

0 comments on commit 8c65b15

Please sign in to comment.