Skip to content

Commit

Permalink
fixes empty responses for xml parser
Browse files Browse the repository at this point in the history
  • Loading branch information
nategood committed May 20, 2012
1 parent dd5b82a commit 1488873
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Httpful/Handlers/JsonHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 2 additions & 0 deletions src/Httpful/Handlers/XmlHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
18 changes: 11 additions & 7 deletions tests/Httpful/HttpfulTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 1488873

Please sign in to comment.