From 57f8c69ca0d541d7b706b87c7a9de760e8acbd62 Mon Sep 17 00:00:00 2001 From: flashwave Date: Sun, 18 Aug 2024 16:41:02 +0000 Subject: [PATCH] Fixed middleware on / issue. --- VERSION | 2 +- src/Http/Routing/HttpRouter.php | 11 +++++++---- tests/RouterTest.php | 22 +++++++++++++++++++++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/VERSION b/VERSION index 29cdac6..09e8a76 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2408.181620 +0.2408.181640 diff --git a/src/Http/Routing/HttpRouter.php b/src/Http/Routing/HttpRouter.php index ff08937..f61d0bc 100644 --- a/src/Http/Routing/HttpRouter.php +++ b/src/Http/Routing/HttpRouter.php @@ -1,7 +1,7 @@ middlewares[] = $mwInfo; @@ -213,7 +216,7 @@ class HttpRouter implements IRouter { array_shift($args); } elseif(array_key_exists('prefix', $mwInfo)) { - if(!str_starts_with($path, $mwInfo['prefix'])) + if($mwInfo['prefix'] !== '' && !str_starts_with($path, $mwInfo['prefix'])) continue; $args = []; diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 7cb3c5e..10a18d4 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -1,12 +1,13 @@ assertEquals(['OPTIONS', 'GET', 'DELETE'], $resolved->getSupportedMethods()); } + + public function testMiddlewareInterceptionOnRoot(): void { + $router = new HttpRouter; + $router->use('/', fn() => 'expected'); + $router->get('/', fn() => 'unexpected'); + $router->get('/test', fn() => 'also unexpected'); + + ob_start(); + $router->dispatch(new HttpRequest('1.1', 'GET', '/', [], [], new HttpHeaders([]), null)); + $this->assertEquals('expected', ob_get_clean()); + + ob_start(); + $router->dispatch(new HttpRequest('1.1', 'GET', '/test', [], [], new HttpHeaders([]), null)); + $this->assertEquals('expected', ob_get_clean()); + + ob_start(); + $router->dispatch(new HttpRequest('1.1', 'GET', '/error', [], [], new HttpHeaders([]), null)); + $this->assertEquals('expected', ob_get_clean()); + } }