Skip to content
Permalink
Browse files

Implement Request::header()

  • Loading branch information...
markstory committed Dec 21, 2012
1 parent a5ceed1 commit 8c65b15d742540032a5b4de050637796ed19ae4f
Showing with 71 additions and 9 deletions.
  1. +52 −0 lib/Cake/Network/Http/Request.php
  2. +19 −9 lib/Cake/Test/TestCase/Network/Http/RequestTest.php
@@ -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;
@@ -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.
You can’t perform that action at this time.