Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/v2' into global-middleware-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Nov 16, 2023
2 parents 1c83377 + dd8986e commit 2d14266
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/HttpSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ protected function createRequestOptions(PendingRequest $pendingRequest, RequestI
// deal with PSR requests. We'll add the headers from the PSR
// request to ensure the lowest-level values possible.

$config->add(RequestOptions::HEADERS, $psrRequest->getHeaders());
$config->add(RequestOptions::HEADERS, array_merge(
$psrRequest->getHeaders(),
$config->get(RequestOptions::HEADERS, [])
));

return $config->all();
}
Expand Down
26 changes: 26 additions & 0 deletions tests/Feature/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,29 @@

$connector->send($request);
});

test('you can pass additional headers that will be merged with the default headers from the psr request', function () {
$connector = new HttpSenderConnector();

$connector->config()->add('debug', true);

$connector->sender()->addMiddleware(function (callable $handler) use ($connector) {
return function (RequestInterface $guzzleRequest, array $options) use ($connector) {
expect($guzzleRequest->getHeaders())->toBe([
'Host' => ['tests.saloon.dev'],
'Accept' => ['application/json'],
'User-Agent' => ['Saloon'],
]);

$factoryCollection = $connector->sender()->getFactoryCollection();

return new FulfilledPromise(MockResponse::make()->createPsrResponse($factoryCollection->responseFactory, $factoryCollection->streamFactory));
};
});

$request = new UserRequest;

$request->config()->add('headers', ['User-Agent' => 'Saloon']);

$connector->send($request);
});

0 comments on commit 2d14266

Please sign in to comment.