Fix #357: Allow passing json_decode() parameters to Curl::setDefaultJsonDecoder

This commit is contained in:
Zach Borboa
2016-07-23 01:26:57 -07:00
parent 4ab73948c8
commit 8285b9ff21
2 changed files with 34 additions and 6 deletions
+7 -2
View File
@@ -750,11 +750,16 @@ class Curl
* Set Default JSON Decoder
*
* @access public
* @param $assoc
* @param $depth
* @param $options
*/
public function setDefaultJsonDecoder()
{
$this->jsonDecoder = function($response) {
$json_obj = json_decode($response, false);
$args = func_get_args();
$this->jsonDecoder = function($response) use ($args) {
array_unshift($args, $response);
$json_obj = call_user_func_array('json_decode', $args);
if (!($json_obj === null)) {
$response = $json_obj;
}
+27 -4
View File
@@ -1040,7 +1040,7 @@ class CurlTest extends PHPUnit_Framework_TestCase
$this->assertFalse(file_exists($file_path));
}
public function testJSONRequest()
public function testJsonRequest()
{
foreach (
array(
@@ -1091,7 +1091,7 @@ class CurlTest extends PHPUnit_Framework_TestCase
}
}
public function testJSONResponse()
public function testJsonResponse()
{
foreach (array(
'Content-Type',
@@ -1133,7 +1133,30 @@ class CurlTest extends PHPUnit_Framework_TestCase
}
}
public function testJSONDecoder()
public function testJsonDecoderOptions()
{
// Implicit default json decoder should return object.
$test = new Test();
$test->server('json_response', 'GET');
$this->assertTrue(is_object($test->curl->response));
// Explicit default json decoder should return object.
$test = new Test();
$test->curl->setDefaultJsonDecoder();
$test->server('json_response', 'GET');
$this->assertTrue(is_object($test->curl->response));
// Explicit default json decoder with options should return associative array as specified.
$assoc = true;
$depth = 512;
$options = 0;
$test = new Test();
$test->curl->setDefaultJsonDecoder($assoc, $depth, $options);
$test->server('json_response', 'GET');
$this->assertTrue(is_array($test->curl->response));
}
public function testJsonDecoder()
{
$data = array(
'key' => 'Content-Type',
@@ -1154,7 +1177,7 @@ class CurlTest extends PHPUnit_Framework_TestCase
$this->assertTrue(is_array($test->curl->response));
}
public function testJSONContentTypeDetection()
public function testJsonContentTypeDetection()
{
$json_content_types = array(
'application/alto-costmap+json',