Skip to content

Commit

Permalink
Implementing mustRevaidate()
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 19, 2012
1 parent 552c70a commit 3240f62
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/Cake/Network/CakeResponse.php
Expand Up @@ -767,6 +767,29 @@ public function maxAge($seconds = null) {
return null;
}

/**
* Sets the Cache-Control must-revalidate directive.
* must-revalidate indicates that the response should not be served
* stale by a cache under any cirumstance without first revalidating
* with the origin.
* If called with no parameters, this function will return wheter must-revalidate is present.
*
* @param int $seconds if null, the method will return the current
* must-revalidate value
* @return boolean
*/
public function mustRevalidate($enable = null) {
if ($enable !== null) {
if ($enable) {
$this->_cacheDirectives['must-revalidate'] = null;
} else {
unset($this->_cacheDirectives['must-revalidate']);
}
$this->_setCacheControl();
}
return array_key_exists('must-revalidate', $this->_cacheDirectives);
}

/**
* Helper method to generate a valid Cache-Control header from the options set
* in other methods
Expand Down
30 changes: 30 additions & 0 deletions lib/Cake/Test/Case/Network/CakeResponseTest.php
Expand Up @@ -733,4 +733,34 @@ public function testSharedMaxAge() {
->method('_sendHeader')->with('Cache-Control', 's-maxage=3600, public');
$response->send();
}

/**
* Tests setting of must-revalidate Cache-Control directive
*
* @return void
*/
public function testMustRevalidate() {
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
$this->assertFalse($response->mustRevalidate());
$response->mustRevalidate(true);
$this->assertTrue($response->mustRevalidate());
$headers = $response->header();
$this->assertEquals('must-revalidate', $headers['Cache-Control']);
$response->expects($this->at(1))
->method('_sendHeader')->with('Cache-Control', 'must-revalidate');
$response->send();
$response->mustRevalidate(false);
$this->assertFalse($response->mustRevalidate());

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

}

}

0 comments on commit 3240f62

Please sign in to comment.