Skip to content

Commit 3fd8bd4

Browse files
committed
adds tests and refactoring for when alt=media
1 parent be537c3 commit 3fd8bd4

File tree

2 files changed

+100
-7
lines changed

2 files changed

+100
-7
lines changed

src/Google/Http/REST.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,20 @@ public static function decodeHttpResponse(
9494
$body = (string) $response->getBody();
9595
$code = $response->getStatusCode();
9696

97+
// return raw response when "alt" is "media"
98+
$isJson = !($request && 'media' == $request->getQuery()->get('alt'));
99+
$result = $isJson ? $response->json() : $body;
100+
97101
// retry strategy
98102
if ((intVal($code)) >= 300) {
99103
$errors = null;
100-
$result = $response->json();
101104
// Specific check for APIs which don't return error details, such as Blogger.
102105
if (isset($result['error']) && isset($result['error']['errors'])) {
103106
$errors = $result['error']['errors'];
104107
}
105108
throw new Google_Service_Exception($body, $code, null, $errors);
106109
}
107110

108-
// return raw response when "alt" is "media"
109-
if ($request && $request->getQuery()->get('alt') == 'media') {
110-
return $body;
111-
}
112-
113-
return $response->json();
111+
return $result;
114112
}
115113
}

tests/Google/Service/ResourceTest.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
* under the License.
1919
*/
2020

21+
use GuzzleHttp\Message\Request;
22+
use GuzzleHttp\Message\Response;
23+
use GuzzleHttp\Stream\Stream;
24+
2125
class Test_Google_Service extends Google_Service
2226
{
2327
public function __construct(Google_Client $client)
@@ -193,4 +197,95 @@ public function testAppEngineSslCerts()
193197
$this->client->getHttpClient()->getDefaultOption('verify')
194198
);
195199
}
200+
201+
public function testNoExpectedClassForAltMediaWithHttpSuccess()
202+
{
203+
// set the "alt" parameter to "media"
204+
$arguments = [['alt' => 'media']];
205+
$request = new Request('GET', '/?alt=media');
206+
$body = Stream::factory('thisisnotvalidjson');
207+
$response = new Response(200, [], $body);
208+
209+
$http = $this->getMockBuilder("GuzzleHttp\Client")
210+
->disableOriginalConstructor()
211+
->getMock();
212+
$http->expects($this->once())
213+
->method('send')
214+
->will($this->returnValue($response));
215+
$http->expects($this->any())
216+
->method('createRequest')
217+
->will($this->returnValue($request));
218+
$client = new Google_Client();
219+
$client->setHttpClient($http);
220+
$service = new Test_Google_Service($client);
221+
222+
// set up mock objects
223+
$resource = new Google_Service_Resource(
224+
$service,
225+
"test",
226+
"testResource",
227+
array("methods" =>
228+
array(
229+
"testMethod" => array(
230+
"parameters" => array(),
231+
"path" => "method/path",
232+
"httpMethod" => "POST",
233+
)
234+
)
235+
)
236+
);
237+
238+
$expectedClass = 'ThisShouldBeIgnored';
239+
$decoded = $resource->call('testMethod', $arguments, $expectedClass);
240+
$this->assertEquals('thisisnotvalidjson', $decoded);
241+
}
242+
243+
public function testNoExpectedClassForAltMediaWithHttpFail()
244+
{
245+
// set the "alt" parameter to "media"
246+
$arguments = [['alt' => 'media']];
247+
$request = new Request('GET', '/?alt=media');
248+
$body = Stream::factory('thisisnotvalidjson');
249+
$response = new Response(300, [], $body);
250+
251+
$http = $this->getMockBuilder("GuzzleHttp\Client")
252+
->disableOriginalConstructor()
253+
->getMock();
254+
$http->expects($this->once())
255+
->method('send')
256+
->will($this->returnValue($response));
257+
$http->expects($this->any())
258+
->method('createRequest')
259+
->will($this->returnValue(new Request('GET', '/?alt=media')));
260+
$http->expects($this->once())
261+
->method('send')
262+
->will($this->returnValue($response));
263+
$client = new Google_Client();
264+
$client->setHttpClient($http);
265+
$service = new Test_Google_Service($client);
266+
267+
// set up mock objects
268+
$resource = new Google_Service_Resource(
269+
$service,
270+
"test",
271+
"testResource",
272+
array("methods" =>
273+
array(
274+
"testMethod" => array(
275+
"parameters" => array(),
276+
"path" => "method/path",
277+
"httpMethod" => "POST",
278+
)
279+
)
280+
)
281+
);
282+
283+
try {
284+
$expectedClass = 'ThisShouldBeIgnored';
285+
$decoded = $resource->call('testMethod', $arguments, $expectedClass);
286+
$this->fail('should have thrown exception');
287+
} catch (Google_Service_Exception $e) {
288+
$this->assertEquals('thisisnotvalidjson', $e->getMessage());
289+
}
290+
}
196291
}

0 commit comments

Comments
 (0)