Skip to content

Commit 749f069

Browse files
committed
Made response mediator content type aware
1 parent 984e463 commit 749f069

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

lib/Github/HttpClient/Message/ResponseMediator.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ class ResponseMediator
99
{
1010
public static function getContent(Response $response)
1111
{
12-
$body = $response->getBody(true);
13-
$content = json_decode($body, true);
14-
15-
if (JSON_ERROR_NONE !== json_last_error()) {
16-
return $body;
12+
$body = $response->getBody(true);
13+
if (strpos($response->getContentType(), 'application/json') === 0) {
14+
$content = json_decode($body, true);
15+
if (JSON_ERROR_NONE === json_last_error()) {
16+
return $content;
17+
}
1718
}
1819

19-
return $content;
20+
return $body;
2021
}
2122

2223
public static function getPagination(Response $response)

test/Github/Tests/Functional/RepoTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ public function shouldRetrieveRawBlob()
4242
$this->assertStringStartsWith('<?php', $contents);
4343
}
4444

45+
/**
46+
* @test
47+
*/
48+
public function shouldNotDecodeRawBlob()
49+
{
50+
$api = $this->client->api('git_data')->blobs();
51+
$api->configure('raw');
52+
53+
$contents = $api->show(
54+
'KnpLabs',
55+
'php-github-api',
56+
'dc16d3e77fd4e40638cb722927ffe15fa85b1434'
57+
);
58+
59+
$this->assertInternalType('string', $contents);
60+
$this->assertStringStartsWith('{', $contents);
61+
}
62+
4563
/**
4664
* @test
4765
*/

test/Github/Tests/HttpClient/Listener/ErrorListenerTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ public function shouldNotPassWhen400IsSent()
120120
$response->expects($this->once())
121121
->method('getBody')
122122
->will($this->returnValue(json_encode(array('message' => 'test'))));
123+
$response->expects($this->once())
124+
->method('getContentType')
125+
->will($this->returnValue('application/json'));
123126
$response->expects($this->any())
124127
->method('getStatusCode')
125128
->will($this->returnValue(400));
@@ -159,6 +162,9 @@ public function shouldNotPassWhen422IsSentWithErrorCode($errorCode)
159162
->method('getHeader')
160163
->with('X-RateLimit-Limit')
161164
->will($this->returnValue(5000));
165+
$response->expects($this->once())
166+
->method('getContentType')
167+
->will($this->returnValue('application/json'));
162168
$response->expects($this->once())
163169
->method('getBody')
164170
->will($this->returnValue($content));

test/Github/Tests/Mock/TestResponse.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ public function getHeader($header = null)
3636

3737
return $header;
3838
}
39+
40+
public function getContentType()
41+
{
42+
return 'application/json';
43+
}
3944
}

0 commit comments

Comments
 (0)