Skip to content

Commit

Permalink
fixes issue #30, prevents json parser from bombing out on empty bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
nategood committed May 20, 2012
1 parent cc202f9 commit dd5b82a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Httpful/Handlers/JsonHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@

namespace Httpful\Handlers;

class JsonHandler extends MimeHandlerAdapter
class JsonHandler extends MimeHandlerAdapter
{
/**
* @param string $body
* @return mixed
*/
public function parse($body)
{
if (empty($body))
return "";
$parsed = json_decode($body, false);
if (is_null($parsed))
throw new \Exception("Unable to parse response as JSON");
return $parsed;
}

/**
* @param mixed $payload
* @return string
Expand Down
7 changes: 7 additions & 0 deletions tests/Httpful/HttpfulTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ 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

0 comments on commit dd5b82a

Please sign in to comment.