Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: CURLRequest request body is not reset on the next request #6014

Merged
merged 3 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions system/HTTP/CURLRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ protected function resetOptions()
$this->headers = [];
$this->headerMap = [];

// Reset body
$this->body = null;

// Reset configs
$this->config = $this->defaultConfig;

Expand Down
27 changes: 25 additions & 2 deletions tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,21 @@ public function testApplyBody()
$this->assertSame('name=George', $request->curl_options[CURLOPT_POSTFIELDS]);
}

public function testBodyIsResetOnSecondRequest()
{
$request = $this->getRequest([
'base_uri' => 'http://www.foo.com/api/v1/',
'delay' => 100,
]);
$request->setBody('name=George');
$request->setOutput('Hi there');

$request->post('answer');
$request->post('answer');

$this->assertArrayNotHasKey(CURLOPT_POSTFIELDS, $request->curl_options);
}

public function testResponseHeaders()
{
$request = $this->getRequest([
Expand Down Expand Up @@ -922,7 +937,10 @@ public function testJSONData()
$this->assertSame('post', $this->request->getMethod());

$expected = json_encode($params);
$this->assertSame($expected, $this->request->getBody());
$this->assertSame(
$expected,
$this->request->curl_options[CURLOPT_POSTFIELDS]
);
}

public function testSetJSON()
Expand All @@ -936,7 +954,12 @@ public function testSetJSON()
];
$this->request->setJSON($params)->post('/post');

$this->assertSame(json_encode($params), $this->request->getBody());
$expected = json_encode($params);
$this->assertSame(
$expected,
$this->request->curl_options[CURLOPT_POSTFIELDS]
);

$this->assertSame(
'Content-Type: application/json',
$this->request->curl_options[CURLOPT_HTTPHEADER][0]
Expand Down
4 changes: 3 additions & 1 deletion user_guide_src/source/libraries/curlrequest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ Sharing Options

Due to historical reasons, by default, the CURLRequest shares all the options between requests.
If you send more than one request with an instance of the class,
this behavior may cause an error request with unnecessary headers.
this behavior may cause an error request with unnecessary headers and body.

You can change the behavior by editing the following config parameter value in **app/Config/CURLRequest.php** to ``false``:

.. literalinclude:: curlrequest/001.php

.. note:: Before v4.2.0, the request body is not reset even if ``$shareOptions`` is false due to a bug.

*******************
Loading the Library
*******************
Expand Down