Implementing CakeResponse::notModified()

This commit is contained in:
Jose Lorenzo Rodriguez 2012-01-13 00:03:29 -04:30
parent dbd097debb
commit 8e979cc83e
2 changed files with 43 additions and 1 deletions

View file

@ -854,6 +854,30 @@ class CakeResponse {
return null;
}
/**
* Sets the response as Not Modified by removing any body contents
* setting the status code to "304 Not Modified" and removing all
* conflicting headers
*
* @return void
**/
public function notModified() {
$this->statusCode(304);
$this->body('');
$remove = array(
'Allow',
'Content-Encoding',
'Content-Language',
'Content-Length',
'Content-MD5',
'Content-Type',
'Last-Modified'
);
foreach ($remove as $header) {
unset($this->_headers[$header]);
}
}
/**
* Sets the Vary header for the response, if an array is passed,
* values will be imploded into a comma separated string. If no

View file

@ -803,6 +803,24 @@ class CakeResponseTest extends CakeTestCase {
$response->expects($this->at(1))
->method('_sendHeader')->with('Etag', 'W/"something"');
$response->send();
}
/**
* Tests that the response is able to be marked as not modified
*
* @return void
*/
public function testNotModified() {
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
$response->body('something');
$response->statusCode(200);
$response->length(100);
$response->modified('now');
$response->notModified();
$this->assertEmpty($response->header());
$this->assertEmpty($response->body());
$this->assertEquals(304, $response->statusCode());
}
}