Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand All @@ -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');
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();

Expand All @@ -442,6 +447,8 @@
MockResponse::make('<?xml version="1.0"?><root></root>', 200, ['Content-Type' => 'application/xml']),
// XML with charset
MockResponse::make('<?xml version="1.0"?><root></root>', 200, ['Content-Type' => 'text/xml; charset=utf-8']),
// XML with lowercase header (e.g. FastAPI, HTTP/2)
MockResponse::make('<?xml version="1.0"?><root></root>', 200, ['content-type' => 'application/xml']),
// Non-XML content type
MockResponse::make('plain text', 200, ['Content-Type' => 'text/plain']),
// No content type
Expand All @@ -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();

Expand Down