Skip to content

Commit

Permalink
Adding expires() to CakeResponse to help adding expiration dates to t…
Browse files Browse the repository at this point in the history
…he http response cache directives
  • Loading branch information
lorenzo committed Jan 19, 2012
1 parent f32c703 commit bab7e77
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
46 changes: 45 additions & 1 deletion lib/Cake/Network/CakeResponse.php
Expand Up @@ -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;
}

/**
Expand Down
39 changes: 36 additions & 3 deletions lib/Cake/Test/Case/Network/CakeResponseTest.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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.