From 803d49c7c6a80c2bfc3894f3af1263e97d069656 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Thu, 12 Jan 2012 22:15:27 -0430 Subject: [PATCH] Adding CakeResponse::vary() --- lib/Cake/Network/CakeResponse.php | 21 +++++++++++++++++++ .../Test/Case/Network/CakeResponseTest.php | 21 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/Cake/Network/CakeResponse.php b/lib/Cake/Network/CakeResponse.php index 700ce123e..3e58d15b8 100644 --- a/lib/Cake/Network/CakeResponse.php +++ b/lib/Cake/Network/CakeResponse.php @@ -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 diff --git a/lib/Cake/Test/Case/Network/CakeResponseTest.php b/lib/Cake/Test/Case/Network/CakeResponseTest.php index 58efb0d57..60f225d40 100644 --- a/lib/Cake/Test/Case/Network/CakeResponseTest.php +++ b/lib/Cake/Test/Case/Network/CakeResponseTest.php @@ -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()); + } + }