From d40c3b3cb15537e477cc1646b7eb327b8cd0a893 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Wed, 11 Jan 2023 17:29:03 -0800 Subject: [PATCH] Fix Curl::diagnose() request type output for POST requests --- src/Curl/Curl.php | 30 ++++++++++++++----------- tests/PHPCurlClass/PHPCurlClassTest.php | 24 +++++++++++++++++++- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 406ff30..2e8da17 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -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 diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index e07708c..3b85f41 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -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 = [