diff --git a/lib/Github/HttpClient/Message/ResponseMediator.php b/lib/Github/HttpClient/Message/ResponseMediator.php index 23982ae3144..5394d80cccb 100644 --- a/lib/Github/HttpClient/Message/ResponseMediator.php +++ b/lib/Github/HttpClient/Message/ResponseMediator.php @@ -9,14 +9,15 @@ class ResponseMediator { public static function getContent(Response $response) { - $body = $response->getBody(true); - $content = json_decode($body, true); - - if (JSON_ERROR_NONE !== json_last_error()) { - return $body; + $body = $response->getBody(true); + if (strpos($response->getContentType(), 'application/json') === 0) { + $content = json_decode($body, true); + if (JSON_ERROR_NONE === json_last_error()) { + return $content; + } } - return $content; + return $body; } public static function getPagination(Response $response) diff --git a/test/Github/Tests/Functional/RepoTest.php b/test/Github/Tests/Functional/RepoTest.php index ce25d545ee6..b45c27c832a 100644 --- a/test/Github/Tests/Functional/RepoTest.php +++ b/test/Github/Tests/Functional/RepoTest.php @@ -42,6 +42,24 @@ public function shouldRetrieveRawBlob() $this->assertStringStartsWith('client->api('git_data')->blobs(); + $api->configure('raw'); + + $contents = $api->show( + 'KnpLabs', + 'php-github-api', + 'dc16d3e77fd4e40638cb722927ffe15fa85b1434' + ); + + $this->assertInternalType('string', $contents); + $this->assertStringStartsWith('{', $contents); + } + /** * @test */ diff --git a/test/Github/Tests/HttpClient/Listener/ErrorListenerTest.php b/test/Github/Tests/HttpClient/Listener/ErrorListenerTest.php index ee783a888b6..b491d3d58c5 100644 --- a/test/Github/Tests/HttpClient/Listener/ErrorListenerTest.php +++ b/test/Github/Tests/HttpClient/Listener/ErrorListenerTest.php @@ -120,6 +120,9 @@ public function shouldNotPassWhen400IsSent() $response->expects($this->once()) ->method('getBody') ->will($this->returnValue(json_encode(array('message' => 'test')))); + $response->expects($this->once()) + ->method('getContentType') + ->will($this->returnValue('application/json')); $response->expects($this->any()) ->method('getStatusCode') ->will($this->returnValue(400)); @@ -159,6 +162,9 @@ public function shouldNotPassWhen422IsSentWithErrorCode($errorCode) ->method('getHeader') ->with('X-RateLimit-Limit') ->will($this->returnValue(5000)); + $response->expects($this->once()) + ->method('getContentType') + ->will($this->returnValue('application/json')); $response->expects($this->once()) ->method('getBody') ->will($this->returnValue($content)); diff --git a/test/Github/Tests/Mock/TestResponse.php b/test/Github/Tests/Mock/TestResponse.php index 9d102844138..0ca86483439 100644 --- a/test/Github/Tests/Mock/TestResponse.php +++ b/test/Github/Tests/Mock/TestResponse.php @@ -36,4 +36,9 @@ public function getHeader($header = null) return $header; } + + public function getContentType() + { + return 'application/json'; + } }