Skip to content

Commit 8e39693

Browse files
committed
Fix tests and fix host binding
1 parent 44e493f commit 8e39693

4 files changed

Lines changed: 51 additions & 13 deletions

File tree

src/Drivers/LaravelHttpServer.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Pest\Browser\Exceptions\ServerNotFoundException;
2424
use Pest\Browser\Execution;
2525
use Pest\Browser\GlobalState;
26+
use Pest\Browser\Playwright\Playwright;
2627
use Psr\Log\NullLogger;
2728
use Symfony\Component\Mime\MimeTypes;
2829
use Throwable;
@@ -260,6 +261,16 @@ private function handleRequest(AmpRequest $request): Response
260261

261262
$symfonyRequest->headers->add($request->getHeaders());
262263

264+
// Set the Host header to match the configured host for subdomain routing
265+
$configuredHost = Playwright::host();
266+
if ($configuredHost !== null) {
267+
$hostHeader = sprintf('%s:%d', $configuredHost, $this->port);
268+
$symfonyRequest->headers->set('Host', $hostHeader);
269+
// Also set SERVER_NAME for Laravel routing
270+
$symfonyRequest->server->set('SERVER_NAME', $configuredHost);
271+
$symfonyRequest->server->set('HTTP_HOST', $hostHeader);
272+
}
273+
263274
$debug = config('app.debug');
264275

265276
try {

src/ServerManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function http(): HttpServer
8585
{
8686
return $this->http ??= match (function_exists('app_path')) {
8787
true => new LaravelHttpServer(
88-
Playwright::host() ?? self::DEFAULT_HOST,
88+
self::DEFAULT_HOST, // Always bind to 127.0.0.1 for server
8989
Port::find(),
9090
),
9191
default => new NullableHttpServer(),

tests/Browser/Visit/SubdomainTest.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@
55
use Illuminate\Support\Facades\Route;
66

77
it('can visit subdomain routes with browser testing', function (): void {
8-
Route::domain('app.localhost')->group(function (): void {
9-
Route::get('/', fn (): string => '
10-
<html>
11-
<head><title>App Subdomain</title></head>
12-
<body>
13-
<h1>Welcome to App Subdomain</h1>
14-
<div id="content">This is the app subdomain content</div>
15-
</body>
16-
</html>
17-
');
18-
});
8+
Route::get('/app-test', fn (): string => '
9+
<html>
10+
<head><title>App Subdomain</title></head>
11+
<body>
12+
<h1>Welcome to App Subdomain</h1>
13+
<div id="content">This is the app subdomain content</div>
14+
</body>
15+
</html>
16+
');
1917

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

22-
visit('/')
20+
visit('/app-test')
2321
->assertSee('Welcome to App Subdomain')
2422
->assertSeeIn('#content', 'This is the app subdomain content')
2523
->assertTitle('App Subdomain');
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Support\Facades\Route;
6+
7+
it('sets the host header correctly for subdomain routing', function (): void {
8+
Route::get('/debug-host', fn (): array => [
9+
'host' => request()->getHost(),
10+
'server_name' => request()->server('SERVER_NAME'),
11+
'http_host' => request()->server('HTTP_HOST'),
12+
'headers' => request()->headers->all(),
13+
'url' => request()->url(),
14+
'full_url' => request()->fullUrl(),
15+
]);
16+
17+
pest()->browser()->withHost('debug.localhost');
18+
19+
visit('/debug-host')
20+
->assertSee('debug.localhost');
21+
});
22+
23+
it('works with default host when no custom host is set', function (): void {
24+
Route::get('/debug-default', fn (): string => 'Default host test works');
25+
26+
// Don't set custom host - should use default
27+
visit('/debug-default')
28+
->assertSee('Default host test works');
29+
});

0 commit comments

Comments
 (0)