Fix tests

(cherry picked from commit 0661f1f889)
(cherry picked from commit bab804b8e1)

# Conflicts:
#	tests/e2e/Client.php
This commit is contained in:
Jake Barnby
2023-05-30 19:01:46 +12:00
parent fad53d3e89
commit edce3cceed
5 changed files with 51 additions and 65 deletions
+18 -32
View File
@@ -160,31 +160,21 @@ class Client
* @param array $params
* @param array $headers
* @param bool $decode
* @return array|string
* @return array
* @throws Exception
*/
public function call(string $method, string $path = '', array $headers = [], array $params = [], bool $decode = true)
public function call(string $method, string $path = '', array $headers = [], array $params = [], bool $decode = true): array
{
$headers = array_merge($this->headers, $headers);
$ch = curl_init($this->endpoint . $path . (($method == self::METHOD_GET && !empty($params)) ? '?' . http_build_query($params) : ''));
$responseHeaders = [];
$responseStatus = -1;
$responseType = '';
$responseBody = '';
switch ($headers['content-type']) {
case 'application/json':
$query = json_encode($params);
break;
case 'multipart/form-data':
$query = $this->flatten($params);
break;
default:
$query = http_build_query($params);
break;
}
$query = match ($headers['content-type']) {
'application/json' => json_encode($params),
'multipart/form-data' => $this->flatten($params),
'application/graphql' => $params[0],
default => http_build_query($params),
};
foreach ($headers as $i => $header) {
$headers[] = $i . ':' . $header;
@@ -216,7 +206,7 @@ class Client
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
}
// Allow self signed certificates
// Allow self-signed certificates
if ($this->selfSigned) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
@@ -226,22 +216,18 @@ class Client
$responseType = $responseHeaders['content-type'] ?? '';
$responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($decode) {
switch (substr($responseType, 0, strpos($responseType, ';'))) {
case 'application/json':
$json = json_decode($responseBody, true);
if ($decode && substr($responseType, 0, strpos($responseType, ';')) == 'application/json') {
$json = json_decode($responseBody, true);
if ($json === null) {
throw new Exception('Failed to parse response: ' . $responseBody);
}
$responseBody = $json;
$json = null;
break;
if ($json === null) {
throw new Exception('Failed to parse response: ' . $responseBody);
}
$responseBody = $json;
$json = null;
}
if ((curl_errno($ch)/* || 200 != $responseStatus*/)) {
if ((curl_errno($ch))) {
throw new Exception(curl_error($ch) . ' with status code ' . $responseStatus, $responseStatus);
}
@@ -269,7 +255,7 @@ class Client
{
$cookies = [];
parse_str(strtr($cookie, array('&' => '%26', '+' => '%2B', ';' => '&')), $cookies);
parse_str(strtr($cookie, ['&' => '%26', '+' => '%2B', ';' => '&']), $cookies);
return $cookies;
}