Skip to content
Permalink
Browse files

Removing Pragma headers, implementing sharedMaxAge in CakeResponse

  • Loading branch information...
lorenzo committed Nov 7, 2011
1 parent 2428e83 commit 552c70a5714842dcfc0b32b1e247ce9bc374591b
Showing with 72 additions and 15 deletions.
  1. +29 −5 lib/Cake/Network/CakeResponse.php
  2. +43 −10 lib/Cake/Test/Case/Network/CakeResponseTest.php
@@ -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'
));
}
@@ -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);
@@ -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) {
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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();
}
/**
@@ -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.
You can’t perform that action at this time.