Skip to content
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
2 changes: 1 addition & 1 deletion src/Http/Faking/FakeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function createPsrResponse(ResponseFactoryInterface $responseFactory, Str
$response = $responseFactory->createResponse($this->status());

foreach ($this->headers()->all() as $headerName => $headerValue) {
$response = $response->withHeader($headerName, $headerValue);
$response = $response->withHeader($headerName, $headerValue ?? '');
}

return $response->withBody($this->body()->toStream($streamFactory));
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/PendingRequest/ManagesPsrRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function createPsrRequest(): RequestInterface
);

foreach ($this->headers()->all() as $headerName => $headerValue) {
$request = $request->withHeader($headerName, $headerValue);
$request = $request->withHeader($headerName, $headerValue ?? '');
}

if ($this->body() instanceof BodyRepository) {
Expand Down
19 changes: 19 additions & 0 deletions tests/Feature/PsrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use GuzzleHttp\Psr7\Uri;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;
use Saloon\Tests\Fixtures\Connectors\TestConnector;
use Saloon\Tests\Fixtures\Requests\NullHeaderRequest;
use Saloon\Tests\Fixtures\Requests\ModifiedPsrUserRequest;
use Saloon\Tests\Fixtures\Connectors\ModifiedPsrRequestConnector;

Expand All @@ -26,3 +28,20 @@

expect($response->getPsrRequest()->getHeaders())->toHaveKey('X-Howdy', ['Yeehaw']);
});

test('The psr request headers must be converted to empty string', function () {
$mockClient = new MockClient([
MockResponse::make(headers: ['X-Null-Header' => null]),
]);

$connector = new TestConnector();
$connector->withMockClient($mockClient);

$response = $connector->send(new NullHeaderRequest());

// The request will convert null to empty string
expect($response->getPsrRequest()->getHeader('X-Null-Header')[0])->toBe('');

// The response will convert null header to empty string
expect($response->headers()->get('X-Null-Header'))->toBe('');
});
33 changes: 33 additions & 0 deletions tests/Fixtures/Requests/NullHeaderRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Saloon\Tests\Fixtures\Requests;

use Saloon\Enums\Method;
use Saloon\Http\Request;

class NullHeaderRequest extends Request
{
/**
* Define the method that the request will use.
*
* @var string
*/
protected Method $method = Method::GET;

/**
* Define the endpoint for the request.
*/
public function resolveEndpoint(): string
{
return '/user';
}

protected function defaultHeaders(): array
{
return [
'X-Null-Header' => null,
];
}
}