Skip to content
Permalink
Browse files

Not appending the charset information for content types that are not …

…text/* in CakeResponse
  • Loading branch information...
lorenzo committed Nov 4, 2011
1 parent 4d19d53 commit f32c703e7e1af296ebd6901ca8b711f3535498c1
Showing with 40 additions and 16 deletions.
  1. +18 −1 lib/Cake/Network/CakeResponse.php
  2. +22 −15 lib/Cake/Test/Case/Network/CakeResponseTest.php
@@ -348,15 +348,32 @@ public function send() {
$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
*
@@ -182,11 +182,13 @@ public function testSend() {
$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 @@ public function testSendChangingContentYype() {
$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 @@ public function testSendChangingContentType() {
$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 @@ public function testSendWithLocation() {
$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 @@ public function testSendContentLength() {
$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 @@ public function testSendContentLength() {
$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 @@ public function testSendContentLength() {
$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 @@ public function testSendContentLength() {
$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 @@ public function testLength() {
$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 @@ public function testUnmodifiedContent() {
$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');

0 comments on commit f32c703

Please sign in to comment.
You can’t perform that action at this time.