Allow http_errors unset/set on each call
If set or not set, on each call this option can be set. If set to null on call, the original value or default config value is used
This commit is contained in:
@@ -990,7 +990,7 @@ final class CoreLibsUrlRequestsCurlTest extends TestCase
|
|||||||
$curl = new \CoreLibs\UrlRequests\Curl();
|
$curl = new \CoreLibs\UrlRequests\Curl();
|
||||||
$this->expectException(\RuntimeException::class);
|
$this->expectException(\RuntimeException::class);
|
||||||
$this->expectExceptionMessageMatches("/InvalidRequestType/");
|
$this->expectExceptionMessageMatches("/InvalidRequestType/");
|
||||||
$response = $curl->request('wrong', 'http://foo.bar.com');
|
$curl->request('wrong', 'http://foo.bar.com');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1018,13 +1018,13 @@ final class CoreLibsUrlRequestsCurlTest extends TestCase
|
|||||||
$this->expectException(\RuntimeException::class);
|
$this->expectException(\RuntimeException::class);
|
||||||
$this->expectExceptionMessageMatches("/CurlExecError/");
|
$this->expectExceptionMessageMatches("/CurlExecError/");
|
||||||
// invalid yrl
|
// invalid yrl
|
||||||
$response = $curl->request('get', 'as-4939345!#$%');
|
$curl->request('get', 'as-4939345!#$%');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: Exception:BadRequest
|
* Exception:ClientError
|
||||||
*
|
*
|
||||||
* @testdox UrlRequests\Curl Exception:BadRequest
|
* @testdox UrlRequests\Curl Exception:ClientError
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
@@ -1033,12 +1033,72 @@ final class CoreLibsUrlRequestsCurlTest extends TestCase
|
|||||||
$curl = new \CoreLibs\UrlRequests\Curl(["http_errors" => true]);
|
$curl = new \CoreLibs\UrlRequests\Curl(["http_errors" => true]);
|
||||||
$this->expectException(\RuntimeException::class);
|
$this->expectException(\RuntimeException::class);
|
||||||
$this->expectExceptionMessageMatches("/ClientError/");
|
$this->expectExceptionMessageMatches("/ClientError/");
|
||||||
|
$curl->get($this->url_basic, [
|
||||||
|
"headers" => [
|
||||||
|
"Authorization" => "schmalztiegel",
|
||||||
|
"RunAuthTest" => "yes",
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception:ClientError
|
||||||
|
*
|
||||||
|
* @testdox UrlRequests\Curl Exception:ClientError on call enable
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testExceptionBadRequestEnable(): void
|
||||||
|
{
|
||||||
|
$curl = new \CoreLibs\UrlRequests\Curl(["http_errors" => false]);
|
||||||
|
$this->expectException(\RuntimeException::class);
|
||||||
|
$this->expectExceptionMessageMatches("/ClientError/");
|
||||||
|
$curl->get($this->url_basic, [
|
||||||
|
"headers" => [
|
||||||
|
"Authorization" => "schmalztiegel",
|
||||||
|
"RunAuthTest" => "yes",
|
||||||
|
],
|
||||||
|
"http_errors" => true
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception:ClientError
|
||||||
|
*
|
||||||
|
* @testdox UrlRequests\Curl Exception:ClientError unset on call
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testExceptionBadRequestUnset(): void
|
||||||
|
{
|
||||||
|
// if true, with false it has to be off
|
||||||
|
$curl = new \CoreLibs\UrlRequests\Curl(["http_errors" => true]);
|
||||||
$response = $curl->get($this->url_basic, [
|
$response = $curl->get($this->url_basic, [
|
||||||
"headers" => [
|
"headers" => [
|
||||||
"Authorization" => "schmalztiegel",
|
"Authorization" => "schmalztiegel",
|
||||||
"RunAuthTest" => "yes",
|
"RunAuthTest" => "yes",
|
||||||
]
|
],
|
||||||
|
"http_errors" => false,
|
||||||
]);
|
]);
|
||||||
|
$this->assertEquals(
|
||||||
|
"401",
|
||||||
|
$response['code'],
|
||||||
|
'Unset Exception failed with false'
|
||||||
|
);
|
||||||
|
// if false, null should not change it
|
||||||
|
$curl = new \CoreLibs\UrlRequests\Curl(["http_errors" => false]);
|
||||||
|
$response = $curl->get($this->url_basic, [
|
||||||
|
"headers" => [
|
||||||
|
"Authorization" => "schmalztiegel",
|
||||||
|
"RunAuthTest" => "yes",
|
||||||
|
],
|
||||||
|
"http_errors" => null,
|
||||||
|
]);
|
||||||
|
$this->assertEquals(
|
||||||
|
"401",
|
||||||
|
$response['code'],
|
||||||
|
'Unset Exception failed with null'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -276,6 +276,24 @@ try {
|
|||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
print "Exception: <pre>" . print_r(json_decode($e->getMessage(), true), true) . "</pre><br>";
|
print "Exception: <pre>" . print_r(json_decode($e->getMessage(), true), true) . "</pre><br>";
|
||||||
}
|
}
|
||||||
|
print "AUTH REQUEST WITH EXCEPTION (UNSET):<br>";
|
||||||
|
try {
|
||||||
|
$uc = new Curl([
|
||||||
|
"base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/',
|
||||||
|
"http_errors" => true,
|
||||||
|
"headers" => [
|
||||||
|
"Authorization" => "schmalztiegel",
|
||||||
|
"RunAuthTest" => "yes",
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
$response = $uc->get('UrlRequests.target.php', ['http_errors' => false]);
|
||||||
|
print "AUTH REQUEST (UNSET): <pre>" . print_r($response, true) . "</pre>";
|
||||||
|
print "[uc] SENT URL: " . $uc->getUrlSent() . "<br>";
|
||||||
|
print "[uc] SENT URL PARSED: <pre>" . print_r($uc->getUrlParsedSent(), true) . "</pre>";
|
||||||
|
print "[uc] SENT HEADERS: <pre>" . print_r($uc->getHeadersSent(), true) . "</pre>";
|
||||||
|
} catch (Exception $e) {
|
||||||
|
print "Exception: <pre>" . print_r(json_decode($e->getMessage(), true), true) . "</pre><br>";
|
||||||
|
}
|
||||||
|
|
||||||
print "<hr>";
|
print "<hr>";
|
||||||
$uc = new Curl([
|
$uc = new Curl([
|
||||||
|
|||||||
@@ -458,22 +458,25 @@ class Curl implements Interface\RequestsInterface
|
|||||||
/**
|
/**
|
||||||
* Overall request call
|
* Overall request call
|
||||||
*
|
*
|
||||||
* @param string $type get, post, pathc, put, delete:
|
* @param string $type get, post, pathc, put, delete:
|
||||||
* if not set or invalid throw error
|
* if not set or invalid throw error
|
||||||
* @param string $url The URL being requested,
|
* @param string $url The URL being requested,
|
||||||
* including domain and protocol
|
* including domain and protocol
|
||||||
* @param null|array<string,string|array<string>> $headers [default=[]] Headers to be used in the request
|
* @param null|array<string,string|array<string>> $headers Headers to be used in the request
|
||||||
* @param null|array<string,string> $query [default=null] Optinal query parameters
|
* @param null|array<string,string> $query Optinal query parameters
|
||||||
* @param null|string|array<string,mixed> $body [default=null] Data body, converted to JSON
|
* @param null|string|array<string,mixed> $body Data body, converted to JSON
|
||||||
|
* @param null|bool $http_errors Throw exception on http response
|
||||||
|
* 400 or higher if set to true
|
||||||
* @return array{code:string,headers:array<string,array<string>>,content:string}
|
* @return array{code:string,headers:array<string,array<string>>,content:string}
|
||||||
* @throws \RuntimeException if type param is not valid
|
* @throws \RuntimeException if type param is not valid
|
||||||
*/
|
*/
|
||||||
private function curlRequest(
|
private function curlRequest(
|
||||||
string $type,
|
string $type,
|
||||||
string $url,
|
string $url,
|
||||||
null|array $headers = [],
|
null|array $headers,
|
||||||
null|array $query = null,
|
null|array $query,
|
||||||
null|string|array $body = null
|
null|string|array $body,
|
||||||
|
null|bool $http_errors,
|
||||||
): array {
|
): array {
|
||||||
$this->url = $this->buildQuery($url, $query);
|
$this->url = $this->buildQuery($url, $query);
|
||||||
$this->headers = $this->convertHeaders($this->buildHeaders($headers));
|
$this->headers = $this->convertHeaders($this->buildHeaders($headers));
|
||||||
@@ -514,7 +517,7 @@ class Curl implements Interface\RequestsInterface
|
|||||||
// for debug
|
// for debug
|
||||||
// print "CURLINFO_HEADER_OUT: <pre>" . curl_getinfo($handle, CURLINFO_HEADER_OUT) . "</pre>";
|
// print "CURLINFO_HEADER_OUT: <pre>" . curl_getinfo($handle, CURLINFO_HEADER_OUT) . "</pre>";
|
||||||
// get response code and bail on not authorized
|
// get response code and bail on not authorized
|
||||||
$http_response = $this->handleCurlResponse($http_result, $handle);
|
$http_response = $this->handleCurlResponse($http_result, $http_errors, $handle);
|
||||||
// close handler
|
// close handler
|
||||||
$this->handleCurlClose($handle);
|
$this->handleCurlClose($handle);
|
||||||
// return response and result
|
// return response and result
|
||||||
@@ -685,17 +688,19 @@ class Curl implements Interface\RequestsInterface
|
|||||||
* can be turned off by setting http_errors to false
|
* can be turned off by setting http_errors to false
|
||||||
*
|
*
|
||||||
* @param string $http_result result string from the url call
|
* @param string $http_result result string from the url call
|
||||||
|
* @param ?bool $http_errors if we should throw an exception on error, override config setting
|
||||||
* @param \CurlHandle $handle Curl handler
|
* @param \CurlHandle $handle Curl handler
|
||||||
* @return string http response code
|
* @return string http response code
|
||||||
* @throws \RuntimeException if http_errors is true then will throw exception on any response code >= 400
|
* @throws \RuntimeException if http_errors is true then will throw exception on any response code >= 400
|
||||||
*/
|
*/
|
||||||
private function handleCurlResponse(
|
private function handleCurlResponse(
|
||||||
string $http_result,
|
string $http_result,
|
||||||
|
?bool $http_errors,
|
||||||
\CurlHandle $handle
|
\CurlHandle $handle
|
||||||
): string {
|
): string {
|
||||||
$http_response = curl_getinfo($handle, CURLINFO_RESPONSE_CODE);
|
$http_response = curl_getinfo($handle, CURLINFO_RESPONSE_CODE);
|
||||||
if (
|
if (
|
||||||
empty($this->config['http_errors']) ||
|
empty($http_errors ?? $this->config['http_errors']) ||
|
||||||
$http_response < self::HTTP_BAD_REQUEST
|
$http_response < self::HTTP_BAD_REQUEST
|
||||||
) {
|
) {
|
||||||
return (string)$http_response;
|
return (string)$http_response;
|
||||||
@@ -942,7 +947,7 @@ class Curl implements Interface\RequestsInterface
|
|||||||
* phpcs:disable Generic.Files.LineLength
|
* phpcs:disable Generic.Files.LineLength
|
||||||
* @param string $type
|
* @param string $type
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<string,mixed>} $options
|
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options
|
||||||
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
|
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
|
||||||
* @throws \UnexpectedValueException on missing body data when body data is needed
|
* @throws \UnexpectedValueException on missing body data when body data is needed
|
||||||
* phpcs:enable Generic.Files.LineLength
|
* phpcs:enable Generic.Files.LineLength
|
||||||
@@ -964,7 +969,8 @@ class Curl implements Interface\RequestsInterface
|
|||||||
$url,
|
$url,
|
||||||
!array_key_exists('headers', $options) ? [] : $options['headers'],
|
!array_key_exists('headers', $options) ? [] : $options['headers'],
|
||||||
$options['query'] ?? null,
|
$options['query'] ?? null,
|
||||||
$options['body'] ?? null
|
$options['body'] ?? null,
|
||||||
|
!array_key_exists('http_errors', $options) ? null : $options['http_errors'],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,8 +25,8 @@ trait CurlTrait
|
|||||||
* "get" calls do not set any body
|
* "get" calls do not set any body
|
||||||
*
|
*
|
||||||
* @param string $type if set as get do not add body, else add body
|
* @param string $type if set as get do not add body, else add body
|
||||||
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>} $options Request options
|
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Request options
|
||||||
* @return array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>}
|
* @return array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool}
|
||||||
*/
|
*/
|
||||||
private function setOptions(string $type, array $options): array
|
private function setOptions(string $type, array $options): array
|
||||||
{
|
{
|
||||||
@@ -34,12 +34,14 @@ trait CurlTrait
|
|||||||
return [
|
return [
|
||||||
"headers" => !array_key_exists('headers', $options) ? [] : $options['headers'],
|
"headers" => !array_key_exists('headers', $options) ? [] : $options['headers'],
|
||||||
"query" => $options['query'] ?? null,
|
"query" => $options['query'] ?? null,
|
||||||
|
"http_errors" => !array_key_exists('http_errors', $options) ? null : $options['http_errors'],
|
||||||
];
|
];
|
||||||
} else {
|
} else {
|
||||||
return [
|
return [
|
||||||
"headers" => !array_key_exists('headers', $options) ? [] : $options['headers'],
|
"headers" => !array_key_exists('headers', $options) ? [] : $options['headers'],
|
||||||
"query" => $options['query'] ?? null,
|
"query" => $options['query'] ?? null,
|
||||||
"body" => $options['body'] ?? null,
|
"body" => $options['body'] ?? null,
|
||||||
|
"http_errors" => !array_key_exists('http_errors', $options) ? null : $options['http_errors'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -53,7 +55,7 @@ trait CurlTrait
|
|||||||
*
|
*
|
||||||
* @param string $type What type of request we send, will throw exception if not a valid one
|
* @param string $type What type of request we send, will throw exception if not a valid one
|
||||||
* @param string $url The url to send
|
* @param string $url The url to send
|
||||||
* @param array{headers?:null|array<string,string|array<string>>,query?:null|string|array<string,mixed>,body?:null|string|array<string,mixed>} $options Request options
|
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Request options
|
||||||
* @return array{code:string,headers:array<string,array<string>>,content:string} [default=[]] Result code, headers and content as array, content is json
|
* @return array{code:string,headers:array<string,array<string>>,content:string} [default=[]] Result code, headers and content as array, content is json
|
||||||
* @throws \UnexpectedValueException on missing body data when body data is needed
|
* @throws \UnexpectedValueException on missing body data when body data is needed
|
||||||
*/
|
*/
|
||||||
@@ -65,7 +67,7 @@ trait CurlTrait
|
|||||||
*
|
*
|
||||||
* @param string $url The URL being requested,
|
* @param string $url The URL being requested,
|
||||||
* including domain and protocol
|
* including domain and protocol
|
||||||
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>} $options Options to set
|
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Options to set
|
||||||
* @return array{code:string,headers:array<string,array<string>>,content:string} [default=[]] Result code, headers and content as array, content is json
|
* @return array{code:string,headers:array<string,array<string>>,content:string} [default=[]] Result code, headers and content as array, content is json
|
||||||
*/
|
*/
|
||||||
public function get(string $url, array $options = []): array
|
public function get(string $url, array $options = []): array
|
||||||
@@ -86,7 +88,7 @@ trait CurlTrait
|
|||||||
*
|
*
|
||||||
* @param string $url The URL being requested,
|
* @param string $url The URL being requested,
|
||||||
* including domain and protocol
|
* including domain and protocol
|
||||||
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>} $options Options to set
|
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Options to set
|
||||||
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
|
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
|
||||||
*/
|
*/
|
||||||
public function post(string $url, array $options): array
|
public function post(string $url, array $options): array
|
||||||
@@ -104,7 +106,7 @@ trait CurlTrait
|
|||||||
*
|
*
|
||||||
* @param string $url The URL being requested,
|
* @param string $url The URL being requested,
|
||||||
* including domain and protocol
|
* including domain and protocol
|
||||||
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>} $options Options to set
|
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Options to set
|
||||||
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
|
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
|
||||||
*/
|
*/
|
||||||
public function put(string $url, array $options): array
|
public function put(string $url, array $options): array
|
||||||
@@ -122,7 +124,7 @@ trait CurlTrait
|
|||||||
*
|
*
|
||||||
* @param string $url The URL being requested,
|
* @param string $url The URL being requested,
|
||||||
* including domain and protocol
|
* including domain and protocol
|
||||||
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>} $options Options to set
|
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Options to set
|
||||||
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
|
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
|
||||||
*/
|
*/
|
||||||
public function patch(string $url, array $options): array
|
public function patch(string $url, array $options): array
|
||||||
@@ -141,7 +143,7 @@ trait CurlTrait
|
|||||||
*
|
*
|
||||||
* @param string $url The URL being requested,
|
* @param string $url The URL being requested,
|
||||||
* including domain and protocol
|
* including domain and protocol
|
||||||
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>} $options Options to set
|
* @param array{headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Options to set
|
||||||
* @return array{code:string,headers:array<string,array<string>>,content:string} [default=[]] Result code, headers and content as array, content is json
|
* @return array{code:string,headers:array<string,array<string>>,content:string} [default=[]] Result code, headers and content as array, content is json
|
||||||
*/
|
*/
|
||||||
public function delete(string $url, array $options = []): array
|
public function delete(string $url, array $options = []): array
|
||||||
|
|||||||
Reference in New Issue
Block a user