Skip to content

Commit

Permalink
Merge pull request #15 from pascalbaljet/global-middleware-fix
Browse files Browse the repository at this point in the history
Apply global middleware also to Saloon requests
  • Loading branch information
Sammyjo20 committed Dec 1, 2023
2 parents 88ef55c + 6b38001 commit 22d0ecb
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ jobs:
matrix:
os: [ ubuntu-latest, windows-latest ]
php: [ 8.1, 8.2 ]
laravel: [ 9.*, 10.* ]
laravel: [ 10.* ]
stability: [ prefer-lowest, prefer-stable ]
include:
- laravel: 9.*
testbench: 7.*
- laravel: 10.*
testbench: 8.*

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
"homepage": "https://github.com/sammyjo20",
"require": {
"php": "^8.1",
"illuminate/http": "^9.52 || ^10.0",
"saloonphp/saloon": "^3.0"
"illuminate/http": "^10.32",
"saloonphp/saloon": "^3.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.5",
"pestphp/pest": "^1.23",
"phpstan/phpstan": "^1.9",
"spatie/ray": "^1.33",
"orchestra/testbench": "^7.30 || ^8.0"
"orchestra/testbench": "^8.0"
},
"minimum-stability": "stable",
"autoload": {
Expand Down
6 changes: 4 additions & 2 deletions src/HttpPendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ class HttpPendingRequest extends PendingRequest
{
/**
* Constructor
*
* @param array<int, callable> $middleware
*/
public function __construct(Factory $factory = null)
public function __construct(Factory $factory = null, array $middleware = [])
{
parent::__construct($factory);
parent::__construct($factory, $middleware);

$this->options = [];
}
Expand Down
7 changes: 5 additions & 2 deletions src/HttpSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function send(PendingRequest $pendingRequest): Response
(string)$psrRequest->getUri(),
$this->createRequestOptions($pendingRequest, $psrRequest),
);
} catch (ConnectionException|ConnectException $exception) {
} catch (ConnectionException | ConnectException $exception) {
throw new FatalRequestException($exception, $pendingRequest);
}

Expand Down Expand Up @@ -168,7 +168,10 @@ protected function createRequestOptions(PendingRequest $pendingRequest, RequestI
*/
protected function createLaravelPendingRequest(RequestInterface $psrRequest, bool $asynchronous): HttpPendingRequest
{
$httpPendingRequest = new HttpPendingRequest(resolve(Factory::class));
/** @var Factory $httpFactory */
$httpFactory = resolve(Factory::class);

$httpPendingRequest = new HttpPendingRequest($httpFactory, $httpFactory->getGlobalMiddleware());
$httpPendingRequest->setClient($this->client);

$this->laravelMiddleware->setRequest($httpPendingRequest);
Expand Down
26 changes: 26 additions & 0 deletions tests/Feature/GlobalMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

use Saloon\HttpSender\HttpSender;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Config;
use Saloon\HttpSender\Tests\Fixtures\Requests\UserRequest;
use Saloon\HttpSender\Tests\Fixtures\Connectors\HttpSenderConnector;

test('the global middleware of the http client factory is also applied to saloon requests', function () {
Config::set('saloon.default_sender', HttpSender::class);

$globalMiddlewareWasCalled = false;

Http::globalRequestMiddleware(function ($request) use (&$globalMiddlewareWasCalled) {
$globalMiddlewareWasCalled = true;

return $request;
});

$connector = new HttpSenderConnector;
$connector->send(new UserRequest);

expect($globalMiddlewareWasCalled)->toBeTrue();
});

0 comments on commit 22d0ecb

Please sign in to comment.