Skip to content
Merged
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
25 changes: 20 additions & 5 deletions src/Mercure/Service/UrlService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

namespace Pimcore\Bundle\StudioBackendBundle\Mercure\Service;

use Pimcore\Bundle\StaticResolverBundle\Lib\ToolResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Mercure\Util\Constant\Mercure;
use Symfony\Component\HttpFoundation\RequestStack;
use LogicException;

/**
* @internal
Expand All @@ -24,7 +25,7 @@
public function __construct(
private ?string $serverSideUrl,
private ?string $clientSideUrl,
private ToolResolverInterface $toolResolver,
private RequestStack $requestStack,
) {
}

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

return str_replace(Mercure::HOST_PLACEHOLDER->value, $this->toolResolver->getHostUrl(), $this->clientSideUrl);
return str_replace(Mercure::HOST_PLACEHOLDER->value, $this->getHostUrl(), $this->clientSideUrl);
}

private function getDefaultServerUrl(): string
{
return $this->toolResolver->getHostUrl() . '/hub/.well-known/mercure';
return $this->getHostUrl() . '/hub/.well-known/mercure';
}

private function getDefaultClientUrl(): string
{
return $this->toolResolver->getHostUrl() . '/hub';
return $this->getHostUrl() . '/hub';
}

/**
* @throws LogicException
*/
private function getHostUrl(): string
{
$request = $this->requestStack->getMainRequest();

if ($request === null) {
throw new LogicException('Mercure fallback URL resolution requires an active HTTP request.');
Comment thread
lukmzig marked this conversation as resolved.
}

return $request->getSchemeAndHttpHost();
}
}
90 changes: 90 additions & 0 deletions tests/Unit/Mercure/Service/UrlServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
declare(strict_types=1);

/**
* This source file is available under the terms of the
* Pimcore Open Core License (POCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
* @license Pimcore Open Core License (POCL)
*/

namespace Pimcore\Bundle\StudioBackendBundle\Tests\Unit\Mercure\Service;

use Codeception\Test\Unit;
use LogicException;
use Pimcore\Bundle\StudioBackendBundle\Mercure\Service\UrlService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

final class UrlServiceTest extends Unit
{
public function testGetServerSideUrlWithCustomUrl(): void
{
$service = new UrlService('https://custom/mercure', null, new RequestStack());

$this->assertSame('https://custom/mercure', $service->getServerSideUrl());
}

public function testGetServerSideUrlWithDefault(): void
{
$service = new UrlService(
null,
null,
$this->createRequestStackWithRequest('https://example.com')
);

$this->assertSame('https://example.com/hub/.well-known/mercure', $service->getServerSideUrl());
}

public function testGetClientSideUrlWithCustomUrlReplacesPlaceholder(): void
{
$service = new UrlService(
null,
'<PIMCORE_SCHEMA_HOST>/custom-hub',
$this->createRequestStackWithRequest('https://example.com'),
);

$this->assertSame('https://example.com/custom-hub', $service->getClientSideUrl());
}

public function testGetClientSideUrlWithDefault(): void
{
$service = new UrlService(
null,
null,
$this->createRequestStackWithRequest('https://example.com')
);

$this->assertSame('https://example.com/hub', $service->getClientSideUrl());
}

public function testThrowsLogicExceptionWithoutRequest(): void
{
$service = new UrlService(null, null, new RequestStack());

$this->expectException(LogicException::class);
$service->getServerSideUrl();
}

public function testDefaultUrlIncludesNonStandardPort(): void
{
$service = new UrlService(
null,
null,
$this->createRequestStackWithRequest('http://localhost:8080')
);

$this->assertSame('http://localhost:8080/hub/.well-known/mercure', $service->getServerSideUrl());
}

private function createRequestStackWithRequest(string $uri): RequestStack
{
$requestStack = new RequestStack();
$requestStack->push(Request::create(uri: $uri));

return $requestStack;
}
}
Loading