From 14888730b6d9681dabb6c88df219fbd34e993705 Mon Sep 17 00:00:00 2001 From: Nate Good Date: Sun, 20 May 2012 18:00:07 -0400 Subject: [PATCH] fixes empty responses for xml parser --- src/Httpful/Handlers/JsonHandler.php | 2 +- src/Httpful/Handlers/XmlHandler.php | 2 ++ tests/Httpful/HttpfulTest.php | 18 +++++++++++------- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Httpful/Handlers/JsonHandler.php b/src/Httpful/Handlers/JsonHandler.php index af6cf76..7207314 100644 --- a/src/Httpful/Handlers/JsonHandler.php +++ b/src/Httpful/Handlers/JsonHandler.php @@ -15,7 +15,7 @@ class JsonHandler extends MimeHandlerAdapter public function parse($body) { if (empty($body)) - return ""; + return null; $parsed = json_decode($body, false); if (is_null($parsed)) throw new \Exception("Unable to parse response as JSON"); diff --git a/src/Httpful/Handlers/XmlHandler.php b/src/Httpful/Handlers/XmlHandler.php index cd3014a..39e9444 100644 --- a/src/Httpful/Handlers/XmlHandler.php +++ b/src/Httpful/Handlers/XmlHandler.php @@ -36,6 +36,8 @@ public function __construct(array $conf = array()) */ public function parse($body) { + if (empty($body)) + return null; $parsed = simplexml_load_string($body, null, $this->libxml_opts, $this->namespace); if (!$parsed) throw new \Exception("Unable to parse response as XML"); diff --git a/tests/Httpful/HttpfulTest.php b/tests/Httpful/HttpfulTest.php index 22b7fbb..5e269fd 100644 --- a/tests/Httpful/HttpfulTest.php +++ b/tests/Httpful/HttpfulTest.php @@ -176,13 +176,6 @@ function testJsonResponseParse() $this->assertEquals(1, $response->body->array[0]); } - function testEmptyJsonResponseParse() - { - $req = Request::init()->sendsAndExpects(Mime::JSON); - $response = new Response("", self::SAMPLE_JSON_HEADER, $req); - $this->assertEquals("", $response->body); - } - function testXMLResponseParse() { $req = Request::init()->sendsAndExpects(Mime::XML); @@ -214,6 +207,17 @@ function testParsingContentTypeCharset() $this->assertEquals($response->charset, 'utf-8'); } + function testEmptyResponseParse() + { + $req = Request::init()->sendsAndExpects(Mime::JSON); + $response = new Response("", self::SAMPLE_JSON_HEADER, $req); + $this->assertEquals(null, $response->body); + + $reqXml = Request::init()->sendsAndExpects(Mime::XML); + $responseXml = new Response("", self::SAMPLE_XML_HEADER, $reqXml); + $this->assertEquals(null, $responseXml->body); + } + function testNoAutoParse() { $req = Request::init()->sendsAndExpects(Mime::JSON)->withoutAutoParsing();