Skip to content

Commit c8e7e8f

Browse files
authored
[codex] Normalize null request paths during routing (#232)
1 parent d3e4143 commit c8e7e8f

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Http/Http.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ public function match(Request $request, bool $fresh = true): ?Route
678678
}
679679

680680
$url = \parse_url($request->getURI(), PHP_URL_PATH);
681+
$url = \is_string($url) ? ($url === '' ? '/' : $url) : '/';
681682
$method = $request->getMethod();
682683
$method = (self::REQUEST_METHOD_HEAD == $method) ? self::REQUEST_METHOD_GET : $method;
683684

@@ -945,6 +946,7 @@ private function runInternal(Request $request, Response $response): static
945946
$route = self::$wildcardRoute;
946947
$this->route = $route;
947948
$path = \parse_url($request->getURI(), PHP_URL_PATH);
949+
$path = \is_string($path) ? ($path === '' ? '/' : $path) : '/';
948950
$route->path($path);
949951

950952
$this->setRequestResource('route', fn () => $route, []);

tests/HttpTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,17 @@ public function testCanMatchFreshRoute(): void
483483
}
484484
}
485485

486+
public function testCanMatchRootRouteWhenUriHasNoPath(): void
487+
{
488+
$route = Http::get('/');
489+
490+
$_SERVER['REQUEST_METHOD'] = Http::REQUEST_METHOD_GET;
491+
$_SERVER['REQUEST_URI'] = 'https://example.com?x=1';
492+
493+
$this->assertSame($route, $this->http->match(new Request()));
494+
$this->assertSame($route, $this->http->getRoute());
495+
}
496+
486497
public function testCanRunRequest(): void
487498
{
488499
// Test head requests
@@ -556,6 +567,31 @@ public function testWildcardRoute(): void
556567
$_SERVER['REQUEST_URI'] = $uri;
557568
}
558569

570+
public function testWildcardRouteWhenUriHasNoPath(): void
571+
{
572+
$method = $_SERVER['REQUEST_METHOD'] ?? null;
573+
$uri = $_SERVER['REQUEST_URI'] ?? null;
574+
575+
$_SERVER['REQUEST_METHOD'] = 'GET';
576+
$_SERVER['REQUEST_URI'] = 'https://example.com?x=1';
577+
578+
Http::wildcard()
579+
->inject('response')
580+
->action(function ($response) {
581+
$response->send('HELLO');
582+
});
583+
584+
\ob_start();
585+
@$this->http->run(new Request(), new Response());
586+
$result = \ob_get_contents();
587+
\ob_end_clean();
588+
589+
$_SERVER['REQUEST_METHOD'] = $method;
590+
$_SERVER['REQUEST_URI'] = $uri;
591+
592+
$this->assertEquals('HELLO', $result);
593+
}
594+
559595
public function testCallableStringParametersNotExecuted(): void
560596
{
561597
// Test that callable strings (like function names) are not executed

0 commit comments

Comments
 (0)