Skip to content

Commit

Permalink
Removing Pragma headers, implementing sharedMaxAge in CakeResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 19, 2012
1 parent 2428e83 commit 552c70a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 15 deletions.
34 changes: 29 additions & 5 deletions lib/Cake/Network/CakeResponse.php
Expand Up @@ -666,8 +666,7 @@ public function disableCache() {
$this->header(array(
'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
'Last-Modified' => gmdate("D, d M Y H:i:s") . " GMT",
'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
'Pragma' => 'no-cache'
'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
));
}

Expand Down Expand Up @@ -699,9 +698,10 @@ public function cache($since, $time = '+1 day') {
* @param boolean $public if set to true, the Cache-Control header will be set as public
* if set to false, the response will be set to private
* if no value is provided, it will return whether the response is sharable or not
* @param int $time time in seconds after which the response should no longer be considered fresh
* @return boolean
*/
public function sharable($public = null) {
public function sharable($public = null, $time = null) {
if ($public === null) {
$public = array_key_exists('public', $this->_cacheDirectives);
$private = array_key_exists('private', $this->_cacheDirectives);
Expand All @@ -715,21 +715,45 @@ public function sharable($public = null) {
if ($public) {
$this->_cacheDirectives['public'] = null;
unset($this->_cacheDirectives['private']);
$this->sharedMaxAge($time);
} else {
$this->_cacheDirectives['private'] = null;
unset($this->_cacheDirectives['public']);
$this->maxAge($time);
}
if ($time == null) {
$this->_setCacheControl();
}
$this->_setCacheControl();
return (bool) $public;
}

/**
* Sets the Cache-Control s-maxage directive.
* The max-age is the number of seconds after which the response should no longer be considered
* a good candidate to be fetched from a shared cache (like in a proxy server).
* If called with no parameters, this function will return the current max-age value if any
*
* @param int $seconds if null, the method will return the current s-maxage value
* @return int
*/
public function sharedMaxAge($seconds = null) {
if ($seconds !== null) {
$this->_cacheDirectives['s-maxage'] = $seconds;
$this->_setCacheControl();
}
if (isset($this->_cacheDirectives['s-maxage'])) {
return $this->_cacheDirectives['s-maxage'];
}
return null;
}

/**
* Sets the Cache-Control max-age directive.
* The max-age is the number of seconds after which the response should no longer be considered
* a good candidate to be fetched from the local (client) cache.
* If called with no parameters, this function will return the current max-age value if any
*
* @param int $seconds
* @param int $seconds if null, the method will return the current max-age value
* @return int
*/
public function maxAge($seconds = null) {
Expand Down
53 changes: 43 additions & 10 deletions lib/Cake/Test/Case/Network/CakeResponseTest.php
Expand Up @@ -253,8 +253,7 @@ public function testDisableCache() {
$expected = array(
'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
'Last-Modified' => gmdate("D, d M Y H:i:s") . " GMT",
'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
'Pragma' => 'no-cache'
'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
);
$response->disableCache();
$this->assertEquals($response->header(), $expected);
Expand All @@ -273,8 +272,7 @@ public function testCache() {
'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
'Expires' => $time->format('D, j M Y H:i:s') . ' GMT',
'Cache-Control' => 'public, max-age=' . ($time->format('U') - time()),
'Pragma' => 'cache'
'Cache-Control' => 'public, max-age=' . ($time->format('U') - time())
);
$response->cache($since);
$this->assertEquals($response->header(), $expected);
Expand All @@ -286,8 +284,7 @@ public function testCache() {
'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
'Expires' => gmdate("D, j M Y H:i:s", strtotime($time)) . " GMT",
'Cache-Control' => 'public, max-age=' . (strtotime($time) - time()),
'Pragma' => 'cache'
'Cache-Control' => 'public, max-age=' . (strtotime($time) - time())
);
$response->cache($since, $time);
$this->assertEquals($response->header(), $expected);
Expand All @@ -299,8 +296,7 @@ public function testCache() {
'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
'Expires' => gmdate("D, j M Y H:i:s", $time) . " GMT",
'Cache-Control' => 'public, max-age=0',
'Pragma' => 'cache'
'Cache-Control' => 'public, max-age=0'
);
$response->cache($since, $time);
$this->assertEquals($response->header(), $expected);
Expand Down Expand Up @@ -673,6 +669,17 @@ public function testSharable() {
$this->assertFalse($response->sharable());
$response->sharable(true);
$this->assertTrue($response->sharable());

$response = new CakeResponse;
$response->sharable(true, 3600);
$headers = $response->header();
$this->assertEquals('public, s-maxage=3600', $headers['Cache-Control']);

$response = new CakeResponse;
$response->sharable(false, 3600);
$headers = $response->header();
$this->assertEquals('private, max-age=3600', $headers['Cache-Control']);
$response->send();
}

/**
Expand All @@ -693,11 +700,37 @@ public function testMaxAge() {

$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
$response->maxAge(3600);
$response->sharable(false);
$headers = $response->header();
$this->assertEquals('max-age=3600, private', $headers['Cache-Control']);
$response->expects($this->at(1))
->method('_sendHeader')->with('Cache-Control', 'max-age=3600, private');
$response->send();
}

/**
* Tests setting of s-maxage Cache-Control directive
*
* @return void
*/
public function testSharedMaxAge() {
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
$this->assertNull($response->maxAge());
$response->sharedMaxAge(3600);
$this->assertEquals(3600, $response->sharedMaxAge());
$headers = $response->header();
$this->assertEquals('s-maxage=3600', $headers['Cache-Control']);
$response->expects($this->at(1))
->method('_sendHeader')->with('Cache-Control', 's-maxage=3600');
$response->send();

$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
$response->sharedMaxAge(3600);
$response->sharable(true);
$headers = $response->header();
$this->assertEquals('max-age=3600, public', $headers['Cache-Control']);
$this->assertEquals('s-maxage=3600, public', $headers['Cache-Control']);
$response->expects($this->at(1))
->method('_sendHeader')->with('Cache-Control', 'max-age=3600, public');
->method('_sendHeader')->with('Cache-Control', 's-maxage=3600, public');
$response->send();
}
}

0 comments on commit 552c70a

Please sign in to comment.