Skip to content

Commit

Permalink
a few more application contents should send the charset
Browse files Browse the repository at this point in the history
adding content type tests as dataprovider
  • Loading branch information
euromark committed Dec 21, 2012
1 parent 554d579 commit 910e21e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
16 changes: 13 additions & 3 deletions lib/Cake/Network/CakeResponse.php
Expand Up @@ -444,10 +444,20 @@ protected function _setContentType() {
if (in_array($this->_status, array(304, 204))) {
return;
}
if (strpos($this->_contentType, 'text/') === 0) {
$whitelist = array(
'application/javascript', 'application/json', 'application/xml', 'application/rss+xml'
);

$charset = false;
if (
$this->_charset &&
(strpos($this->_contentType, 'text/') === 0 || in_array($this->_contentType, $whitelist))
) {
$charset = true;
}

if ($charset) {
$this->header('Content-Type', "{$this->_contentType}; charset={$this->_charset}");
} elseif ($this->_contentType === 'application/json') {
$this->header('Content-Type', "{$this->_contentType}; charset=UTF-8");
} else {
$this->header('Content-Type', "{$this->_contentType}");
}
Expand Down
37 changes: 27 additions & 10 deletions lib/Cake/Test/Case/Network/CakeResponseTest.php
Expand Up @@ -196,12 +196,27 @@ public function testSend() {
}

/**
* Tests the send method and changing the content type
* Data provider for content type tests.
*
* @return array
*/
public static function charsetTypeProvider() {
return array(
array('mp3', 'audio/mpeg'),
array('js', 'application/javascript; charset=UTF-8'),
array('json', 'application/json; charset=UTF-8'),
array('xml', 'application/xml; charset=UTF-8'),
array('txt', 'text/plain; charset=UTF-8'),
);
}

/**
* Tests the send method and changing the content type
* @dataProvider charsetTypeProvider
*/
public function testSendChangingContentType() {
public function testSendChangingContentType($original, $expected) {
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent', '_setCookies'));
$response->type('mp3');
$response->type($original);
$response->body('the response body');
$response->expects($this->once())->method('_sendContent')->with('the response body');
$response->expects($this->at(0))->method('_setCookies');
Expand All @@ -210,26 +225,28 @@ public function testSendChangingContentType() {
$response->expects($this->at(2))
->method('_sendHeader')->with('Content-Length', 17);
$response->expects($this->at(3))
->method('_sendHeader')->with('Content-Type', 'audio/mpeg');
->method('_sendHeader')->with('Content-Type', $expected);
$response->send();
}

/**
* Tests the send method and changing the content type to JSON
* Tests the send method and changing the content type to JS without adding the charset
*
*/
public function testSendChangingContentTypeJSON() {
public function testSendChangingContentTypeWithoutCharset() {
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent', '_setCookies'));
$response->type('json');
$response->body('the response body');
$response->expects($this->once())->method('_sendContent')->with('the response body');
$response->type('js');
$response->charset('');

$response->body('var $foo = "bar";');
$response->expects($this->once())->method('_sendContent')->with('var $foo = "bar";');
$response->expects($this->at(0))->method('_setCookies');
$response->expects($this->at(1))
->method('_sendHeader')->with('HTTP/1.1 200 OK');
$response->expects($this->at(2))
->method('_sendHeader')->with('Content-Length', 17);
$response->expects($this->at(3))
->method('_sendHeader')->with('Content-Type', 'application/json; charset=UTF-8');
->method('_sendHeader')->with('Content-Type', 'application/javascript');
$response->send();
}

Expand Down

0 comments on commit 910e21e

Please sign in to comment.