Skip to content
Permalink
Browse files

Adding expires() to CakeResponse to help adding expiration dates to t…

…he http response cache directives
  • Loading branch information...
lorenzo committed Nov 5, 2011
1 parent f32c703 commit bab7e772d2e04313409375ae1e0b9ea5ab3790e2
Showing with 81 additions and 4 deletions.
  1. +45 −1 lib/Cake/Network/CakeResponse.php
  2. +36 −3 lib/Cake/Test/Case/Network/CakeResponseTest.php
@@ -677,10 +677,54 @@ public function cache($since, $time = '+1 day') {
$this->header(array(
'Date' => gmdate("D, j M Y G:i:s ", time()) . 'GMT',
'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
'Expires' => gmdate("D, j M Y H:i:s", $time) . " GMT",
'Cache-Control' => 'public, max-age=' . ($time - time()),
'Pragma' => 'cache'
));
$this->expires($time);
}
/**
* Sets the Expires header for the response by taking an expiration time
* If called with no parameters it will return the current Expires value
*
* ## Examples:
*
* `$response->expires('now')` Will Expire the response cache now
* `$response->expires(new DateTime('+1 day'))` Will set the expiration in next 24 hours
* `$response->expires)` Will return the current expiration header value
*
* @param string|DateTime $time
* @return string
*/
public function expires($time = null) {
if ($time !== null) {
$date = $this->_getUTCDate($time);
$this->_headers['Expires'] = $date->format('D, j M Y H:i:s') . ' GMT';
}
if (isset($this->_headers['Expires'])) {
return $this->_headers['Expires'];
}
return null;
}
/**
* Returns a DateTime object initialized at the $time param and using UTC
* as timezone
*
* @param string|int|DateTime $time
* @return DateTime
*/
protected function _getUTCDate($time = null) {
if ($time instanceof DateTime) {
$result = clone $time;
} else if (is_integer($time)) {
$result = new DateTime(date('Y-m-d H:i:s', $time));
} else {
$result = new DateTime($time);
}
$result->setTimeZone(new DateTimeZone('UTC'));
return $result;
}
/**
@@ -267,12 +267,13 @@ public function testDisableCache() {
public function testCache() {
$response = new CakeResponse();
$since = time();
$time = '+1 day';
$time = new DateTime('+1 day', new DateTimeZone('UTC'));
$response->expires('+1 day');
$expected = array(
'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
'Expires' => gmdate("D, j M Y H:i:s", strtotime($time)) . " GMT",
'Cache-Control' => 'public, max-age=' . (strtotime($time) - time()),
'Expires' => $time->format('D, j M Y H:i:s') . ' GMT',
'Cache-Control' => 'public, max-age=' . ($time->format('U') - time()),
'Pragma' => 'cache'
);
$response->cache($since);
@@ -570,4 +571,36 @@ public function testUnmodifiedContent() {
->method('_sendContent')->with('This is a body');
$response->send();
}
/**
* Tests setting the expiration date
*
* @return void
*/
public function testExpires() {
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
$now = new DateTime('now', new DateTimeZone('America/Los_Angeles'));
$response->expires($now);
$now->setTimeZone(new DateTimeZone('UTC'));
$this->assertEquals($now->format('D, j M Y H:i:s') . ' GMT', $response->expires());
$response->expects($this->at(1))
->method('_sendHeader')->with('Expires', $now->format('D, j M Y H:i:s') . ' GMT');
$response->send();
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
$now = time();
$response->expires($now);
$this->assertEquals(gmdate('D, j M Y H:i:s', $now) . ' GMT', $response->expires());
$response->expects($this->at(1))
->method('_sendHeader')->with('Expires', gmdate('D, j M Y H:i:s', $now) . ' GMT');
$response->send();
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
$time = new DateTime('+1 day', new DateTimeZone('UTC'));
$response->expires('+1 day');
$this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->expires());
$response->expects($this->at(1))
->method('_sendHeader')->with('Expires', $time->format('D, j M Y H:i:s') . ' GMT');
$response->send();
}
}

0 comments on commit bab7e77

Please sign in to comment.
You can’t perform that action at this time.