From 290c30b9451160ae88d26c9f6204097214b7b179 Mon Sep 17 00:00:00 2001 From: JonPurvis Date: Thu, 26 Feb 2026 21:34:57 +0000 Subject: [PATCH] Fix: Handle lowercase Content-Type header for JSON and XML --- src/Http/Response.php | 12 ++++-------- tests/Unit/ResponseTest.php | 10 ++++++++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Http/Response.php b/src/Http/Response.php index 06c878bd..4d4485e3 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -494,14 +494,12 @@ public function header(string $header): string|array|null */ public function isJson(): bool { - $contentType = $this->header('Content-Type'); + $contentType = $this->psrResponse->getHeaderLine('Content-Type'); - if (is_null($contentType)) { + if ($contentType === '') { return false; } - $contentType = is_array($contentType) ? $contentType[0] : $contentType; - return str_contains($contentType, 'json'); } @@ -510,14 +508,12 @@ public function isJson(): bool */ public function isXml(): bool { - $contentType = $this->header('Content-Type'); + $contentType = $this->psrResponse->getHeaderLine('Content-Type'); - if (is_null($contentType)) { + if ($contentType === '') { return false; } - $contentType = is_array($contentType) ? $contentType[0] : $contentType; - return str_contains($contentType, 'xml'); } diff --git a/tests/Unit/ResponseTest.php b/tests/Unit/ResponseTest.php index 14d8706e..02d0e363 100644 --- a/tests/Unit/ResponseTest.php +++ b/tests/Unit/ResponseTest.php @@ -415,6 +415,8 @@ MockResponse::make(['foo' => 'bar'], 200, ['Content-Type' => 'application/json']), // JSON with charset MockResponse::make(['foo' => 'bar'], 200, ['Content-Type' => 'application/json; charset=utf-8']), + // JSON with lowercase header (e.g. FastAPI, HTTP/2) + MockResponse::make(['foo' => 'bar'], 200, ['content-type' => 'application/json']), // Non-JSON content type MockResponse::make('plain text', 200, ['Content-Type' => 'text/plain']), // No content type @@ -429,6 +431,9 @@ $response = $connector->send(new UserRequest, $mockClient); expect($response->isJson())->toBeTrue(); + $response = $connector->send(new UserRequest, $mockClient); + expect($response->isJson())->toBeTrue(); + $response = $connector->send(new UserRequest, $mockClient); expect($response->isJson())->toBeFalse(); @@ -442,6 +447,8 @@ MockResponse::make('', 200, ['Content-Type' => 'application/xml']), // XML with charset MockResponse::make('', 200, ['Content-Type' => 'text/xml; charset=utf-8']), + // XML with lowercase header (e.g. FastAPI, HTTP/2) + MockResponse::make('', 200, ['content-type' => 'application/xml']), // Non-XML content type MockResponse::make('plain text', 200, ['Content-Type' => 'text/plain']), // No content type @@ -456,6 +463,9 @@ $response = $connector->send(new UserRequest, $mockClient); expect($response->isXml())->toBeTrue(); + $response = $connector->send(new UserRequest, $mockClient); + expect($response->isXml())->toBeTrue(); + $response = $connector->send(new UserRequest, $mockClient); expect($response->isXml())->toBeFalse();