Skip to content

Commit

Permalink
Implement encoding() and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Dec 27, 2012
1 parent b0d8b37 commit 026d07b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/Cake/Network/Http/Response.php
Expand Up @@ -186,9 +186,18 @@ public function statusCode() {
/**
* Get the encoding if it was set.
*
* @return string
* @return string|null
*/
public function encoding() {
$content = $this->header('content-type');
if (!$content) {
return null;
}
preg_match('/charset\s?=\s?[\'"]?([a-z0-9-_]+)[\'"]?/i', $content, $matches);
if (empty($matches[1])) {
return null;
}
return $matches[1];
}

/**
Expand Down
32 changes: 31 additions & 1 deletion lib/Cake/Test/TestCase/Network/Http/ResponseTest.php
Expand Up @@ -172,8 +172,38 @@ public function testStatusCode() {
$this->assertEquals(404, $response->statusCode());
}

/**
* Test reading the encoding out.
*
* @return void
*/
public function testEncoding() {
$this->markTestIncomplete();
$headers = [
'HTTP/1.0 200 Ok',
];
$response = new Response($headers, '');
$this->assertNull($response->encoding());

$headers = [
'HTTP/1.0 200 Ok',
'Content-Type: text/html'
];
$response = new Response($headers, '');
$this->assertNull($response->encoding());

$headers = [
'HTTP/1.0 200 Ok',
'Content-Type: text/html; charset="UTF-8"'
];
$response = new Response($headers, '');
$this->assertEquals('UTF-8', $response->encoding());

$headers = [
'HTTP/1.0 200 Ok',
"Content-Type: text/html; charset='ISO-8859-1'"
];
$response = new Response($headers, '');
$this->assertEquals('ISO-8859-1', $response->encoding());
}

}

0 comments on commit 026d07b

Please sign in to comment.