diff --git a/src/Http/Response.php b/src/Http/Response.php
index bc2ed81b..005026bc 100644
--- a/src/Http/Response.php
+++ b/src/Http/Response.php
@@ -489,6 +489,38 @@ 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
+ {
+ $contentType = $this->header('Content-Type');
+
+ if(is_null($contentType)) {
+ return false;
+ }
+
+ return str_contains($contentType, 'json');
+ }
+
+ /**
+ * Determine if the response is in XML format.
+ *
+ * @return bool
+ */
+ public function isXml(): bool
+ {
+ $contentType = $this->header('Content-Type');
+
+ if(is_null($contentType)) {
+ return false;
+ }
+
+ return str_contains($contentType, 'xml');
+ }
+
/**
* Create a temporary resource for the stream.
*
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();
+});