diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 8263dd4..279ba02 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -1389,7 +1389,8 @@ class Curl extends BaseCurl echo 'Response content length (calculated): ' . $response_calculated_length . "\n"; } - if (preg_match($this->jsonPattern, $this->responseHeaders['Content-Type'])) { + if (isset($this->responseHeaders['Content-Type']) && + preg_match($this->jsonPattern, $this->responseHeaders['Content-Type'])) { $parsed_response = json_decode($this->rawResponse, true); if ($parsed_response !== null) { $messages = []; diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index c1fddc7..c57aad2 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -4363,6 +4363,18 @@ class PHPCurlClassTest extends \PHPUnit\Framework\TestCase $this->assertStringContainsString($expect, $test_5_output); } + public function testDiagnoseContentTypeMissing() + { + $test = new Test(); + $test->server('json_response', 'POST', [ + 'remove-content-type-header' => '', + ]); + $test_output = $test->curl->diagnose(true); + + $this->assertFalse(isset($test->curl->responseHeaders['content-type'])); + $this->assertStringContainsString('Response did not set a content type', $test_output); + } + public function testStopRequest() { $response_length_bytes = 1e6; // 1e6 = 1 megabyte diff --git a/tests/server.php b/tests/server.php index 4f1443a..ea1c1bd 100644 --- a/tests/server.php +++ b/tests/server.php @@ -217,6 +217,10 @@ if ($test === 'http_basic_auth') { ]); } + if (isset($_POST['remove-content-type-header'])) { + header_remove('Content-Type'); + } + echo $body; exit; } elseif ($test === 'xml_response') {