Adding CakeResponse::vary()

This commit is contained in:
Jose Lorenzo Rodriguez 2012-01-12 22:15:27 -04:30
parent 3240f6221e
commit 803d49c7c6
2 changed files with 42 additions and 0 deletions

View file

@ -854,6 +854,27 @@ class CakeResponse {
return null;
}
/**
* 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
* value is returned
*
* @param string|array $cacheVariances a single Vary string or a array
* containig the list for variances.
* @return array
**/
public function vary($cacheVariances = null) {
if ($cacheVariances !== null) {
$cacheVariances = (array) $cacheVariances;
$this->_headers['Vary'] = implode(', ', $cacheVariances);
}
if (isset($this->_headers['Vary'])) {
return explode(', ', $this->_headers['Vary']);
}
return null;
}
/**
* Returns a DateTime object initialized at the $time param and using UTC
* as timezone

View file

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