-
Notifications
You must be signed in to change notification settings - Fork 498
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #167 - Add json() for json responses
* Add json() which returns decoded json from the response body if the response body is a valid json else throws an exception
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
class RequestsTest_Response extends PHPUnit_Framework_TestCase { | ||
/** | ||
* @expectedException Requests_Exception | ||
*/ | ||
public function testInvalidJsonResponse() { | ||
$response = new Requests_Response(); | ||
$response->body = 'Invalid JSON'; | ||
$response->json(); | ||
} | ||
|
||
public function testJsonResponse() { | ||
$response = new Requests_Response(); | ||
$response->body = '{"success": false, "error": [], "data": null}'; | ||
$decodedBody = $response->json(); | ||
|
||
$expected = array( | ||
'success' => false, | ||
'error' => array(), | ||
'data' => null | ||
); | ||
|
||
foreach($expected as $key => $value) | ||
{ | ||
$this->assertEquals($value, $decodedBody[$key]); | ||
} | ||
} | ||
} |