Skip to content

Commit

Permalink
Restructured CakeResponse::header() to avoid multiple returns, assign…
Browse files Browse the repository at this point in the history
…ments, and calls to itself

Added ability to send multiple values with the same type of header in CakeResponse
  • Loading branch information
tigrang committed Aug 6, 2013
1 parent a54c92f commit a6e3bb3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
34 changes: 15 additions & 19 deletions lib/Cake/Network/CakeResponse.php
Expand Up @@ -416,8 +416,10 @@ public function send() {
$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);
Expand Down Expand Up @@ -556,31 +558,25 @@ protected function _sendContent($content) {
* @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);
}
return $this->_headers;
if (!is_array($header)) {
$header = array($header => $value);
}

if (!is_null($value)) {
$this->_headers[$header] = $value;
return $this->_headers;
foreach ($header as $h => $v) {
if (is_numeric($h)) {
list($h, $v) = array($v, null);
}
if (is_null($v)) {
list($h, $v) = explode(':', $h, 2);
}
$this->_headers[$h] = (is_array($v)) ? array_map('trim', $v) : trim($v);
}

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

Expand Down
13 changes: 11 additions & 2 deletions lib/Cake/Test/Case/Network/CakeResponseTest.php
Expand Up @@ -172,6 +172,10 @@ public function testHeader() {
$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);
}

/**
Expand All @@ -182,7 +186,8 @@ public function testSend() {
$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');
Expand All @@ -194,8 +199,12 @@ public function testSend() {
$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();
}
Expand Down

0 comments on commit a6e3bb3

Please sign in to comment.