Merge pull request #757 from zachborboa/master

Fix Curl::diagnose() request type output for POST requests
This commit is contained in:
Zach Borboa
2023-01-12 07:20:00 -08:00
committed by GitHub
2 changed files with 40 additions and 14 deletions
+17 -13
View File
@@ -1529,7 +1529,23 @@ class Curl
if ($this->attempts === 0) {
echo 'No HTTP requests have been made.' . "\n";
} else {
$request_method = $this->getOpt(CURLOPT_CUSTOMREQUEST);
$request_types = array(
'DELETE' => $this->getOpt(CURLOPT_CUSTOMREQUEST) === 'DELETE',
'GET' => $this->getOpt(CURLOPT_CUSTOMREQUEST) === 'GET' || $this->getOpt(CURLOPT_HTTPGET),
'HEAD' => $this->getOpt(CURLOPT_CUSTOMREQUEST) === 'HEAD',
'OPTIONS' => $this->getOpt(CURLOPT_CUSTOMREQUEST) === 'OPTIONS',
'PATCH' => $this->getOpt(CURLOPT_CUSTOMREQUEST) === 'PATCH',
'POST' => $this->getOpt(CURLOPT_CUSTOMREQUEST) === 'POST' || $this->getOpt(CURLOPT_POST),
'PUT' => $this->getOpt(CURLOPT_CUSTOMREQUEST) === 'PUT',
'SEARCH' => $this->getOpt(CURLOPT_CUSTOMREQUEST) === 'SEARCH',
);
$request_method = '';
foreach ($request_types as $http_method_name => $http_method_used) {
if ($http_method_used) {
$request_method = $http_method_name;
break;
}
}
$request_url = $this->getOpt(CURLOPT_URL);
$request_options_count = count($this->options);
$request_headers_count = count($this->requestHeaders);
@@ -1598,18 +1614,6 @@ class Curl
$allowed_request_types = array_map(function ($v) {
return trim($v);
}, explode(',', strtoupper($this->responseHeaders['allow'])));
$request_types = array(
'DELETE' => $this->getOpt(CURLOPT_CUSTOMREQUEST) === 'DELETE',
'GET' => $this->getOpt(CURLOPT_CUSTOMREQUEST) === 'GET' || $this->getOpt(CURLOPT_HTTPGET),
'HEAD' => $this->getOpt(CURLOPT_CUSTOMREQUEST) === 'HEAD',
'OPTIONS' => $this->getOpt(CURLOPT_CUSTOMREQUEST) === 'OPTIONS',
'PATCH' => $this->getOpt(CURLOPT_CUSTOMREQUEST) === 'PATCH',
'POST' => $this->getOpt(CURLOPT_CUSTOMREQUEST) === 'POST' || $this->getOpt(CURLOPT_POST),
'PUT' => $this->getOpt(CURLOPT_CUSTOMREQUEST) === 'PUT',
'SEARCH' => $this->getOpt(CURLOPT_CUSTOMREQUEST) === 'SEARCH',
);
foreach ($request_types as $http_method_name => $http_method_used) {
if ($http_method_used && !in_array($http_method_name, $allowed_request_types, true)) {
echo
+23 -1
View File
@@ -4081,7 +4081,7 @@ class CurlTest extends \PHPUnit\Framework\TestCase
$this->assertEquals(3, $curl->getOpt(CURLOPT_MAXREDIRS));
}
public function testDiagnose()
public function testDiagnoseOutputGet()
{
// Test diagnose() with default parameters.
$test_1 = new Test();
@@ -4122,6 +4122,28 @@ class CurlTest extends \PHPUnit\Framework\TestCase
}
}
public function testDiagnoseOutputPost()
{
$test = new Test();
$test->server('error_message', 'POST');
$test_output = $test->curl->diagnose(true);
foreach ([
'--- Begin PHP Curl Class diagnostic output ---',
'PHP Curl Class version: ' . Curl::VERSION,
'PHP version: ' . PHP_VERSION,
'CURLOPT_POST: true',
'Sent an HTTP POST request ',
'Request contained no body.',
'Received an HTTP status code of 401.',
'Received an HTTP 401 error response with message "HTTP/1.1 401 Unauthorized".',
'Received an empty response body (response="").',
'--- End PHP Curl Class diagnostic output ---',
] as $expected_string) {
$this->assertStringContainsString($expected_string, $test_output);
}
}
public function testDiagnoseAllowHeader()
{
$tests = [