Skip to content

Commit 6338fcb

Browse files
authored
Merge pull request #64 from SRWieZ/feature/nightwatch-logging-middleware
Add Nightwatch middleware and corresponding tests
2 parents 7fb7ddd + f1ef548 commit 6338fcb

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Saloon\Laravel\Http\Middleware;
6+
7+
use Saloon\Http\PendingRequest;
8+
use Saloon\Contracts\RequestMiddleware;
9+
10+
class NightwatchMiddleware implements RequestMiddleware
11+
{
12+
/**
13+
* Apply Nightwatch middleware to Guzzle requests when using GuzzleSender
14+
*/
15+
public function __invoke(PendingRequest $pendingRequest): void
16+
{
17+
$sender = $pendingRequest->getConnector()->sender();
18+
19+
// Check if Nightwatch is installed
20+
if (! class_exists('Nightwatch\\Nightwatch')) {
21+
return;
22+
}
23+
24+
// Check if we're using GuzzleSender
25+
if ($sender instanceof \Saloon\Http\Senders\GuzzleSender === false) {
26+
return;
27+
}
28+
29+
$sender->addMiddleware(\Nightwatch\Nightwatch::guzzleMiddleware(), 'nightwatch');
30+
}
31+
32+
}

src/SaloonServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Saloon\Laravel\Http\Middleware\SendRequestEvent;
2121
use Saloon\Laravel\Http\Middleware\SendResponseEvent;
2222
use Saloon\Laravel\Console\Commands\MakeAuthenticator;
23+
use Saloon\Laravel\Http\Middleware\NightwatchMiddleware;
2324

2425
class SaloonServiceProvider extends ServiceProvider
2526
{
@@ -60,6 +61,7 @@ public function boot(): void
6061

6162
Config::globalMiddleware()
6263
->onRequest(new MockMiddleware, 'laravelMock')
64+
->onRequest(new NightwatchMiddleware, 'laravelNightwatch')
6365
->onRequest(new SendRequestEvent, 'laravelSendRequestEvent', PipeOrder::LAST)
6466
->onResponse(new RecordResponse, 'laravelRecordResponse', PipeOrder::FIRST)
6567
->onResponse(new SendResponseEvent, 'laravelSendResponseEvent', PipeOrder::FIRST);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Saloon\Http\PendingRequest;
6+
use Saloon\Laravel\Tests\Fixtures\Requests\UserRequest;
7+
use Saloon\Laravel\Http\Middleware\NightwatchMiddleware;
8+
use Saloon\Laravel\Tests\Fixtures\Connectors\TestConnector;
9+
10+
test('nightwatch middleware is invoked without errors when nightwatch is not available', function () {
11+
$connector = TestConnector::make();
12+
$request = new UserRequest();
13+
$pendingRequest = new PendingRequest($connector, $request);
14+
15+
$middleware = new NightwatchMiddleware();
16+
17+
// Should not throw any exceptions even when Nightwatch is not available
18+
expect(function () use ($middleware, $pendingRequest) {
19+
$middleware($pendingRequest);
20+
})->not->toThrow(Exception::class);
21+
});
22+
23+
test('nightwatch middleware checks for guzzle sender', function () {
24+
$connector = TestConnector::make();
25+
$request = new UserRequest();
26+
$pendingRequest = new PendingRequest($connector, $request);
27+
28+
$middleware = new NightwatchMiddleware();
29+
30+
// Should handle gracefully for any sender type
31+
expect(function () use ($middleware, $pendingRequest) {
32+
$middleware($pendingRequest);
33+
})->not->toThrow(Exception::class);
34+
});

0 commit comments

Comments
 (0)