Skip to content

Commit e432558

Browse files
Merge pull request #173 from utopia-php/fix-null-path-1.x
Fix: issue with matching null path and relevant test
2 parents 40a6e37 + 2c68cd8 commit e432558

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/Http/Http.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,11 @@ public function start()
383383
public function match(Request $request): ?Route
384384
{
385385
$url = \parse_url($request->getURI(), PHP_URL_PATH);
386+
387+
if ($url === null || $url === false) {
388+
$url = '/'; // Default to root path for malformed URLs
389+
}
390+
386391
$method = $request->getMethod();
387392
$method = (self::REQUEST_METHOD_HEAD == $method) ? self::REQUEST_METHOD_GET : $method;
388393

tests/HttpTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,58 @@ public function testCanMatchRoute(string $method, string $path, ?string $url = n
608608
$this->assertEquals($expected, $route);
609609
}
610610

611+
public function testMatchWithNullPath(): void
612+
{
613+
// Create a route for root path
614+
$expected = Http::get('/');
615+
616+
// Test case where parse_url returns null (malformed URL)
617+
$_SERVER['REQUEST_METHOD'] = 'GET';
618+
$_SERVER['REQUEST_URI'] = '?param=1'; // This will cause parse_url to return null for PATH component
619+
620+
$matched = $this->http->match(new Request());
621+
$this->assertEquals($expected, $matched);
622+
}
623+
624+
public function testMatchWithEmptyPath(): void
625+
{
626+
// Create a route for root path
627+
$expected = Http::get('/');
628+
629+
// Test case where URI has no path component
630+
$_SERVER['REQUEST_METHOD'] = 'GET';
631+
$_SERVER['REQUEST_URI'] = 'https://example.com'; // No path component
632+
633+
$matched = $this->http->match(new Request());
634+
$this->assertEquals($expected, $matched);
635+
}
636+
637+
public function testMatchWithMalformedURL(): void
638+
{
639+
// Create a route for root path
640+
$expected = Http::get('/');
641+
642+
// Test case where parse_url returns false (severely malformed URL)
643+
$_SERVER['REQUEST_METHOD'] = 'GET';
644+
$_SERVER['REQUEST_URI'] = '#fragment'; // Malformed scheme
645+
646+
$matched = $this->http->match(new Request());
647+
$this->assertEquals($expected, $matched);
648+
}
649+
650+
public function testMatchWithOnlyQueryString(): void
651+
{
652+
// Create a route for root path
653+
$expected = Http::get('/');
654+
655+
// Test case where URI has only query string (no path)
656+
$_SERVER['REQUEST_METHOD'] = 'GET';
657+
$_SERVER['REQUEST_URI'] = '?param=value'; // Only query string, no path
658+
659+
$matched = $this->http->match(new Request());
660+
$this->assertEquals($expected, $matched);
661+
}
662+
611663
public function testNoMismatchRoute(): void
612664
{
613665
$requests = [

0 commit comments

Comments
 (0)