Skip to content

Commit

Permalink
Prevent route groups with middlewares to affect the request in the ne…
Browse files Browse the repository at this point in the history
…xt routes
  • Loading branch information
IngeniozIT committed Apr 13, 2024
1 parent 0c12602 commit e4dcb94
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Router.php
Expand Up @@ -79,9 +79,12 @@ private function handleRoutes(ServerRequestInterface $request): ResponseInterfac
$newRouter = new Router(
$route,
$this->container,
$this->handle(...),
);
return $newRouter->handle($request);
try {
return $newRouter->handle($request);
} catch (EmptyRouteStack) {
continue;
}
}

$matchedParams = $route->match($request);
Expand Down
22 changes: 22 additions & 0 deletions tests/MiddlewaresTest.php
Expand Up @@ -119,4 +119,26 @@ public static function providerInvalidMiddlewares(): array
],
];
}

public function testRouteGroupWithMiddlewareDoesNotAffectTheNextRoutes(): void
{
$routeGroup = new RouteGroup([
new RouteGroup(
routes: [
Route::get(path: '/no', callback: static fn(): ResponseInterface => self::response('NO')),
],
middlewares: [
fn($request, $handler) => self::response(
strrev((string)$handler->handle($request)->getBody())
)
],
),
Route::get(path: '/', callback: static fn(): ResponseInterface => self::response('TEST')),
]);
$request = self::serverRequest('GET', '/');

$response = $this->router($routeGroup)->handle($request);

self::assertEquals('TEST', (string)$response->getBody());
}
}

0 comments on commit e4dcb94

Please sign in to comment.