From 5c697767aa15d9e8631d07fc4b959233d186d410 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Wed, 27 Jan 2021 16:17:25 -0300 Subject: [PATCH] fix(http): empty request uri is an empty path (/) --- src/Http/Http.php | 2 +- tests/Unit/Http/HttpTest.php | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Http/Http.php b/src/Http/Http.php index 7c2b0755..1b26a43e 100644 --- a/src/Http/Http.php +++ b/src/Http/Http.php @@ -114,7 +114,7 @@ function path(): string } $query_string = strpos($request_uri, '?'); - $request_uri = $query_string ? substr($request_uri, 0, $query_string) : $request_uri; + $request_uri = $query_string === false ? $request_uri : substr($request_uri, 0, $query_string); $request_uri = rawurldecode($request_uri); $script_path = str_replace('\\', '/', \dirname($script_name)); diff --git a/tests/Unit/Http/HttpTest.php b/tests/Unit/Http/HttpTest.php index 2ec125fb..07ec04bf 100644 --- a/tests/Unit/Http/HttpTest.php +++ b/tests/Unit/Http/HttpTest.php @@ -99,6 +99,21 @@ public function testFuzzyQueryString() $this->assertSame('/bar/baz', Http\path()); } + public function testEmptyRequestUri() + { + $_SERVER['REQUEST_URI'] = ''; + $this->assertSame('/', Http\path()); + + $_SERVER['REQUEST_URI'] = '/'; + $this->assertSame('/', Http\path()); + + $_SERVER['REQUEST_URI'] = '?foo=bar'; + $this->assertSame('/', Http\path()); + + $_SERVER['REQUEST_URI'] = '/?foo=bar'; + $this->assertSame('/', Http\path()); + } + protected function setUp(): void { $_GET = $_POST = $_REQUEST = $_COOKIE = $_SESSION = ['foo' => 'bar'];