Skip to content

Commit

Permalink
Add tests for JSON and XML response detection in ResponseTest
Browse files Browse the repository at this point in the history
  • Loading branch information
mstfblci committed Jan 10, 2025
1 parent c98b1bb commit 7aaa21d
Showing 1 changed file with 54 additions and 0 deletions.
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();
});

0 comments on commit 7aaa21d

Please sign in to comment.