Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Drivers/LaravelHttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/ServerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
32 changes: 15 additions & 17 deletions tests/Browser/Visit/SubdomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@

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 => '
<html>
<head><title>App Subdomain</title></head>
<body>
<h1>Welcome to App Subdomain</h1>
<div id="content">This is the app subdomain content</div>
</body>
</html>
');
});
it('can visit non-subdomain routes with subdomain host browser testing', function (): void {
Route::get('/app-test', fn (): string => '
<html>
<head><title>Non Subdomain</title></head>
<body>
<h1>Welcome to NON Subdomain</h1>
<div id="content">This is the non subdomain content</div>
</body>
</html>
');

pest()->browser()->withHost('app.localhost');

visit('/')
->assertSee('Welcome to App Subdomain')
->assertSeeIn('#content', 'This is the app subdomain content')
->assertTitle('App Subdomain');
visit('/app-test')
->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 => [
Expand Down
29 changes: 29 additions & 0 deletions tests/Unit/Drivers/Laravel/LaravelHttpServerHostDebugTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Route;

it('sets the host header correctly for subdomain routing', function (): void {
Route::get('/debug-host', fn (): array => [
'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');
});