Skip to content

Commit

Permalink
Merge pull request #990 from msmakouz/bugfix/route-host
Browse files Browse the repository at this point in the history
[spiral/router] Adding host check when importing routes via RoutingConfigurator
  • Loading branch information
butschster authored Sep 12, 2023
2 parents b5cb7b4 + 3871a4e commit cc53ed1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Router/src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ public function import(RoutingConfigurator $routes): void
$target = $target->withCore($configurator->core);
}

$route = new Route(\ltrim($configurator->pattern, '/'), $target, $configurator->defaults);
$pattern = \str_starts_with($configurator->pattern, '//')
? $configurator->pattern
: \ltrim($configurator->pattern, '/');
$route = new Route($pattern, $target, $configurator->defaults);

if ($configurator->middleware !== null) {
$route = $route->withMiddleware(...$configurator->middleware);
Expand Down
19 changes: 19 additions & 0 deletions src/Router/tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
use Spiral\Router\Event\Routing;
use Spiral\Router\Exception\RouteNotFoundException;
use Spiral\Router\Exception\UndefinedRouteException;
use Spiral\Router\GroupRegistry;
use Spiral\Router\Loader\Configurator\RoutingConfigurator;
use Spiral\Router\Loader\LoaderInterface;
use Spiral\Router\Route;
use Spiral\Router\RouteCollection;

class RouterTest extends BaseTestCase
{
Expand Down Expand Up @@ -75,4 +79,19 @@ public function testRouteNotFoundEventShouldBeDispatched(): void
$this->expectException(RouteNotFoundException::class);
$router->handle($request);
}

public function testImportWithHost(): void
{
$router = $this->makeRouter('https://host.com', $this->createMock(EventDispatcherInterface::class));

$configurator = new RoutingConfigurator(new RouteCollection(), $this->createMock(LoaderInterface::class));
$configurator->add('foo', '//<host>/register')->callable(fn () => null);

$router->import($configurator);
$this->container->get(GroupRegistry::class)->registerRoutes($router);

$uri = (string) $router->uri('foo', ['host' => 'some']);
$this->assertSame('some/register', $uri);
$this->assertFalse(\str_contains('https://host.com', $uri));
}
}

0 comments on commit cc53ed1

Please sign in to comment.