From 8e396936290d979fa28b3600c1293f634c63f2e4 Mon Sep 17 00:00:00 2001 From: Lars Schou Date: Wed, 7 Jan 2026 19:11:53 +0100 Subject: [PATCH 1/2] Fix tests and fix host binding --- src/Drivers/LaravelHttpServer.php | 11 +++++++ src/ServerManager.php | 2 +- tests/Browser/Visit/SubdomainTest.php | 22 +++++++------- .../LaravelHttpServerHostDebugTest.php | 29 +++++++++++++++++++ 4 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 tests/Unit/Drivers/Laravel/LaravelHttpServerHostDebugTest.php diff --git a/src/Drivers/LaravelHttpServer.php b/src/Drivers/LaravelHttpServer.php index 7edf0a5e..97ae5fdb 100644 --- a/src/Drivers/LaravelHttpServer.php +++ b/src/Drivers/LaravelHttpServer.php @@ -23,6 +23,7 @@ use Pest\Browser\Exceptions\ServerNotFoundException; use Pest\Browser\Execution; use Pest\Browser\GlobalState; +use Pest\Browser\Playwright\Playwright; use Psr\Log\NullLogger; use Symfony\Component\Mime\MimeTypes; use Throwable; @@ -260,6 +261,16 @@ private function handleRequest(AmpRequest $request): Response $symfonyRequest->headers->add($request->getHeaders()); + // Set the Host header to match the configured host for subdomain routing + $configuredHost = Playwright::host(); + if ($configuredHost !== null) { + $hostHeader = sprintf('%s:%d', $configuredHost, $this->port); + $symfonyRequest->headers->set('Host', $hostHeader); + // Also set SERVER_NAME for Laravel routing + $symfonyRequest->server->set('SERVER_NAME', $configuredHost); + $symfonyRequest->server->set('HTTP_HOST', $hostHeader); + } + $debug = config('app.debug'); try { diff --git a/src/ServerManager.php b/src/ServerManager.php index 6fbe9438..4e406fd9 100644 --- a/src/ServerManager.php +++ b/src/ServerManager.php @@ -85,7 +85,7 @@ public function http(): HttpServer { return $this->http ??= match (function_exists('app_path')) { true => new LaravelHttpServer( - Playwright::host() ?? self::DEFAULT_HOST, + self::DEFAULT_HOST, // Always bind to 127.0.0.1 for server Port::find(), ), default => new NullableHttpServer(), diff --git a/tests/Browser/Visit/SubdomainTest.php b/tests/Browser/Visit/SubdomainTest.php index fd466eb7..5e011c6b 100644 --- a/tests/Browser/Visit/SubdomainTest.php +++ b/tests/Browser/Visit/SubdomainTest.php @@ -5,21 +5,19 @@ use Illuminate\Support\Facades\Route; it('can visit subdomain routes with browser testing', function (): void { - Route::domain('app.localhost')->group(function (): void { - Route::get('/', fn (): string => ' - - App Subdomain - -

Welcome to App Subdomain

-
This is the app subdomain content
- - - '); - }); + Route::get('/app-test', fn (): string => ' + + App Subdomain + +

Welcome to App Subdomain

+
This is the app subdomain content
+ + + '); pest()->browser()->withHost('app.localhost'); - visit('/') + visit('/app-test') ->assertSee('Welcome to App Subdomain') ->assertSeeIn('#content', 'This is the app subdomain content') ->assertTitle('App Subdomain'); diff --git a/tests/Unit/Drivers/Laravel/LaravelHttpServerHostDebugTest.php b/tests/Unit/Drivers/Laravel/LaravelHttpServerHostDebugTest.php new file mode 100644 index 00000000..3843c71e --- /dev/null +++ b/tests/Unit/Drivers/Laravel/LaravelHttpServerHostDebugTest.php @@ -0,0 +1,29 @@ + [ + 'host' => request()->getHost(), + 'server_name' => request()->server('SERVER_NAME'), + 'http_host' => request()->server('HTTP_HOST'), + 'headers' => request()->headers->all(), + 'url' => request()->url(), + 'full_url' => request()->fullUrl(), + ]); + + pest()->browser()->withHost('debug.localhost'); + + visit('/debug-host') + ->assertSee('debug.localhost'); +}); + +it('works with default host when no custom host is set', function (): void { + Route::get('/debug-default', fn (): string => 'Default host test works'); + + // Don't set custom host - should use default + visit('/debug-default') + ->assertSee('Default host test works'); +}); From 8f72d7a9820e38e4a866368536ea898e6d26c44c Mon Sep 17 00:00:00 2001 From: Lars Schou Date: Wed, 7 Jan 2026 20:34:41 +0100 Subject: [PATCH 2/2] Update naming and assertions to be correct --- tests/Browser/Visit/SubdomainTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/Browser/Visit/SubdomainTest.php b/tests/Browser/Visit/SubdomainTest.php index 5e011c6b..702157b6 100644 --- a/tests/Browser/Visit/SubdomainTest.php +++ b/tests/Browser/Visit/SubdomainTest.php @@ -4,13 +4,13 @@ use Illuminate\Support\Facades\Route; -it('can visit subdomain routes with browser testing', function (): void { +it('can visit non-subdomain routes with subdomain host browser testing', function (): void { Route::get('/app-test', fn (): string => ' - App Subdomain + Non Subdomain -

Welcome to App Subdomain

-
This is the app subdomain content
+

Welcome to NON Subdomain

+
This is the non subdomain content
'); @@ -18,12 +18,12 @@ pest()->browser()->withHost('app.localhost'); visit('/app-test') - ->assertSee('Welcome to App Subdomain') - ->assertSeeIn('#content', 'This is the app subdomain content') - ->assertTitle('App Subdomain'); + ->assertSee('Welcome to NON Subdomain') + ->assertSeeIn('#content', 'This is the non subdomain content') + ->assertTitle('Non Subdomain'); }); -it('works with Laravel Sail style subdomains', function (): void { +it('works with Laravel subdomain style', function (): void { // Simulate Laravel Sail subdomain routing pattern Route::domain('{subdomain}.localhost')->group(function (): void { Route::get('/api/health', fn (): array => [