Restructured CakeResponse::header() to avoid multiple returns, assignments, and calls to itself

Added ability to send multiple values with the same type of header in CakeResponse
This commit is contained in:
Tigran Gabrielyan 2013-08-06 01:31:03 -07:00
parent a54c92fc0f
commit a6e3bb37a6
2 changed files with 26 additions and 21 deletions

View file

@ -416,8 +416,10 @@ class CakeResponse {
$this->_setContent();
$this->_setContentLength();
$this->_setContentType();
foreach ($this->_headers as $header => $value) {
$this->_sendHeader($header, $value);
foreach ($this->_headers as $header => $values) {
foreach((array)$values as $value) {
$this->_sendHeader($header, $value);
}
}
if ($this->_file) {
$this->_sendFile($this->_file, $this->_fileRange);
@ -556,31 +558,25 @@ class CakeResponse {
* @param string|array $header. An array of header strings or a single header string
* - an associative array of "header name" => "header value" is also accepted
* - an array of string headers is also accepted
* @param string $value. The header value.
* @param string|array $value. The header value(s)
* @return array list of headers to be sent
*/
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);
if (!is_array($header)) {
$header = array($header => $value);
}
foreach ($header as $h => $v) {
if (is_numeric($h)) {
list($h, $v) = array($v, null);
}
return $this->_headers;
if (is_null($v)) {
list($h, $v) = explode(':', $h, 2);
}
$this->_headers[$h] = (is_array($v)) ? array_map('trim', $v) : trim($v);
}
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;
}

View file

@ -172,6 +172,10 @@ class CakeResponseTest extends CakeTestCase {
$response->header(array('Content-Encoding: gzip', 'Vary: *', 'Pragma' => 'no-cache'));
$headers += array('Content-Encoding' => 'gzip', 'Vary' => '*', 'Pragma' => 'no-cache');
$this->assertEquals($response->header(), $headers);
$response->header('Access-Control-Allow-Origin', array('domain1', 'domain2'));
$headers += array('Access-Control-Allow-Origin' => array('domain1', 'domain2'));
$this->assertEquals($response->header(), $headers);
}
/**
@ -182,7 +186,8 @@ class CakeResponseTest extends CakeTestCase {
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent', '_setCookies'));
$response->header(array(
'Content-Language' => 'es',
'WWW-Authenticate' => 'Negotiate'
'WWW-Authenticate' => 'Negotiate',
'Access-Control-Allow-Origin' => array('domain1', 'domain2'),
));
$response->body('the response body');
$response->expects($this->once())->method('_sendContent')->with('the response body');
@ -194,8 +199,12 @@ class CakeResponseTest extends CakeTestCase {
$response->expects($this->at(3))
->method('_sendHeader')->with('WWW-Authenticate', 'Negotiate');
$response->expects($this->at(4))
->method('_sendHeader')->with('Content-Length', 17);
->method('_sendHeader')->with('Access-Control-Allow-Origin', 'domain1');
$response->expects($this->at(5))
->method('_sendHeader')->with('Access-Control-Allow-Origin', 'domain2');
$response->expects($this->at(6))
->method('_sendHeader')->with('Content-Length', 17);
$response->expects($this->at(7))
->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8');
$response->send();
}