Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implementing CakeResponse::notModified()
  • Loading branch information
lorenzo committed Jan 19, 2012
1 parent dbd097d commit 8e979cc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/Cake/Network/CakeResponse.php
Expand Up @@ -854,6 +854,30 @@ public function modified($time = null) {
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
Expand Down
18 changes: 18 additions & 0 deletions lib/Cake/Test/Case/Network/CakeResponseTest.php
Expand Up @@ -803,6 +803,24 @@ public function testEtag() {
$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());
}

}

0 comments on commit 8e979cc

Please sign in to comment.