|
3 | 3 | declare(strict_types=1); |
4 | 4 |
|
5 | 5 | use Illuminate\Support\Facades\Route; |
| 6 | +use Pest\Browser\Playwright\Playwright; |
6 | 7 |
|
7 | 8 | it('can visit non-subdomain routes with subdomain host browser testing', function (): void { |
8 | 9 | Route::get('/app-test', fn (): string => ' |
|
40 | 41 | ->assertSee('"subdomain":"api"') |
41 | 42 | ->assertSee('"host":"api.localhost"'); |
42 | 43 | }); |
| 44 | + |
| 45 | +it('Can chain withHost on visit', function (): void { |
| 46 | + Route::domain('{subdomain}.localhost')->group(function (): void { |
| 47 | + Route::get('/api/health', fn (): array => [ |
| 48 | + 'status' => 'ok', |
| 49 | + 'subdomain' => request()->route('subdomain'), |
| 50 | + 'host' => request()->getHost(), |
| 51 | + ]); |
| 52 | + }); |
| 53 | + |
| 54 | + visit('/api/health') |
| 55 | + ->withHost('api.localhost') |
| 56 | + ->assertSee('"status":"ok"') |
| 57 | + ->assertSee('"subdomain":"api"') |
| 58 | + ->assertSee('"host":"api.localhost"'); |
| 59 | +}); |
| 60 | + |
| 61 | +it('Chaining withHost will not override global host', function (): void { |
| 62 | + Route::domain('{subdomain}.localhost')->group(function (): void { |
| 63 | + Route::get('/api/health', fn (): array => [ |
| 64 | + 'subdomain' => request()->route('subdomain'), |
| 65 | + 'host' => request()->getHost(), |
| 66 | + ]); |
| 67 | + }); |
| 68 | + |
| 69 | + Route::get('/', fn (): array => [ |
| 70 | + 'host' => request()->getHost(), |
| 71 | + ]); |
| 72 | + |
| 73 | + // Set global host: test.domain |
| 74 | + pest()->browser()->withHost('test.domain'); |
| 75 | + |
| 76 | + // 1. Visit withHost: api.localhost |
| 77 | + visit('/api/health') |
| 78 | + ->withHost('api.localhost') |
| 79 | + ->assertSee('"host":"api.localhost"') |
| 80 | + ->assertDontSee('test.domain'); |
| 81 | + |
| 82 | + // 2. Visit without withHost: should use global host "test.domain" |
| 83 | + visit('/') |
| 84 | + ->assertSee('"host":"test.domain"') |
| 85 | + ->assertDontSee('api.localhost'); |
| 86 | +}); |
| 87 | + |
| 88 | +it('uses the first withHost when chained multiple times', function (): void { |
| 89 | + // Because of the spread operator "...$this->options" in PendingAwaitablePage |
| 90 | + Route::domain('{subdomain}.localhost')->group(function (): void { |
| 91 | + Route::get('/api/info', fn (): array => [ |
| 92 | + 'subdomain' => request()->route('subdomain'), |
| 93 | + 'host' => request()->getHost(), |
| 94 | + ]); |
| 95 | + }); |
| 96 | + |
| 97 | + visit('/api/info') |
| 98 | + ->withHost('first.localhost') |
| 99 | + ->withHost('api.localhost') |
| 100 | + ->assertSee('"host":"first.localhost"') |
| 101 | + ->assertSee('"subdomain":"first"') |
| 102 | + ->assertDontSee('api.localhost'); |
| 103 | +}); |
| 104 | + |
| 105 | +it('withHost works correctly when combined with other options', function (): void { |
| 106 | + Route::domain('{subdomain}.localhost')->group(function (): void { |
| 107 | + Route::get('/api/locale', fn (): array => [ |
| 108 | + 'host' => request()->getHost(), |
| 109 | + 'subdomain' => request()->route('subdomain'), |
| 110 | + ]); |
| 111 | + }); |
| 112 | + |
| 113 | + visit('/api/locale') |
| 114 | + ->withHost('api.localhost') |
| 115 | + ->inDarkMode() |
| 116 | + ->assertSee('"host":"api.localhost"') |
| 117 | + ->assertSee('"subdomain":"api"'); |
| 118 | +}); |
| 119 | + |
| 120 | +it('correctly alternates hosts across multiple visits', function (): void { |
| 121 | + Route::domain('{subdomain}.localhost')->group(function (): void { |
| 122 | + Route::get('/check', fn (): array => [ |
| 123 | + 'host' => request()->getHost(), |
| 124 | + 'subdomain' => request()->route('subdomain'), |
| 125 | + ]); |
| 126 | + }); |
| 127 | + |
| 128 | + // Visit 1: api.localhost |
| 129 | + visit('/check') |
| 130 | + ->withHost('api.localhost') |
| 131 | + ->assertSee('"host":"api.localhost"') |
| 132 | + ->assertSee('"subdomain":"api"'); |
| 133 | + |
| 134 | + // Visit 2: admin.localhost |
| 135 | + visit('/check') |
| 136 | + ->withHost('admin.localhost') |
| 137 | + ->assertSee('"host":"admin.localhost"') |
| 138 | + ->assertSee('"subdomain":"admin"'); |
| 139 | + |
| 140 | + // Visit 3: back to api.localhost |
| 141 | + visit('/check') |
| 142 | + ->withHost('api.localhost') |
| 143 | + ->assertSee('"host":"api.localhost"') |
| 144 | + ->assertSee('"subdomain":"api"'); |
| 145 | +}); |
| 146 | + |
| 147 | +it('withHost works when no global host is configured', function (): void { |
| 148 | + Route::domain('{subdomain}.localhost')->group(function (): void { |
| 149 | + Route::get('/standalone', fn (): array => [ |
| 150 | + 'host' => request()->getHost(), |
| 151 | + 'subdomain' => request()->route('subdomain'), |
| 152 | + ]); |
| 153 | + }); |
| 154 | + |
| 155 | + // No pest()->browser()->withHost() call - just use per-visit withHost |
| 156 | + visit('/standalone') |
| 157 | + ->withHost('custom.localhost') |
| 158 | + ->assertSee('"host":"custom.localhost"') |
| 159 | + ->assertSee('"subdomain":"custom"'); |
| 160 | +}); |
| 161 | + |
| 162 | +it('restores global host even when page creation encounters issues', function (): void { |
| 163 | + Route::get('/restore-check', fn (): array => [ |
| 164 | + 'host' => request()->getHost(), |
| 165 | + ]); |
| 166 | + |
| 167 | + $originalHost = 'original.localhost'; |
| 168 | + pest()->browser()->withHost($originalHost); |
| 169 | + |
| 170 | + // Perform a visit with a different host |
| 171 | + visit('/restore-check') |
| 172 | + ->withHost('temporary.localhost') |
| 173 | + ->assertSee('"host":"temporary.localhost"'); |
| 174 | + |
| 175 | + // Verify global host is still the original after the visit |
| 176 | + expect(Playwright::host())->toBe($originalHost); |
| 177 | + |
| 178 | + // Verify next visit without withHost uses the global host |
| 179 | + visit('/restore-check') |
| 180 | + ->assertSee('"host":"original.localhost"'); |
| 181 | +}); |
0 commit comments