Implementing CakeResponse::cache()

This commit is contained in:
José Lorenzo Rodríguez 2010-07-31 18:31:50 -04:30
parent 5ba0e43aa3
commit 8780f0b33d
2 changed files with 51 additions and 0 deletions

View file

@ -509,6 +509,15 @@ class CakeResponse {
* @return void * @return void
*/ */
public function cache($since, $time = '+1 day') { public function cache($since, $time = '+1 day') {
if (!is_integer($time)) {
$time = strtotime($time);
}
$this->header(array(
'Date' => date("D, j M Y G:i:s ", $since) . 'GMT',
'Expires' => gmdate("D, j M Y H:i:s", $time) . " GMT",
'Cache-Control' => 'cache',
'Pragma' => 'cache'
));
} }
/** /**

View file

@ -215,4 +215,46 @@ class CakeRequestTestCase extends CakeTestCase {
$response->disableCache(); $response->disableCache();
$this->assertEquals($response->header(), $expected); $this->assertEquals($response->header(), $expected);
} }
/**
* Tests the cache method
*
*/
public function testCache() {
$response = new CakeResponse();
$since = time();
$time = '+1 day';
$expected = array(
'Date' => date("D, j M Y G:i:s ", $since) . 'GMT',
'Expires' => gmdate("D, j M Y H:i:s", strtotime($time)) . " GMT",
'Cache-Control' => 'cache',
'Pragma' => 'cache'
);
$response->cache($since);
$this->assertEquals($response->header(), $expected);
$response = new CakeResponse();
$since = time();
$time = '+5 day';
$expected = array(
'Date' => date("D, j M Y G:i:s ", $since) . 'GMT',
'Expires' => gmdate("D, j M Y H:i:s", strtotime($time)) . " GMT",
'Cache-Control' => 'cache',
'Pragma' => 'cache'
);
$response->cache($since, $time);
$this->assertEquals($response->header(), $expected);
$response = new CakeResponse();
$since = time();
$time = time();
$expected = array(
'Date' => date("D, j M Y G:i:s ", $since) . 'GMT',
'Expires' => gmdate("D, j M Y H:i:s", $time) . " GMT",
'Cache-Control' => 'cache',
'Pragma' => 'cache'
);
$response->cache($since, $time);
$this->assertEquals($response->header(), $expected);
}
} }