Permalink
Browse files

Implementing CakeResponse::notModified()

  • Loading branch information...
1 parent dbd097d commit 8e979cc83e1330f4b00a1fd5064d8b63a19d9ff6 @lorenzo lorenzo committed Jan 13, 2012
Showing with 42 additions and 0 deletions.
  1. +24 −0 lib/Cake/Network/CakeResponse.php
  2. +18 −0 lib/Cake/Test/Case/Network/CakeResponseTest.php
@@ -855,6 +855,30 @@ public function modified($time = 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
* parameters are passed, then an array with the current Vary header
@@ -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.