Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6338fcb

Browse files
authored
Merge pull request#64 from SRWieZ/feature/nightwatch-logging-middleware
Add Nightwatch middleware and corresponding tests
2 parents7fb7ddd +f1ef548 commit6338fcb

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+
namespaceSaloon\Laravel\Http\Middleware;
6+
7+
useSaloon\Http\PendingRequest;
8+
useSaloon\Contracts\RequestMiddleware;
9+
10+
class NightwatchMiddlewareimplements RequestMiddleware
11+
{
12+
/**
13+
* Apply Nightwatch middleware to Guzzle requests when using GuzzleSender
14+
*/
15+
publicfunction__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 ($senderinstanceof \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
useSaloon\Laravel\Http\Middleware\SendRequestEvent;
2121
useSaloon\Laravel\Http\Middleware\SendResponseEvent;
2222
useSaloon\Laravel\Console\Commands\MakeAuthenticator;
23+
useSaloon\Laravel\Http\Middleware\NightwatchMiddleware;
2324

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

6162
Config::globalMiddleware()
6263
->onRequest(newMockMiddleware,'laravelMock')
64+
->onRequest(newNightwatchMiddleware,'laravelNightwatch')
6365
->onRequest(newSendRequestEvent,'laravelSendRequestEvent', PipeOrder::LAST)
6466
->onResponse(newRecordResponse,'laravelRecordResponse', PipeOrder::FIRST)
6567
->onResponse(newSendResponseEvent,'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+
useSaloon\Http\PendingRequest;
6+
useSaloon\Laravel\Tests\Fixtures\Requests\UserRequest;
7+
useSaloon\Laravel\Http\Middleware\NightwatchMiddleware;
8+
useSaloon\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 =newUserRequest();
13+
$pendingRequest =newPendingRequest($connector,$request);
14+
15+
$middleware =newNightwatchMiddleware();
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 =newUserRequest();
26+
$pendingRequest =newPendingRequest($connector,$request);
27+
28+
$middleware =newNightwatchMiddleware();
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)

[8]ページ先頭

©2009-2025 Movatter.jp