Browse Source

Merge pull request #1503 from tigrang/cake-response-multi-header

Cake response multi header. Fixes #3960
José Lorenzo Rodríguez 12 years ago
parent
commit
42bb766d92
2 changed files with 33 additions and 30 deletions
  1. 13 19
      lib/Cake/Network/CakeResponse.php
  2. 20 11
      lib/Cake/Test/Case/Network/CakeResponseTest.php

+ 13 - 19
lib/Cake/Network/CakeResponse.php

@@ -416,8 +416,10 @@ class CakeResponse {
 		$this->_setContent();
 		$this->_setContentLength();
 		$this->_setContentType();
-		foreach ($this->_headers as $header => $value) {
-			$this->_sendHeader($header, $value);
+		foreach ($this->_headers as $header => $values) {
+			foreach ((array)$values as $value) {
+				$this->_sendHeader($header, $value);
+			}
 		}
 		if ($this->_file) {
 			$this->_sendFile($this->_file, $this->_fileRange);
@@ -556,31 +558,23 @@ class CakeResponse {
  * @param string|array $header. An array of header strings or a single header string
  *	- an associative array of "header name" => "header value" is also accepted
  *	- an array of string headers is also accepted
- * @param string $value. The header value.
+ * @param string|array $value. The header value(s)
  * @return array list of headers to be sent
  */
 	public function header($header = null, $value = null) {
 		if (is_null($header)) {
 			return $this->_headers;
 		}
-		if (is_array($header)) {
-			foreach ($header as $h => $v) {
-				if (is_numeric($h)) {
-					$this->header($v);
-					continue;
-				}
-				$this->_headers[$h] = trim($v);
+		$headers = is_array($header) ? $header : array($header => $value);
+		foreach ($headers as $header => $value) {
+			if (is_numeric($header)) {
+				list($header, $value) = array($value, null);
 			}
-			return $this->_headers;
-		}
-
-		if (!is_null($value)) {
-			$this->_headers[$header] = $value;
-			return $this->_headers;
+			if (is_null($value)) {
+				list($header, $value) = explode(':', $header, 2);
+			}
+			$this->_headers[$header] = is_array($value) ? array_map('trim', $value) : trim($value);
 		}
-
-		list($header, $value) = explode(':', $header, 2);
-		$this->_headers[$header] = trim($value);
 		return $this->_headers;
 	}
 

+ 20 - 11
lib/Cake/Test/Case/Network/CakeResponseTest.php

@@ -137,41 +137,45 @@ class CakeResponseTest extends CakeTestCase {
 	public function testHeader() {
 		$response = new CakeResponse();
 		$headers = array();
-		$this->assertEquals($response->header(), $headers);
+		$this->assertEquals($headers, $response->header());
 
 		$response->header('Location', 'http://example.com');
 		$headers += array('Location' => 'http://example.com');
-		$this->assertEquals($response->header(), $headers);
+		$this->assertEquals($headers, $response->header());
 
 		//Headers with the same name are overwritten
 		$response->header('Location', 'http://example2.com');
 		$headers = array('Location' => 'http://example2.com');
-		$this->assertEquals($response->header(), $headers);
+		$this->assertEquals($headers, $response->header());
 
 		$response->header(array('WWW-Authenticate' => 'Negotiate'));
 		$headers += array('WWW-Authenticate' => 'Negotiate');
-		$this->assertEquals($response->header(), $headers);
+		$this->assertEquals($headers, $response->header());
 
 		$response->header(array('WWW-Authenticate' => 'Not-Negotiate'));
 		$headers['WWW-Authenticate'] = 'Not-Negotiate';
-		$this->assertEquals($response->header(), $headers);
+		$this->assertEquals($headers, $response->header());
 
 		$response->header(array('Age' => 12, 'Allow' => 'GET, HEAD'));
 		$headers += array('Age' => 12, 'Allow' => 'GET, HEAD');
-		$this->assertEquals($response->header(), $headers);
+		$this->assertEquals($headers, $response->header());
 
 		// String headers are allowed
 		$response->header('Content-Language: da');
 		$headers += array('Content-Language' => 'da');
-		$this->assertEquals($response->header(), $headers);
+		$this->assertEquals($headers, $response->header());
 
 		$response->header('Content-Language: da');
 		$headers += array('Content-Language' => 'da');
-		$this->assertEquals($response->header(), $headers);
+		$this->assertEquals($headers, $response->header());
 
 		$response->header(array('Content-Encoding: gzip', 'Vary: *', 'Pragma' => 'no-cache'));
 		$headers += array('Content-Encoding' => 'gzip', 'Vary' => '*', 'Pragma' => 'no-cache');
-		$this->assertEquals($response->header(), $headers);
+		$this->assertEquals($headers, $response->header());
+
+		$response->header('Access-Control-Allow-Origin', array('domain1', 'domain2'));
+		$headers += array('Access-Control-Allow-Origin' => array('domain1', 'domain2'));
+		$this->assertEquals($headers, $response->header());
 	}
 
 /**
@@ -182,7 +186,8 @@ class CakeResponseTest extends CakeTestCase {
 		$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent', '_setCookies'));
 		$response->header(array(
 			'Content-Language' => 'es',
-			'WWW-Authenticate' => 'Negotiate'
+			'WWW-Authenticate' => 'Negotiate',
+			'Access-Control-Allow-Origin' => array('domain1', 'domain2'),
 		));
 		$response->body('the response body');
 		$response->expects($this->once())->method('_sendContent')->with('the response body');
@@ -194,8 +199,12 @@ class CakeResponseTest extends CakeTestCase {
 		$response->expects($this->at(3))
 			->method('_sendHeader')->with('WWW-Authenticate', 'Negotiate');
 		$response->expects($this->at(4))
-			->method('_sendHeader')->with('Content-Length', 17);
+			->method('_sendHeader')->with('Access-Control-Allow-Origin', 'domain1');
 		$response->expects($this->at(5))
+			->method('_sendHeader')->with('Access-Control-Allow-Origin', 'domain2');
+		$response->expects($this->at(6))
+			->method('_sendHeader')->with('Content-Length', 17);
+		$response->expects($this->at(7))
 			->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8');
 		$response->send();
 	}