diff --git a/lib/Cake/Network/CakeResponse.php b/lib/Cake/Network/CakeResponse.php index be9d717a3..700ce123e 100644 --- a/lib/Cake/Network/CakeResponse.php +++ b/lib/Cake/Network/CakeResponse.php @@ -767,6 +767,29 @@ class CakeResponse { 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 diff --git a/lib/Cake/Test/Case/Network/CakeResponseTest.php b/lib/Cake/Test/Case/Network/CakeResponseTest.php index 0a6c0f92b..58efb0d57 100644 --- a/lib/Cake/Test/Case/Network/CakeResponseTest.php +++ b/lib/Cake/Test/Case/Network/CakeResponseTest.php @@ -733,4 +733,34 @@ class CakeResponseTest extends CakeTestCase { ->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(); + + } + }