Permalink
Browse files

Adding CakeResponse::vary()

  • Loading branch information...
1 parent 3240f62 commit 803d49c7c6a80c2bfc3894f3af1263e97d069656 @lorenzo lorenzo committed Jan 13, 2012
Showing with 42 additions and 0 deletions.
  1. +21 −0 lib/Cake/Network/CakeResponse.php
  2. +21 −0 lib/Cake/Test/Case/Network/CakeResponseTest.php
@@ -855,6 +855,27 @@ public function modified($time = 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
*
@@ -763,4 +763,25 @@ public function testMustRevalidate() {
}
+/**
+ * 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());
+ }
+
}

0 comments on commit 803d49c

Please sign in to comment.