Merge pull request #744 from zachborboa/master

Add automatic gzip decoding of response
This commit is contained in:
Zach Borboa
2022-12-05 19:07:23 -08:00
committed by GitHub
3 changed files with 35 additions and 9 deletions
+9
View File
@@ -2124,6 +2124,8 @@ class Curl
* Returns the original raw response unless a default decoder has been set.
* If the response content-type cannot be determined:
* Returns the original raw response.
* If the response content-encoding is gzip:
* Returns the response gzip-decoded.
*/
private function parseResponse($response_headers, $raw_response)
{
@@ -2148,6 +2150,13 @@ class Curl
}
}
if (isset($response_headers['Content-Encoding']) && $response_headers['Content-Encoding'] === 'gzip') {
$decoded_response = gzdecode($response);
if ($decoded_response !== false) {
$response = $decoded_response;
}
}
return $response;
}
+11
View File
@@ -4385,4 +4385,15 @@ class CurlTest extends \PHPUnit\Framework\TestCase
$this->assertEquals(5, $test->curl->retries);
$this->assertFalse($test->curl->error);
}
public function testGzipDecoding()
{
$test = new Test();
$test->server('json_response', 'POST', [
'key' => 'content-encoding',
'value' => 'gzip',
'body' => gzencode('hello'),
]);
$this->assertEquals('hello', $test->curl->response);
}
}
+15 -9
View File
@@ -197,16 +197,22 @@ if ($test === 'http_basic_auth') {
$value = 'application/json';
}
if (isset($_POST['body'])) {
$body = $_POST['body'];
} else {
$body = json_encode([
'null' => null,
'true' => true,
'false' => false,
'integer' => 1,
'float' => 3.14,
'empty' => '',
'string' => 'string',
]);
}
header($key . ': ' . $value);
echo json_encode([
'null' => null,
'true' => true,
'false' => false,
'integer' => 1,
'float' => 3.14,
'empty' => '',
'string' => 'string',
]);
echo $body;
exit;
} elseif ($test === 'xml_response') {
$key = $_POST['key'];