Repository navigation
[5.x] Fix canonical URL handling for subdomain routing #258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e55137c
fix canonical URL handling for subdomain routing
hafezdivandari 4e96b48
raise Amp body size limit to allow large JSON request bodies
hafezdivandari b153845
address review feedback on canonical url handling
hafezdivandari efd9b6d
Revert "raise Amp body size limit to allow large JSON request bodies"
hafezdivandari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Illuminate\Http\Request; | ||
| use Illuminate\Support\Facades\Route; | ||
| use Pest\Browser\ServerManager; | ||
|
|
||
| afterEach(function (): void { | ||
| pest()->browser()->withHost(null); | ||
| }); | ||
|
|
||
| it('exposes the canonical host on request()->url() when withHost is set', function (): void { | ||
| Route::domain('app.localhost')->get('/canonical/request-url', fn (Request $request) => $request->url()); | ||
|
|
||
| pest()->browser()->withHost('app.localhost'); | ||
|
|
||
| $port = ServerManager::instance()->http()->port; // @phpstan-ignore-line | ||
|
|
||
| visit('/canonical/request-url') | ||
| ->assertUrlIs("http://app.localhost:{$port}/canonical/request-url") | ||
| ->assertSee("http://app.localhost:{$port}/canonical/request-url"); | ||
| }); | ||
|
|
||
| it('generates route() URLs against the canonical host when withHost is set', function (): void { | ||
| Route::domain('app.localhost')->as('canonical.test')->get('/canonical/route-url', fn (): string => route('canonical.test')); | ||
|
|
||
| pest()->browser()->withHost('app.localhost'); | ||
|
|
||
| $port = ServerManager::instance()->http()->port; // @phpstan-ignore-line | ||
|
|
||
| visit('/canonical/route-url') | ||
| ->assertSee("http://app.localhost:{$port}/canonical/route-url"); | ||
|
|
||
| expect(route('canonical.test'))->toBe("http://app.localhost:{$port}/canonical/route-url"); | ||
| }); | ||
|
|
||
| it('reverts to the bound IP origin when withHost(null) clears the configured host', function (): void { | ||
| Route::as('canonical.no-host')->get('/canonical/no-host', fn (): string => route('canonical.no-host')); | ||
|
|
||
| $port = ServerManager::instance()->http()->port; // @phpstan-ignore-line | ||
|
|
||
| visit('/canonical/no-host') | ||
| ->assertSee("http://127.0.0.1:{$port}/canonical/no-host"); | ||
|
|
||
| expect(route('canonical.no-host'))->toBe("http://127.0.0.1:{$port}/canonical/no-host"); | ||
| }); | ||
|
|
||
| it('updates the URL generator immediately when withHost changes mid-test', function (): void { | ||
| Route::domain('first.localhost')->as('canonical.first')->get('/canonical/first', fn (): string => route('canonical.first')); | ||
| Route::domain('second.localhost')->as('canonical.second')->get('/canonical/second', fn (): string => route('canonical.second')); | ||
|
|
||
| $port = ServerManager::instance()->http()->port; // @phpstan-ignore-line | ||
|
|
||
| visit('/canonical/first') | ||
| ->withHost('first.localhost') | ||
| ->assertSee("http://first.localhost:{$port}/canonical/first"); | ||
|
|
||
| expect(route('canonical.first'))->toBe("http://first.localhost:{$port}/canonical/first"); | ||
|
|
||
| // Per-request resync should also surface the new host on the page itself. | ||
| visit('/canonical/second') | ||
| ->withHost('second.localhost') | ||
| ->assertSee("http://second.localhost:{$port}/canonical/second"); | ||
|
|
||
| expect(route('canonical.second'))->toBe("http://second.localhost:{$port}/canonical/second"); | ||
| }); | ||
|
|
||
| it('rewrites asset URLs to the configured host so scripts stay same-origin', function (): void { | ||
| @file_put_contents(public_path('canonical-asset.js'), "console.log('base http://localhost');"); | ||
|
|
||
| pest()->browser()->withHost('assets.localhost'); | ||
|
|
||
| visit('/canonical-asset.js') | ||
| ->assertSee('http://assets.localhost') | ||
| ->assertDontSee('http://127.0.0.1'); | ||
| }); | ||
|
|
||
| it('updates generated URLs as soon as withHost changes, before the next request', function (): void { | ||
| Route::as('canonical.plain')->get('/canonical/plain', fn (): string => 'plain'); | ||
|
|
||
| $port = ServerManager::instance()->http()->port; // @phpstan-ignore-line | ||
|
|
||
| pest()->browser()->withHost('one.localhost'); | ||
| visit('/canonical/plain')->assertSee('plain'); | ||
|
|
||
| expect(route('canonical.plain'))->toBe("http://one.localhost:{$port}/canonical/plain"); | ||
|
|
||
| // No request in between: the URL generator should already reflect the new host. | ||
| pest()->browser()->withHost('two.localhost'); | ||
|
|
||
| expect(route('canonical.plain'))->toBe("http://two.localhost:{$port}/canonical/plain"); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.