|
| 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