Skip to content

Commit 43d2c78

Browse files
pokyunnSammyjo20
andauthored
[4.x] Fix: Null header deprecated notice for PSR requests (#554)
* Fix: Null header deprecated notice for PSR requests Notice: Since guzzlehttp/psr7 2.11: Passing null to MessageInterface::withHeader() is deprecated; guzzlehttp/psr7 3.0 requires string|string[]. * Add tests to null-header to string conversion * Remove unused import * Remove debug * Fix typos and cleanup --------- Co-authored-by: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com>
1 parent 538994a commit 43d2c78

4 files changed

Lines changed: 54 additions & 2 deletions

File tree

‎src/Http/Faking/FakeResponse.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function createPsrResponse(ResponseFactoryInterface $responseFactory, Str
124124
$response = $responseFactory->createResponse($this->status());
125125

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

130130
return $response->withBody($this->body()->toStream($streamFactory));

‎src/Traits/PendingRequest/ManagesPsrRequests.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function createPsrRequest(): RequestInterface
4848
);
4949

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

5454
if ($this->body() instanceof BodyRepository) {

‎tests/Feature/PsrTest.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use GuzzleHttp\Psr7\Uri;
66
use Saloon\Http\Faking\MockClient;
77
use Saloon\Http\Faking\MockResponse;
8+
use Saloon\Tests\Fixtures\Connectors\TestConnector;
9+
use Saloon\Tests\Fixtures\Requests\NullHeaderRequest;
810
use Saloon\Tests\Fixtures\Requests\ModifiedPsrUserRequest;
911
use Saloon\Tests\Fixtures\Connectors\ModifiedPsrRequestConnector;
1012

@@ -26,3 +28,20 @@
2628

2729
expect($response->getPsrRequest()->getHeaders())->toHaveKey('X-Howdy', ['Yeehaw']);
2830
});
31+
32+
test('The psr request headers must be converted to empty string', function () {
33+
$mockClient = new MockClient([
34+
MockResponse::make(headers: ['X-Null-Header' => null]),
35+
]);
36+
37+
$connector = new TestConnector();
38+
$connector->withMockClient($mockClient);
39+
40+
$response = $connector->send(new NullHeaderRequest());
41+
42+
// The request will convert null to empty string
43+
expect($response->getPsrRequest()->getHeader('X-Null-Header')[0])->toBe('');
44+
45+
// The response will convert null header to empty string
46+
expect($response->headers()->get('X-Null-Header'))->toBe('');
47+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Saloon\Tests\Fixtures\Requests;
6+
7+
use Saloon\Enums\Method;
8+
use Saloon\Http\Request;
9+
10+
class NullHeaderRequest extends Request
11+
{
12+
/**
13+
* Define the method that the request will use.
14+
*
15+
* @var string
16+
*/
17+
protected Method $method = Method::GET;
18+
19+
/**
20+
* Define the endpoint for the request.
21+
*/
22+
public function resolveEndpoint(): string
23+
{
24+
return '/user';
25+
}
26+
27+
protected function defaultHeaders(): array
28+
{
29+
return [
30+
'X-Null-Header' => null,
31+
];
32+
}
33+
}

0 commit comments

Comments
 (0)