From 026d07b5239180290dc348922fd5957664c3730f Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 26 Dec 2012 22:08:45 -0500 Subject: [PATCH] Implement encoding() and tests. --- lib/Cake/Network/Http/Response.php | 11 ++++++- .../TestCase/Network/Http/ResponseTest.php | 32 ++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Network/Http/Response.php b/lib/Cake/Network/Http/Response.php index a95d5278ee3..c84eb5909fd 100644 --- a/lib/Cake/Network/Http/Response.php +++ b/lib/Cake/Network/Http/Response.php @@ -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]; } /** diff --git a/lib/Cake/Test/TestCase/Network/Http/ResponseTest.php b/lib/Cake/Test/TestCase/Network/Http/ResponseTest.php index 8787da08501..c2529b3092f 100644 --- a/lib/Cake/Test/TestCase/Network/Http/ResponseTest.php +++ b/lib/Cake/Test/TestCase/Network/Http/ResponseTest.php @@ -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()); } }