Implementing the CakeResponse::etag()

This commit is contained in:
Jose Lorenzo Rodriguez 2012-01-12 23:06:05 -04:30
parent 803d49c7c6
commit dbd097debb
2 changed files with 53 additions and 0 deletions

View file

@ -875,6 +875,38 @@ class CakeResponse {
return null; return null;
} }
/**
* Sets the response Etag, Etags are a strong indicative that a response
* can be cached by a HTTP client. A bad way of generaing Etags is
* creating a hash of teh response output, instead generate a unique
* hash of the unique components that identifies a request, such as a
* modification time, a resource Id, and anything else you consider it
* makes it unique.
*
* Second parameter is used to instuct clients that the content has
* changed, but sematicallly, it can be used as the same thing. Think
* for instance of a page with a hit counter, two different page views
* are equivalent, but they differ by a few bytes. This leaves off to
* the Client the decision of using or not the cached page.
*
* If no parameters are passed, current Etag header is returned.
*
* @param string $hash the unique has that identifies this resposnse
* @param boolean $weak whether the response is semantically the same as
* other with th same hash or not
* @return string
**/
public function etag($tag = null, $weak = false) {
if ($tag !== null) {
$this->_headers['Etag'] = sprintf('%s"%s"', ($weak) ? 'W/' : null, $tag);
}
if (isset($this->_headers['Etag'])) {
return $this->_headers['Etag'];
}
return null;
}
/** /**
* Returns a DateTime object initialized at the $time param and using UTC * Returns a DateTime object initialized at the $time param and using UTC
* as timezone * as timezone

View file

@ -784,4 +784,25 @@ class CakeResponseTest extends CakeTestCase {
$this->assertEquals(array('Accept-language', 'Accept-encoding'), $response->vary()); $this->assertEquals(array('Accept-language', 'Accept-encoding'), $response->vary());
} }
/**
* Tests getting/setting the Etag header
*
* @return void
*/
public function testEtag() {
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
$response->etag('something');
$this->assertEquals('"something"', $response->etag());
$response->expects($this->at(1))
->method('_sendHeader')->with('Etag', '"something"');
$response->send();
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
$response->etag('something', true);
$this->assertEquals('W/"something"', $response->etag());
$response->expects($this->at(1))
->method('_sendHeader')->with('Etag', 'W/"something"');
$response->send();
}
} }