diff --git a/tests/Feature/StrayHttpRequestGuardTest.php b/tests/Feature/StrayHttpRequestGuardTest.php new file mode 100644 index 00000000..8b40470a --- /dev/null +++ b/tests/Feature/StrayHttpRequestGuardTest.php @@ -0,0 +1,16 @@ +throws(StrayRequestException::class); + +test('a faked request still goes through when a matching fake is registered', function () { + Http::fake(['example.com/*' => Http::response(['ok' => true])]); + + $response = Http::get('https://example.com/allowed'); + + expect($response->json('ok'))->toBeTrue(); +}); diff --git a/tests/Pest.php b/tests/Pest.php index d3d6531f..69540061 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -14,6 +14,7 @@ use App\Services\Banking\Sync\BankingConnectionSyncerFactory; use App\Services\Banking\TransactionSyncService; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Http; use Stripe\Collection as StripeCollection; use Stripe\Service\SubscriptionService; use Stripe\StripeClient; @@ -52,6 +53,20 @@ pest()->beforeEach(function () { $this->withoutVite(); })->in('Feature', 'Performance'); +/* +|-------------------------------------------------------------------------- +| Block stray HTTP requests in Feature tests +|-------------------------------------------------------------------------- +| +| Any Feature test whose code path hits the network without a matching +| Http::fake() should fail loudly instead of making a real request. Tests +| that legitimately talk to external services register their own fakes, +| which take precedence over this guard. +*/ +pest()->beforeEach(function () { + Http::preventStrayRequests(); +})->in('Feature'); + /* |-------------------------------------------------------------------------- | Expectations