Skip to content
Draft
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
5 changes: 5 additions & 0 deletions src/Bootstrappers/UrlGeneratorBootstrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Overrides\TenancyUrlGenerator;
Expand Down Expand Up @@ -78,6 +79,10 @@ protected function useTenancyUrlGenerator(Tenant $tenant): void
}
}

// Inherit scheme (http/https) from the original generator
$originalScheme = Str::before($this->originalUrlGenerator->formatScheme(), '://');
$newGenerator->forceScheme($originalScheme);
Comment on lines +83 to +84
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does formatScheme() return if no scheme was forced? Is it just http://?

Wondering if we can detect if a scheme was forced so we do not force a scheme if a scheme was not forced on the original generator.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests should also probably first start with a case where the scheme was not forced.


$newGenerator->defaults($defaultParameters);

$newGenerator->setSessionResolver(function () {
Expand Down
35 changes: 34 additions & 1 deletion tests/Bootstrappers/UrlGeneratorBootstrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByRequestDataException;
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
use Stancl\Tenancy\Resolvers\RequestDataTenantResolver;

use function Stancl\Tenancy\Tests\pest;

beforeEach(function () {
Expand Down Expand Up @@ -80,6 +79,40 @@
expect(route('home'))->toBe('http://localhost/central/home');
});

test('tenancy url generator inherits scheme from original url generator', function() {
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);

Route::get('/home', fn () => '')->name('home');
$tenant = Tenant::create();

// Force the original URL generator to use HTTPS
app('url')->forceScheme('https');

// Original generator uses HTTPS
expect(app('url')->formatScheme())->toBe('https://');

// Check that TenancyUrlGenerator inherits the HTTPS scheme
tenancy()->initialize($tenant);
expect(app('url')->formatScheme())->toBe('https://'); // Should inherit HTTPS
expect(route('home'))->toBe('https://localhost/home');

tenancy()->end();

// After ending tenancy, the original generator should still have the original scheme (HTTPS)
expect(route('home'))->toBe('https://localhost/home');

// Use HTTP scheme
app('url')->forceScheme('http');
expect(app('url')->formatScheme())->toBe('http://');

tenancy()->initialize($tenant);
expect(app('url')->formatScheme())->toBe('http://'); // Should inherit scheme (HTTP)
expect(route('home'))->toBe('http://localhost/home');

tenancy()->end();
expect(route('home'))->toBe('http://localhost/home');
});

test('path identification route helper behavior', function (bool $addTenantParameterToDefaults, bool $passTenantParameterToRoutes) {
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);

Expand Down
Loading