diff --git a/src/Http/Http.php b/src/Http/Http.php index b42cbdf..53a76fd 100755 --- a/src/Http/Http.php +++ b/src/Http/Http.php @@ -678,6 +678,7 @@ public function match(Request $request, bool $fresh = true): ?Route } $url = \parse_url($request->getURI(), PHP_URL_PATH); + $url = \is_string($url) ? ($url === '' ? '/' : $url) : '/'; $method = $request->getMethod(); $method = (self::REQUEST_METHOD_HEAD == $method) ? self::REQUEST_METHOD_GET : $method; @@ -945,6 +946,7 @@ private function runInternal(Request $request, Response $response): static $route = self::$wildcardRoute; $this->route = $route; $path = \parse_url($request->getURI(), PHP_URL_PATH); + $path = \is_string($path) ? ($path === '' ? '/' : $path) : '/'; $route->path($path); $this->setRequestResource('route', fn () => $route, []); diff --git a/tests/HttpTest.php b/tests/HttpTest.php index a6ce883..060ec33 100755 --- a/tests/HttpTest.php +++ b/tests/HttpTest.php @@ -483,6 +483,17 @@ public function testCanMatchFreshRoute(): void } } + public function testCanMatchRootRouteWhenUriHasNoPath(): void + { + $route = Http::get('/'); + + $_SERVER['REQUEST_METHOD'] = Http::REQUEST_METHOD_GET; + $_SERVER['REQUEST_URI'] = 'https://example.com?x=1'; + + $this->assertSame($route, $this->http->match(new Request())); + $this->assertSame($route, $this->http->getRoute()); + } + public function testCanRunRequest(): void { // Test head requests @@ -556,6 +567,31 @@ public function testWildcardRoute(): void $_SERVER['REQUEST_URI'] = $uri; } + public function testWildcardRouteWhenUriHasNoPath(): void + { + $method = $_SERVER['REQUEST_METHOD'] ?? null; + $uri = $_SERVER['REQUEST_URI'] ?? null; + + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_SERVER['REQUEST_URI'] = 'https://example.com?x=1'; + + Http::wildcard() + ->inject('response') + ->action(function ($response) { + $response->send('HELLO'); + }); + + \ob_start(); + @$this->http->run(new Request(), new Response()); + $result = \ob_get_contents(); + \ob_end_clean(); + + $_SERVER['REQUEST_METHOD'] = $method; + $_SERVER['REQUEST_URI'] = $uri; + + $this->assertEquals('HELLO', $result); + } + public function testCallableStringParametersNotExecuted(): void { // Test that callable strings (like function names) are not executed