mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Implementing CakeResponse::header() method
This commit is contained in:
parent
a2eac24ec0
commit
2a4b30dba9
3 changed files with 90 additions and 3 deletions
|
@ -485,7 +485,7 @@ class CakeRequest implements ArrayAccess {
|
||||||
*
|
*
|
||||||
* Pattern value comparison allows you to compare a value fetched from `env()` to a regular expression.
|
* 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
|
* ### Option based comparison
|
||||||
*
|
*
|
||||||
|
|
|
@ -346,11 +346,54 @@ class CakeResponse {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Buffers a header string to be sent
|
* 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
|
* @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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -84,4 +84,48 @@ class CakeRequestTestCase extends CakeTestCase {
|
||||||
$this->assertEquals($response->type('xhtml-mobile'), 'application/vnd.wap.xhtml+xml');
|
$this->assertEquals($response->type('xhtml-mobile'), 'application/vnd.wap.xhtml+xml');
|
||||||
$this->assertEquals($response->type('csv'), 'text/csv');
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue