Fixed middleware on / issue.
This commit is contained in:
parent
f690df4200
commit
57f8c69ca0
3 changed files with 29 additions and 6 deletions
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
||||||
0.2408.181620
|
0.2408.181640
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
// HttpRouter.php
|
// HttpRouter.php
|
||||||
// Created: 2024-03-28
|
// Created: 2024-03-28
|
||||||
// Updated: 2024-08-04
|
// Updated: 2024-08-18
|
||||||
|
|
||||||
namespace Index\Http\Routing;
|
namespace Index\Http\Routing;
|
||||||
|
|
||||||
|
@ -151,9 +151,12 @@ class HttpRouter implements IRouter {
|
||||||
$mwInfo['handler'] = $handler;
|
$mwInfo['handler'] = $handler;
|
||||||
|
|
||||||
$prepared = self::preparePath($path, true);
|
$prepared = self::preparePath($path, true);
|
||||||
if($prepared === false)
|
if($prepared === false) {
|
||||||
|
if(str_ends_with($path, '/'))
|
||||||
|
$path = substr($path, 0, -1);
|
||||||
|
|
||||||
$mwInfo['prefix'] = $path;
|
$mwInfo['prefix'] = $path;
|
||||||
else
|
} else
|
||||||
$mwInfo['match'] = $prepared;
|
$mwInfo['match'] = $prepared;
|
||||||
|
|
||||||
$this->middlewares[] = $mwInfo;
|
$this->middlewares[] = $mwInfo;
|
||||||
|
@ -213,7 +216,7 @@ class HttpRouter implements IRouter {
|
||||||
|
|
||||||
array_shift($args);
|
array_shift($args);
|
||||||
} elseif(array_key_exists('prefix', $mwInfo)) {
|
} elseif(array_key_exists('prefix', $mwInfo)) {
|
||||||
if(!str_starts_with($path, $mwInfo['prefix']))
|
if($mwInfo['prefix'] !== '' && !str_starts_with($path, $mwInfo['prefix']))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
$args = [];
|
$args = [];
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
<?php
|
<?php
|
||||||
// RouterTest.php
|
// RouterTest.php
|
||||||
// Created: 2022-01-20
|
// Created: 2022-01-20
|
||||||
// Updated: 2024-07-31
|
// Updated: 2024-08-18
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use PHPUnit\Framework\Attributes\CoversClass;
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||||||
|
use Index\Http\{HttpHeaders,HttpRequest};
|
||||||
use Index\Http\Routing\{HttpGet,HttpMiddleware,HttpPost,HttpPut,HttpRouter,RouteHandler};
|
use Index\Http\Routing\{HttpGet,HttpMiddleware,HttpPost,HttpPut,HttpRouter,RouteHandler};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -162,4 +163,23 @@ final class RouterTest extends TestCase {
|
||||||
|
|
||||||
$this->assertEquals(['OPTIONS', 'GET', 'DELETE'], $resolved->getSupportedMethods());
|
$this->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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue