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
31 changes: 24 additions & 7 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use Throwable;
use LogicException;
use function implode;
use SimpleXMLElement;
use function is_array;
use function mb_strtolower;
use Saloon\Traits\Macroable;
use InvalidArgumentException;
use Saloon\Helpers\ArrayHelpers;
Expand Down Expand Up @@ -486,35 +489,49 @@ public function throw(): static
*/
public function header(string $header): string|array|null
{
return $this->headers()->get($header);
if (! $this->psrResponse->hasHeader($header)) {
return null;
}

$values = $this->psrResponse->getHeader($header);

return count($values) === 1 ? $values[0] : $values;
}

/**
* Determine if the response is in JSON format.
*/
public function isJson(): bool
{
$contentType = $this->psrResponse->getHeaderLine('Content-Type');
$contentType = $this->header('Content-Type');

if ($contentType === '') {
if (empty($contentType)) {
return false;
}

return str_contains($contentType, 'json');
if (is_array($contentType)) {
$contentType = implode(',', $contentType);
}

return str_contains(mb_strtolower($contentType), 'json');
}

/**
* Determine if the response is in XML format.
*/
public function isXml(): bool
{
$contentType = $this->psrResponse->getHeaderLine('Content-Type');
$contentType = $this->header('Content-Type');

if ($contentType === '') {
if (empty($contentType)) {
return false;
}

return str_contains($contentType, 'xml');
if (is_array($contentType)) {
$contentType = implode(',', $contentType);
}

return str_contains(mb_strtolower($contentType), 'xml');
}

/**
Expand Down
33 changes: 33 additions & 0 deletions tests/Unit/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,36 @@
$response = $connector->send(new UserRequest, $mockClient);
expect($response->isXml())->toBeFalse();
});

test('header lookup is case-insensitive per HTTP RFC', function () {
$mockClient = new MockClient([
MockResponse::make([], 200, ['x-my-custom-header' => 'custom-value']),
]);

$response = connector()->send(new UserRequest, $mockClient);

expect($response->header('X-My-Custom-Header'))->toEqual('custom-value');
expect($response->header('x-my-custom-header'))->toEqual('custom-value');
});

test('isJson lookup can use case insensitive headers', function () {
$mockClient = new MockClient([
MockResponse::make([], 200, ['content-type' => 'application/JSON']),
]);

$response = connector()->send(new UserRequest, $mockClient);

expect($response->isJson())->toBeTrue();
expect($response->isXml())->toBeFalse();
});

test('isXml lookup can use case insensitive headers', function () {
$mockClient = new MockClient([
MockResponse::make([], 200, ['content-type' => 'text/xml']),
]);

$response = connector()->send(new UserRequest, $mockClient);

expect($response->isXml())->toBeTrue();
expect($response->isJson())->toBeFalse();
});