Skip to content

Commit deaf30b

Browse files
Copilotnadar
andcommitted
Clear CURLOPT_CUSTOMREQUEST in get() and post() methods to fix persistence issue
Co-authored-by: nadar <[email protected]>
1 parent 6528877 commit deaf30b

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

src/Curl/Curl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ protected function setHttpAuth($httpauth)
295295
*/
296296
public function get($url, $data = array())
297297
{
298-
$this->setOpt(CURLOPT_CUSTOMREQUEST, "GET");
298+
$this->setOpt(CURLOPT_CUSTOMREQUEST, null);
299299
if (count($data) > 0) {
300300
$this->setOpt(CURLOPT_URL, $url.'?'.http_build_query($data));
301301
} else {
@@ -338,7 +338,7 @@ public function purge($url, $hostName = null)
338338
*/
339339
public function post($url, $data = array(), $asJson = false)
340340
{
341-
$this->setOpt(CURLOPT_CUSTOMREQUEST, "POST");
341+
$this->setOpt(CURLOPT_CUSTOMREQUEST, null);
342342
$this->setOpt(CURLOPT_URL, $url);
343343
if ($asJson) {
344344
$this->prepareJsonPayload($data);

tests/SequentialRequestsTest.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace Curl;
4+
5+
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
6+
7+
/**
8+
* Test sequential requests using the same Curl object instance
9+
* to ensure options are properly reset between requests.
10+
*
11+
* This test addresses the issue where CURLOPT_CUSTOMREQUEST and
12+
* related options are not cleared after PUT/PATCH/DELETE requests,
13+
* causing subsequent GET requests to fail.
14+
*/
15+
class SequentialRequestsTest extends TestCase
16+
{
17+
const TEST_URL = 'http://localhost:1234';
18+
19+
/**
20+
* @var Curl
21+
*/
22+
protected $curl;
23+
24+
public function set_up()
25+
{
26+
parent::set_up();
27+
$this->curl = new Curl();
28+
$this->curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
29+
$this->curl->setOpt(CURLOPT_SSL_VERIFYHOST, false);
30+
}
31+
32+
/**
33+
* Test that GET works correctly after a PUT request
34+
*/
35+
public function testGetAfterPut()
36+
{
37+
// First, make a PUT request with payload
38+
$this->curl->put(self::TEST_URL . '/server.php', ['test' => 'put', 'key' => 'test'], true);
39+
$this->assertTrue($this->curl->isSuccess(), 'PUT request should succeed');
40+
41+
// Then, make a GET request
42+
$this->curl->get(self::TEST_URL . '/server.php', ['test' => 'server', 'key' => 'REQUEST_METHOD']);
43+
$this->assertTrue($this->curl->isSuccess(), 'GET request after PUT should succeed');
44+
$this->assertEquals('GET', $this->curl->response, 'Request method should be GET, not PUT');
45+
}
46+
47+
/**
48+
* Test that GET works correctly after a DELETE request
49+
*/
50+
public function testGetAfterDelete()
51+
{
52+
// First, make a DELETE request with payload
53+
$this->curl->delete(self::TEST_URL . '/server.php', ['test' => 'delete', 'key' => 'test'], true);
54+
$this->assertTrue($this->curl->isSuccess(), 'DELETE request should succeed');
55+
56+
// Then, make a GET request
57+
$this->curl->get(self::TEST_URL . '/server.php', ['test' => 'server', 'key' => 'REQUEST_METHOD']);
58+
$this->assertTrue($this->curl->isSuccess(), 'GET request after DELETE should succeed');
59+
$this->assertEquals('GET', $this->curl->response, 'Request method should be GET, not DELETE');
60+
}
61+
62+
/**
63+
* Test that GET works correctly after a PATCH request
64+
*/
65+
public function testGetAfterPatch()
66+
{
67+
// First, make a PATCH request with payload
68+
$this->curl->patch(self::TEST_URL . '/server.php', ['test' => 'patch', 'key' => 'test'], true);
69+
$this->assertTrue($this->curl->isSuccess(), 'PATCH request should succeed');
70+
71+
// Then, make a GET request
72+
$this->curl->get(self::TEST_URL . '/server.php', ['test' => 'server', 'key' => 'REQUEST_METHOD']);
73+
$this->assertTrue($this->curl->isSuccess(), 'GET request after PATCH should succeed');
74+
$this->assertEquals('GET', $this->curl->response, 'Request method should be GET, not PATCH');
75+
}
76+
77+
/**
78+
* Test that POST works correctly after a PUT request
79+
*/
80+
public function testPostAfterPut()
81+
{
82+
// First, make a PUT request with payload
83+
$this->curl->put(self::TEST_URL . '/server.php', ['test' => 'put', 'key' => 'test'], true);
84+
$this->assertTrue($this->curl->isSuccess(), 'PUT request should succeed');
85+
86+
// Then, make a POST request
87+
$this->curl->post(self::TEST_URL . '/server.php', ['test' => 'server', 'key' => 'REQUEST_METHOD']);
88+
$this->assertTrue($this->curl->isSuccess(), 'POST request after PUT should succeed');
89+
$this->assertEquals('POST', $this->curl->response, 'Request method should be POST, not PUT');
90+
}
91+
92+
public function tear_down()
93+
{
94+
if ($this->curl) {
95+
$this->curl->close();
96+
}
97+
parent::tear_down();
98+
}
99+
}

0 commit comments

Comments
 (0)