mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-09-10 10:26:35 +00:00
Merge pull request #512 from zachborboa/master
Use correct body in POST requests that use empty arrays for data
This commit is contained in:
+1
-1
@@ -583,7 +583,7 @@ class Curl
|
||||
* [2] https://github.com/php/php-src/pull/531
|
||||
* [3] http://php.net/ChangeLog-5.php#5.5.11
|
||||
*/
|
||||
public function post($url, $data = array(), $follow_303_with_post = false)
|
||||
public function post($url, $data = '', $follow_303_with_post = false)
|
||||
{
|
||||
if (is_array($url)) {
|
||||
$follow_303_with_post = (bool)$data;
|
||||
|
||||
@@ -209,7 +209,7 @@ class MultiCurl
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public function addPost($url, $data = array(), $follow_303_with_post = false)
|
||||
public function addPost($url, $data = '', $follow_303_with_post = false)
|
||||
{
|
||||
if (is_array($url)) {
|
||||
$follow_303_with_post = (bool)$data;
|
||||
|
||||
@@ -16,14 +16,16 @@ class Test
|
||||
$this->curl->setOpt(CURLOPT_SSL_VERIFYHOST, false);
|
||||
}
|
||||
|
||||
public function server($test, $request_method, $query_parameters = array(), $data = array())
|
||||
public function server($test, $request_method, $arg1 = null, $arg2 = null)
|
||||
{
|
||||
$this->curl->setHeader('X-DEBUG-TEST', $test);
|
||||
$request_method = strtolower($request_method);
|
||||
if (is_array($data) && empty($data)) {
|
||||
$this->curl->$request_method(self::TEST_URL, $query_parameters);
|
||||
if ($arg1 !== null && $arg2 !== null) {
|
||||
$this->curl->$request_method(self::TEST_URL, $arg1, $arg2);
|
||||
} elseif ($arg1 !== null) {
|
||||
$this->curl->$request_method(self::TEST_URL, $arg1);
|
||||
} else {
|
||||
$this->curl->$request_method(self::TEST_URL, $query_parameters, $data);
|
||||
$this->curl->$request_method(self::TEST_URL);
|
||||
}
|
||||
return $this->curl->response;
|
||||
}
|
||||
|
||||
@@ -294,6 +294,15 @@ class CurlTest extends \PHPUnit\Framework\TestCase
|
||||
)));
|
||||
}
|
||||
|
||||
public function testPostDataEmptyJson()
|
||||
{
|
||||
$test = new Test();
|
||||
$test->curl->setHeader('Content-Type', 'application/json');
|
||||
$test->server('post_json', 'POST');
|
||||
$this->assertEquals('', $test->curl->response);
|
||||
$this->assertEquals('', $test->curl->getOpt(CURLOPT_POSTFIELDS));
|
||||
}
|
||||
|
||||
public function testPostAssociativeArrayData()
|
||||
{
|
||||
$data = array(
|
||||
|
||||
@@ -2852,4 +2852,20 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
|
||||
$this->assertEquals($expect_retries, $instance->retries);
|
||||
}
|
||||
}
|
||||
|
||||
public function testPostDataEmptyJson()
|
||||
{
|
||||
$multi_curl = new MultiCurl();
|
||||
$multi_curl->setHeader('X-DEBUG-TEST', 'post_json');
|
||||
$multi_curl->setHeader('Content-Type', 'application/json');
|
||||
$multi_curl->addPost(Test::TEST_URL);
|
||||
$post_complete_called = false;
|
||||
$multi_curl->complete(function ($instance) use (&$post_complete_called) {
|
||||
\PHPUnit\Framework\Assert::assertEquals('', $instance->response);
|
||||
\PHPUnit\Framework\Assert::assertEquals('', $instance->getOpt(CURLOPT_POSTFIELDS));
|
||||
$post_complete_called = true;
|
||||
});
|
||||
$multi_curl->start();
|
||||
$this->assertTrue($post_complete_called);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user