diff --git a/app/Models/ApplicationPreview.php b/app/Models/ApplicationPreview.php index f67497bf34..4a0f6d78f0 100644 --- a/app/Models/ApplicationPreview.php +++ b/app/Models/ApplicationPreview.php @@ -337,6 +337,10 @@ class ApplicationPreview extends BaseModel $previewFqdn = str_replace('{{random}}', $random, $template); $previewFqdn = str_replace('{{domain}}', $host, $previewFqdn); $previewFqdn = str_replace('{{pr_id}}', (string) $this->pull_request_id, $previewFqdn); + if (preg_match('#^(https?)://(.*)$#i', $previewFqdn, $matches)) { + $schema = strtolower($matches[1]); + $previewFqdn = $matches[2]; + } $previewUrl = "{$schema}://{$previewFqdn}{$path}"; $sourceCanonical = DomainPortOverrides::withoutPort($sourceDomain); $port = $url->getPort() ?? ($this->application->domain_port_overrides[$sourceCanonical] ?? null); diff --git a/tests/Feature/PreviewDomainPortOverridesTest.php b/tests/Feature/PreviewDomainPortOverridesTest.php index db78ef95ca..e81738f6ac 100644 --- a/tests/Feature/PreviewDomainPortOverridesTest.php +++ b/tests/Feature/PreviewDomainPortOverridesTest.php @@ -394,6 +394,18 @@ it('copies the parent domain port override onto a generated preview domain', fun ->toBe([8080]); }); +it('uses the preview url template scheme instead of the parent scheme', function (string $template, string $expected) { + $this->application->update(['preview_url_template' => $template]); + + $preview = createPreviewForPortTests($this->application, 2163); + + expect($preview->generatedPreviewDomain('http://app.example.com/api')['url'])->toBe($expected); +})->with([ + 'explicit https' => ['https://{{pr_id}}.preview.example.com', 'https://2163.preview.example.com/api'], + 'explicit uppercase https' => ['HTTPS://{{pr_id}}.preview.example.com', 'https://2163.preview.example.com/api'], + 'scheme-less inherits parent' => ['{{pr_id}}.preview.example.com', 'http://2163.preview.example.com/api'], +]); + it('keeps every generated preview domain port override in the legacy compose parser', function () { $this->application->update([ 'build_pack' => 'dockercompose',