Skip to content

Commit cc826cc

Browse files
authored
[Task] Add mercure host resolution fallback (#1770)
* Change Mercure Fallback URL resolution * Add Unit Tests.
1 parent 8d67be8 commit cc826cc

2 files changed

Lines changed: 110 additions & 5 deletions

File tree

src/Mercure/Service/UrlService.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313

1414
namespace Pimcore\Bundle\StudioBackendBundle\Mercure\Service;
1515

16-
use Pimcore\Bundle\StaticResolverBundle\Lib\ToolResolverInterface;
1716
use Pimcore\Bundle\StudioBackendBundle\Mercure\Util\Constant\Mercure;
17+
use Symfony\Component\HttpFoundation\RequestStack;
18+
use LogicException;
1819

1920
/**
2021
* @internal
@@ -24,7 +25,7 @@
2425
public function __construct(
2526
private ?string $serverSideUrl,
2627
private ?string $clientSideUrl,
27-
private ToolResolverInterface $toolResolver,
28+
private RequestStack $requestStack,
2829
) {
2930
}
3031

@@ -43,16 +44,30 @@ public function getClientSideUrl(): string
4344
return $this->getDefaultClientUrl();
4445
}
4546

46-
return str_replace(Mercure::HOST_PLACEHOLDER->value, $this->toolResolver->getHostUrl(), $this->clientSideUrl);
47+
return str_replace(Mercure::HOST_PLACEHOLDER->value, $this->getHostUrl(), $this->clientSideUrl);
4748
}
4849

4950
private function getDefaultServerUrl(): string
5051
{
51-
return $this->toolResolver->getHostUrl() . '/hub/.well-known/mercure';
52+
return $this->getHostUrl() . '/hub/.well-known/mercure';
5253
}
5354

5455
private function getDefaultClientUrl(): string
5556
{
56-
return $this->toolResolver->getHostUrl() . '/hub';
57+
return $this->getHostUrl() . '/hub';
58+
}
59+
60+
/**
61+
* @throws LogicException
62+
*/
63+
private function getHostUrl(): string
64+
{
65+
$request = $this->requestStack->getMainRequest();
66+
67+
if ($request === null) {
68+
throw new LogicException('Mercure fallback URL resolution requires an active HTTP request.');
69+
}
70+
71+
return $request->getSchemeAndHttpHost();
5772
}
5873
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* This source file is available under the terms of the
6+
* Pimcore Open Core License (POCL)
7+
* Full copyright and license information is available in
8+
* LICENSE.md which is distributed with this source code.
9+
*
10+
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
11+
* @license Pimcore Open Core License (POCL)
12+
*/
13+
14+
namespace Pimcore\Bundle\StudioBackendBundle\Tests\Unit\Mercure\Service;
15+
16+
use Codeception\Test\Unit;
17+
use LogicException;
18+
use Pimcore\Bundle\StudioBackendBundle\Mercure\Service\UrlService;
19+
use Symfony\Component\HttpFoundation\Request;
20+
use Symfony\Component\HttpFoundation\RequestStack;
21+
22+
final class UrlServiceTest extends Unit
23+
{
24+
public function testGetServerSideUrlWithCustomUrl(): void
25+
{
26+
$service = new UrlService('https://custom/mercure', null, new RequestStack());
27+
28+
$this->assertSame('https://custom/mercure', $service->getServerSideUrl());
29+
}
30+
31+
public function testGetServerSideUrlWithDefault(): void
32+
{
33+
$service = new UrlService(
34+
null,
35+
null,
36+
$this->createRequestStackWithRequest('https://example.com')
37+
);
38+
39+
$this->assertSame('https://example.com/hub/.well-known/mercure', $service->getServerSideUrl());
40+
}
41+
42+
public function testGetClientSideUrlWithCustomUrlReplacesPlaceholder(): void
43+
{
44+
$service = new UrlService(
45+
null,
46+
'<PIMCORE_SCHEMA_HOST>/custom-hub',
47+
$this->createRequestStackWithRequest('https://example.com'),
48+
);
49+
50+
$this->assertSame('https://example.com/custom-hub', $service->getClientSideUrl());
51+
}
52+
53+
public function testGetClientSideUrlWithDefault(): void
54+
{
55+
$service = new UrlService(
56+
null,
57+
null,
58+
$this->createRequestStackWithRequest('https://example.com')
59+
);
60+
61+
$this->assertSame('https://example.com/hub', $service->getClientSideUrl());
62+
}
63+
64+
public function testThrowsLogicExceptionWithoutRequest(): void
65+
{
66+
$service = new UrlService(null, null, new RequestStack());
67+
68+
$this->expectException(LogicException::class);
69+
$service->getServerSideUrl();
70+
}
71+
72+
public function testDefaultUrlIncludesNonStandardPort(): void
73+
{
74+
$service = new UrlService(
75+
null,
76+
null,
77+
$this->createRequestStackWithRequest('http://localhost:8080')
78+
);
79+
80+
$this->assertSame('http://localhost:8080/hub/.well-known/mercure', $service->getServerSideUrl());
81+
}
82+
83+
private function createRequestStackWithRequest(string $uri): RequestStack
84+
{
85+
$requestStack = new RequestStack();
86+
$requestStack->push(Request::create(uri: $uri));
87+
88+
return $requestStack;
89+
}
90+
}

0 commit comments

Comments
 (0)