Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature | Added isJson and isXML functions to Response class #455

Open
wants to merge 4 commits into
base: v3
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
54 changes: 54 additions & 0 deletions tests/Unit/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<?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']),
// 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();
});