ソースを参照

Implementing CakeResponse::notModified()

Jose Lorenzo Rodriguez 14 年 前
コミット
8e979cc83e

+ 24 - 0
lib/Cake/Network/CakeResponse.php

@@ -855,6 +855,30 @@ class CakeResponse {
 	}
 
 /**
+ * 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 

+ 18 - 0
lib/Cake/Test/Case/Network/CakeResponseTest.php

@@ -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());
 	}
+
 }