diff --git a/cake/libs/cake_request.php b/cake/libs/cake_request.php index 3be281888..6a84f16e7 100644 --- a/cake/libs/cake_request.php +++ b/cake/libs/cake_request.php @@ -485,7 +485,7 @@ class CakeRequest implements ArrayAccess { * * 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 * diff --git a/cake/libs/cake_response.php b/cake/libs/cake_response.php index f8f7716f3..89fb6bd79 100644 --- a/cake/libs/cake_response.php +++ b/cake/libs/cake_response.php @@ -346,11 +346,54 @@ class CakeResponse { /** * 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; } /** diff --git a/cake/tests/cases/libs/cake_response.test.php b/cake/tests/cases/libs/cake_response.test.php index b6d4172c6..92404e2c5 100644 --- a/cake/tests/cases/libs/cake_response.test.php +++ b/cake/tests/cases/libs/cake_response.test.php @@ -84,4 +84,48 @@ class CakeRequestTestCase extends CakeTestCase { $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); + } } \ No newline at end of file