From 2c68cd8b27673adc4fe991a900197a843eca0b83 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 6 Jul 2025 00:52:30 +0000 Subject: [PATCH] Fix: issue with matching null path and relevant test --- src/Http/Http.php | 5 +++++ tests/HttpTest.php | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/Http/Http.php b/src/Http/Http.php index d9fb7a3..2b2262d 100755 --- a/src/Http/Http.php +++ b/src/Http/Http.php @@ -383,6 +383,11 @@ public function start() public function match(Request $request): ?Route { $url = \parse_url($request->getURI(), PHP_URL_PATH); + + if ($url === null || $url === false) { + $url = '/'; // Default to root path for malformed URLs + } + $method = $request->getMethod(); $method = (self::REQUEST_METHOD_HEAD == $method) ? self::REQUEST_METHOD_GET : $method; diff --git a/tests/HttpTest.php b/tests/HttpTest.php index 3aac0bd..9b7d040 100755 --- a/tests/HttpTest.php +++ b/tests/HttpTest.php @@ -608,6 +608,58 @@ public function testCanMatchRoute(string $method, string $path, ?string $url = n $this->assertEquals($expected, $route); } + public function testMatchWithNullPath(): void + { + // Create a route for root path + $expected = Http::get('/'); + + // Test case where parse_url returns null (malformed URL) + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_SERVER['REQUEST_URI'] = '?param=1'; // This will cause parse_url to return null for PATH component + + $matched = $this->http->match(new Request()); + $this->assertEquals($expected, $matched); + } + + public function testMatchWithEmptyPath(): void + { + // Create a route for root path + $expected = Http::get('/'); + + // Test case where URI has no path component + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_SERVER['REQUEST_URI'] = 'https://example.com'; // No path component + + $matched = $this->http->match(new Request()); + $this->assertEquals($expected, $matched); + } + + public function testMatchWithMalformedURL(): void + { + // Create a route for root path + $expected = Http::get('/'); + + // Test case where parse_url returns false (severely malformed URL) + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_SERVER['REQUEST_URI'] = '#fragment'; // Malformed scheme + + $matched = $this->http->match(new Request()); + $this->assertEquals($expected, $matched); + } + + public function testMatchWithOnlyQueryString(): void + { + // Create a route for root path + $expected = Http::get('/'); + + // Test case where URI has only query string (no path) + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_SERVER['REQUEST_URI'] = '?param=value'; // Only query string, no path + + $matched = $this->http->match(new Request()); + $this->assertEquals($expected, $matched); + } + public function testNoMismatchRoute(): void { $requests = [