From c0e6be87b6af85eb596bd03ed408c5de2ec46d9c Mon Sep 17 00:00:00 2001 From: Mustafa BALCI Date: Thu, 12 Sep 2024 16:44:41 +0300 Subject: [PATCH 1/4] Added isJson and isXML functions to Response class --- src/Http/Response.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Http/Response.php b/src/Http/Response.php index bc2ed81b..6c643b1e 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -489,6 +489,26 @@ public function header(string $header): string|array|null return $this->headers()->get($header); } + /** + * Determine if the response is in JSON format. + * + * @return bool + */ + public function isJson(): bool + { + return str_contains($this->header('Content-Type'), 'json'); + } + + /** + * Determine if the response is in XML format. + * + * @return bool + */ + public function isXML(): bool + { + return str_contains($this->header('Content-Type'), 'xml'); + } + /** * Create a temporary resource for the stream. * From f9de9c17976ac08f14009c9446f857cf7301d614 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mustafa=20Balc=C4=B1?= <19329346+mstfblci@users.noreply.github.com> Date: Thu, 12 Sep 2024 16:49:21 +0300 Subject: [PATCH 2/4] Refactor function name: isXML to isXml --- src/Http/Response.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/Response.php b/src/Http/Response.php index 6c643b1e..617a737f 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -504,7 +504,7 @@ public function isJson(): bool * * @return bool */ - public function isXML(): bool + public function isXml(): bool { return str_contains($this->header('Content-Type'), 'xml'); } From c98b1bba4717f5a46f199b8743ac922df8cfa788 Mon Sep 17 00:00:00 2001 From: Mustafa BALCI Date: Fri, 10 Jan 2025 11:26:34 +0300 Subject: [PATCH 3/4] Enhance isJson and isXml methods in Response class to handle null Content-Type headers gracefully --- src/Http/Response.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Http/Response.php b/src/Http/Response.php index 617a737f..005026bc 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -496,7 +496,13 @@ public function header(string $header): string|array|null */ public function isJson(): bool { - return str_contains($this->header('Content-Type'), 'json'); + $contentType = $this->header('Content-Type'); + + if(is_null($contentType)) { + return false; + } + + return str_contains($contentType, 'json'); } /** @@ -506,7 +512,13 @@ public function isJson(): bool */ public function isXml(): bool { - return str_contains($this->header('Content-Type'), 'xml'); + $contentType = $this->header('Content-Type'); + + if(is_null($contentType)) { + return false; + } + + return str_contains($contentType, 'xml'); } /** From 7aaa21da9d8fb916686068fde1adf9aea63a3610 Mon Sep 17 00:00:00 2001 From: Mustafa BALCI Date: Fri, 10 Jan 2025 11:35:21 +0300 Subject: [PATCH 4/4] Add tests for JSON and XML response detection in ResponseTest --- tests/Unit/ResponseTest.php | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/Unit/ResponseTest.php b/tests/Unit/ResponseTest.php index e63878d4..14d8706e 100644 --- a/tests/Unit/ResponseTest.php +++ b/tests/Unit/ResponseTest.php @@ -408,3 +408,57 @@ expect($response->yee())->toEqual('haw'); }); + +test('can determine if response is JSON', function () { + $mockClient = new MockClient([ + // JSON content type + MockResponse::make(['foo' => 'bar'], 200, ['Content-Type' => 'application/json']), + // JSON with charset + MockResponse::make(['foo' => 'bar'], 200, ['Content-Type' => 'application/json; charset=utf-8']), + // Non-JSON content type + MockResponse::make('plain text', 200, ['Content-Type' => 'text/plain']), + // No content type + MockResponse::make('no content type', 200, []), + ]); + + $connector = connector(); + + $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(); + + $response = $connector->send(new UserRequest, $mockClient); + expect($response->isJson())->toBeFalse(); +}); + +test('can determine if response is XML', function () { + $mockClient = new MockClient([ + // XML content type + MockResponse::make('', 200, ['Content-Type' => 'application/xml']), + // XML with charset + MockResponse::make('', 200, ['Content-Type' => 'text/xml; charset=utf-8']), + // Non-XML content type + MockResponse::make('plain text', 200, ['Content-Type' => 'text/plain']), + // No content type + MockResponse::make('no content type', 200, []), + ]); + + $connector = connector(); + + $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(); + + $response = $connector->send(new UserRequest, $mockClient); + expect($response->isXml())->toBeFalse(); +});