Skip to content

Commit

Permalink
Implementing CakeResponse::header() method
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 31, 2010
1 parent a2eac24 commit 2a4b30d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cake/libs/cake_request.php
Expand Up @@ -485,7 +485,7 @@ public function is($type) {
*
* Pattern value comparison allows you to compare a value fetched from `env()` to a regular expression.
*
* e.g `addDetector('iphone', array('env' => 'HTTP_USER_AGENT', 'pattern' => '/iPhone/i'));
* e.g `addDetector('iphone', array('env' => 'HTTP_USER_AGENT', 'pattern' => '/iPhone/i'));`
*
* ### Option based comparison
*
Expand Down
47 changes: 45 additions & 2 deletions cake/libs/cake_response.php
Expand Up @@ -346,11 +346,54 @@ public function sendHeaders() {

/**
* Buffers a header string to be sent
* Returns the complete list of buffered headers
*
* ### Single header
* e.g `header('Location', 'http://example.com');`
*
* ### Multiple headers
* e.g `header(array('Location' => 'http://example.com', 'X-Extra' => 'My header'));`
*
* ### String header
* e.g `header('WWW-Authenticate: Negotiate');`
*
* ### Array of string headers
* e.g `header(array('WWW-Authenticate: Negotiate'), array('Content-type: application/pdf'));`
*
* Multiple calls for setting the same header name will have the same effect as setting the header once
* with the last value sent for it
* e.g `header('WWW-Authenticate: Negotiate'); header('WWW-Authenticate: Not-Negotiate');`
* will have the same effect as only doing `header('WWW-Authenticate: Not-Negotiate');`
*
* @param mixed $header. An array of header strings or a single header string
* - an assotiative array of "header name" => "header value" is also accepted
* - an array of string headers is also accepted
* @param mixed $value. The header value.
* @return array list of headers to be sent
*/
public function header($header) {

public function header($header = null, $value = null) {
if (is_null($header)) {
return $this->_headers;
}
if (is_array($header)) {
foreach ($header as $h => $v) {
if (is_numeric($h)) {
$this->header($v);
continue;
}
$this->_headers[$h] = trim($v);
}
return $this->_headers;
}

if (!is_null($value)) {
$this->_headers[$header] = $value;
return $this->_headers;
}

list($header, $value) = explode(':', $header, 2);
$this->_headers[$header] = trim($value);
return $this->_headers;
}

/**
Expand Down
44 changes: 44 additions & 0 deletions cake/tests/cases/libs/cake_response.test.php
Expand Up @@ -84,4 +84,48 @@ public function testType() {
$this->assertEquals($response->type('xhtml-mobile'), 'application/vnd.wap.xhtml+xml');
$this->assertEquals($response->type('csv'), 'text/csv');
}

/**
* Tests the header method
*
*/
public function testHeader() {
$response = new CakeResponse();
$headers = array();
$this->assertEquals($response->header(), $headers);

$response->header('Location', 'http://example.com');
$headers += array('Location' => 'http://example.com');
$this->assertEquals($response->header(), $headers);

//Headers with the same name are overwritten
$response->header('Location', 'http://example2.com');
$headers = array('Location' => 'http://example2.com');
$this->assertEquals($response->header(), $headers);

$response->header(array('WWW-Authenticate' => 'Negotiate'));
$headers += array('WWW-Authenticate' => 'Negotiate');
$this->assertEquals($response->header(), $headers);

$response->header(array('WWW-Authenticate' => 'Not-Negotiate'));
$headers['WWW-Authenticate'] = 'Not-Negotiate';
$this->assertEquals($response->header(), $headers);

$response->header(array('Age' => 12, 'Allow' => 'GET, HEAD'));
$headers += array('Age' => 12, 'Allow' => 'GET, HEAD');
$this->assertEquals($response->header(), $headers);

// String headers are allowed
$response->header('Content-Language: da');
$headers += array('Content-Language' => 'da');
$this->assertEquals($response->header(), $headers);

$response->header('Content-Language: da');
$headers += array('Content-Language' => 'da');
$this->assertEquals($response->header(), $headers);

$response->header(array('Content-Encoding: gzip', 'Vary: *', 'Pragma' => 'no-cache'));
$headers += array('Content-Encoding' => 'gzip', 'Vary' => '*', 'Pragma' => 'no-cache');
$this->assertEquals($response->header(), $headers);
}
}

0 comments on commit 2a4b30d

Please sign in to comment.