From ec8b7825aa630b612e9f36dabcb6f2318288af64 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Sat, 17 Jan 2015 01:45:52 -0800 Subject: [PATCH 1/3] Refactor json request test to add additional tests --- tests/PHPCurlClass/PHPCurlClassTest.php | 51 +++++++++++++++---------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 39f8937..33614c0 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -644,31 +644,40 @@ class CurlTest extends PHPUnit_Framework_TestCase public function testJSONRequest() { - $data = array('key' => 'value'); - $expected_response = '{"key":"value"}'; + foreach ( + array( + array( + array( + 'key' => 'value', + ), + '{"key":"value"}', + ), + ) as $test) { + list($data, $expected_response) = $test; - $test = new Test(); - $this->assertEquals($expected_response, $test->server('post_json', 'POST', json_encode($data))); + $test = new Test(); + $this->assertEquals($expected_response, $test->server('post_json', 'POST', json_encode($data))); - foreach (array( - 'Content-Type', - 'content-type', - 'CONTENT-TYPE') as $key) { foreach (array( - 'APPLICATION/JSON', - 'APPLICATION/JSON; CHARSET=UTF-8', - 'APPLICATION/JSON;CHARSET=UTF-8', - 'application/json', - 'application/json; charset=utf-8', - 'application/json;charset=UTF-8', - ) as $value) { - $test = new Test(); - $test->curl->setHeader($key, $value); - $this->assertEquals($expected_response, $test->server('post_json', 'POST', json_encode($data))); + 'Content-Type', + 'content-type', + 'CONTENT-TYPE') as $key) { + foreach (array( + 'APPLICATION/JSON', + 'APPLICATION/JSON; CHARSET=UTF-8', + 'APPLICATION/JSON;CHARSET=UTF-8', + 'application/json', + 'application/json; charset=utf-8', + 'application/json;charset=UTF-8', + ) as $value) { + $test = new Test(); + $test->curl->setHeader($key, $value); + $this->assertEquals($expected_response, $test->server('post_json', 'POST', json_encode($data))); - $test = new Test(); - $test->curl->setHeader($key, $value); - $this->assertEquals($expected_response, $test->server('post_json', 'POST', $data)); + $test = new Test(); + $test->curl->setHeader($key, $value); + $this->assertEquals($expected_response, $test->server('post_json', 'POST', $data)); + } } } } From 891a66c3275a98d8b26264019747a7fd67d368f8 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Sat, 17 Jan 2015 02:00:25 -0800 Subject: [PATCH 2/3] Fix #120: json-encode request when request header Content-Type is application/json and data is a multidimensional array --- src/Curl/Curl.php | 10 +++++++++- tests/PHPCurlClass/PHPCurlClassTest.php | 11 +++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 2659b26..f230f6c 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -413,7 +413,15 @@ class Curl { if (is_array($data)) { if (self::is_array_multidim($data)) { - $data = self::http_build_multi_query($data); + if (isset($this->headers['Content-Type']) && + preg_match($this->json_pattern, $this->headers['Content-Type'])) { + $json_str = json_encode($data); + if (!($json_str === false)) { + $data = $json_str; + } + } else { + $data = self::http_build_multi_query($data); + } } else { $binary_data = false; foreach ($data as $key => $value) { diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 33614c0..51090f7 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -652,6 +652,17 @@ class CurlTest extends PHPUnit_Framework_TestCase ), '{"key":"value"}', ), + array( + array( + 'key' => 'value', + 'strings' => array( + 'a', + 'b', + 'c', + ), + ), + '{"key":"value","strings":["a","b","c"]}', + ), ) as $test) { list($data, $expected_response) = $test; From 1243678883f07e4cf8261a2955272424b219c2cf Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Sat, 17 Jan 2015 02:52:17 -0800 Subject: [PATCH 3/3] Bump version --- composer.json | 2 +- src/Curl/Curl.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 5d2df54..b4bc34b 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "homepage": "https://github.com/php-curl-class/php-curl-class", "license": "Unlicense", "keywords": ["php", "curl", "class"], - "version": "2.1.2", + "version": "2.1.3", "require": { "php": ">=5.3", "ext-curl": "*" diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index f230f6c..e85bc09 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -4,7 +4,7 @@ namespace Curl; class Curl { - const VERSION = '2.1.2'; + const VERSION = '2.1.3'; private $cookies = array(); private $headers = array();