Skip to content

Commit e240b43

Browse files
committed
Fix php-curl-class#511: Use correct body in POST requests that use empty data arrays.
This fixes JSON POST requests incorrectly sending a serialized empty array "[]" instead of an empty request body by default.
1 parent bdcf043 commit e240b43

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
lines changed

src/Curl/Curl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ public function patch($url, $data = array())
583583
* [2] https://github.com/php/php-src/pull/531
584584
* [3] http://php.net/ChangeLog-5.php#5.5.11
585585
*/
586-
public function post($url, $data = array(), $follow_303_with_post = false)
586+
public function post($url, $data = '', $follow_303_with_post = false)
587587
{
588588
if (is_array($url)) {
589589
$follow_303_with_post = (bool)$data;

src/Curl/MultiCurl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public function addPatch($url, $data = array())
209209
*
210210
* @return object
211211
*/
212-
public function addPost($url, $data = array(), $follow_303_with_post = false)
212+
public function addPost($url, $data = '', $follow_303_with_post = false)
213213
{
214214
if (is_array($url)) {
215215
$follow_303_with_post = (bool)$data;

tests/PHPCurlClass/Helper.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ public function __construct()
1616
$this->curl->setOpt(CURLOPT_SSL_VERIFYHOST, false);
1717
}
1818

19-
public function server($test, $request_method, $query_parameters = array(), $data = array())
19+
public function server($test, $request_method, $arg1 = null, $arg2 = null)
2020
{
2121
$this->curl->setHeader('X-DEBUG-TEST', $test);
2222
$request_method = strtolower($request_method);
23-
if (is_array($data) && empty($data)) {
24-
$this->curl->$request_method(self::TEST_URL, $query_parameters);
23+
if ($arg1 !== null && $arg2 !== null) {
24+
$this->curl->$request_method(self::TEST_URL, $arg1, $arg2);
25+
} elseif ($arg1 !== null) {
26+
$this->curl->$request_method(self::TEST_URL, $arg1);
2527
} else {
26-
$this->curl->$request_method(self::TEST_URL, $query_parameters, $data);
28+
$this->curl->$request_method(self::TEST_URL);
2729
}
2830
return $this->curl->response;
2931
}

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,15 @@ public function testPostData()
294294
)));
295295
}
296296

297+
public function testPostDataEmptyJson()
298+
{
299+
$test = new Test();
300+
$test->curl->setHeader('Content-Type', 'application/json');
301+
$test->server('post_json', 'POST');
302+
$this->assertEquals('', $test->curl->response);
303+
$this->assertEquals('', $test->curl->getOpt(CURLOPT_POSTFIELDS));
304+
}
305+
297306
public function testPostAssociativeArrayData()
298307
{
299308
$data = array(

tests/PHPCurlClass/PHPMultiCurlClassTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2852,4 +2852,20 @@ public function testRetryCallableMulti()
28522852
$this->assertEquals($expect_retries, $instance->retries);
28532853
}
28542854
}
2855+
2856+
public function testPostDataEmptyJson()
2857+
{
2858+
$multi_curl = new MultiCurl();
2859+
$multi_curl->setHeader('X-DEBUG-TEST', 'post_json');
2860+
$multi_curl->setHeader('Content-Type', 'application/json');
2861+
$multi_curl->addPost(Test::TEST_URL);
2862+
$post_complete_called = false;
2863+
$multi_curl->complete(function ($instance) use (&$post_complete_called) {
2864+
\PHPUnit\Framework\Assert::assertEquals('', $instance->response);
2865+
\PHPUnit\Framework\Assert::assertEquals('', $instance->getOpt(CURLOPT_POSTFIELDS));
2866+
$post_complete_called = true;
2867+
});
2868+
$multi_curl->start();
2869+
$this->assertTrue($post_complete_called);
2870+
}
28552871
}

0 commit comments

Comments
 (0)