From f32c703e7e1af296ebd6901ca8b711f3535498c1 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Fri, 4 Nov 2011 16:56:08 -0430 Subject: [PATCH] Not appending the charset information for content types that are not text/* in CakeResponse --- lib/Cake/Network/CakeResponse.php | 19 +++++++++- .../Test/Case/Network/CakeResponseTest.php | 37 +++++++++++-------- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/lib/Cake/Network/CakeResponse.php b/lib/Cake/Network/CakeResponse.php index 38e863cdc..0cffcf84a 100644 --- a/lib/Cake/Network/CakeResponse.php +++ b/lib/Cake/Network/CakeResponse.php @@ -348,15 +348,32 @@ class CakeResponse { $codeMessage = $this->_statusCodes[$this->_status]; $this->_sendHeader("{$this->_protocol} {$this->_status} {$codeMessage}"); - $this->_sendHeader('Content-Type', "{$this->_contentType}; charset={$this->_charset}"); $this->_setContent(); $this->_setContentLength(); + $this->_setContentType(); foreach ($this->_headers as $header => $value) { $this->_sendHeader($header, $value); } $this->_sendContent($this->_body); } +/** + * Formats the Content-Type header based on the configured contentType and charset + * the charset will only be set in the header if the response is of type text/* + * + * @return void + */ + protected function _setContentType() { + if (in_array($this->_status, array(304, 204))) { + return; + } + if (strpos($this->_contentType, 'text/') === 0) { + $this->header('Content-Type', "{$this->_contentType}; charset={$this->_charset}"); + } else { + $this->header('Content-Type', "{$this->_contentType}"); + } + } + /** * Sets the response body to an empty text if the status code is 204 or 304 * diff --git a/lib/Cake/Test/Case/Network/CakeResponseTest.php b/lib/Cake/Test/Case/Network/CakeResponseTest.php index 2d886769f..6ad7c50d9 100644 --- a/lib/Cake/Test/Case/Network/CakeResponseTest.php +++ b/lib/Cake/Test/Case/Network/CakeResponseTest.php @@ -182,11 +182,13 @@ class CakeResponseTest extends CakeTestCase { $response->expects($this->at(0)) ->method('_sendHeader')->with('HTTP/1.1 200 OK'); $response->expects($this->at(1)) - ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8'); - $response->expects($this->at(2)) ->method('_sendHeader')->with('Content-Language', 'es'); - $response->expects($this->at(3)) + $response->expects($this->at(2)) ->method('_sendHeader')->with('WWW-Authenticate', 'Negotiate'); + $response->expects($this->at(3)) + ->method('_sendHeader')->with('Content-Length', 17); + $response->expects($this->at(4)) + ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8'); $response->send(); } @@ -202,7 +204,9 @@ class CakeResponseTest extends CakeTestCase { $response->expects($this->at(0)) ->method('_sendHeader')->with('HTTP/1.1 200 OK'); $response->expects($this->at(1)) - ->method('_sendHeader')->with('Content-Type', 'audio/mpeg; charset=UTF-8'); + ->method('_sendHeader')->with('Content-Length', 17); + $response->expects($this->at(2)) + ->method('_sendHeader')->with('Content-Type', 'audio/mpeg'); $response->send(); } @@ -218,7 +222,9 @@ class CakeResponseTest extends CakeTestCase { $response->expects($this->at(0)) ->method('_sendHeader')->with('HTTP/1.1 200 OK'); $response->expects($this->at(1)) - ->method('_sendHeader')->with('Content-Type', 'audio/mpeg; charset=UTF-8'); + ->method('_sendHeader')->with('Content-Length', 17); + $response->expects($this->at(2)) + ->method('_sendHeader')->with('Content-Type', 'audio/mpeg'); $response->send(); } @@ -232,9 +238,9 @@ class CakeResponseTest extends CakeTestCase { $response->expects($this->at(0)) ->method('_sendHeader')->with('HTTP/1.1 302 Found'); $response->expects($this->at(1)) - ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8'); - $response->expects($this->at(2)) ->method('_sendHeader')->with('Location', 'http://www.example.com'); + $response->expects($this->at(2)) + ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8'); $response->send(); } @@ -439,9 +445,9 @@ class CakeResponseTest extends CakeTestCase { $response->expects($this->once())->method('_sendContent')->with('the response body'); $response->expects($this->at(0)) ->method('_sendHeader')->with('HTTP/1.1 200 OK'); - $response->expects($this->at(1)) - ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8'); $response->expects($this->at(2)) + ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8'); + $response->expects($this->at(1)) ->method('_sendHeader')->with('Content-Length', strlen('the response body')); $response->send(); @@ -451,9 +457,9 @@ class CakeResponseTest extends CakeTestCase { $response->expects($this->once())->method('_sendContent')->with($body); $response->expects($this->at(0)) ->method('_sendHeader')->with('HTTP/1.1 200 OK'); - $response->expects($this->at(1)) - ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8'); $response->expects($this->at(2)) + ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8'); + $response->expects($this->at(1)) ->method('_sendHeader')->with('Content-Length', 116); $response->send(); @@ -471,7 +477,7 @@ class CakeResponseTest extends CakeTestCase { $response->header('Content-Length', 1); $response->expects($this->never())->method('outputCompressed'); $response->expects($this->once())->method('_sendContent')->with($body); - $response->expects($this->at(2)) + $response->expects($this->at(1)) ->method('_sendHeader')->with('Content-Length', 1); $response->send(); @@ -494,9 +500,9 @@ class CakeResponseTest extends CakeTestCase { $response->expects($this->at(0)) ->method('_sendHeader')->with('HTTP/1.1 200 OK'); $response->expects($this->at(1)) - ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8'); - $response->expects($this->at(2)) ->method('_sendHeader')->with('Content-Length', strlen($goofyOutput) + 116); + $response->expects($this->at(2)) + ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8'); $response->send(); ob_end_clean(); } @@ -524,7 +530,7 @@ class CakeResponseTest extends CakeTestCase { $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent')); $response->length(100); $this->assertEquals(100, $response->length()); - $response->expects($this->at(2)) + $response->expects($this->at(1)) ->method('_sendHeader')->with('Content-Length', 100); $response->send(); @@ -548,6 +554,7 @@ class CakeResponseTest extends CakeTestCase { $response->expects($this->once()) ->method('_sendContent')->with(''); $response->send(); + $this->assertFalse(array_key_exists('Content-Type', $response->header())); $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent')); $response->body('This is a body');