From 4f3ba28dfce7ddd6c464f037f22b4e119389ffeb Mon Sep 17 00:00:00 2001 From: MGatner Date: Wed, 5 May 2021 02:14:09 +0000 Subject: [PATCH] Fix base URL logic --- system/HTTP/URL.php | 20 ++++++++++++------ tests/system/HTTP/URLTest.php | 38 ++++++++++++++++++++++++----------- 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/system/HTTP/URL.php b/system/HTTP/URL.php index ba8cbf30b155..e97d92508c6b 100644 --- a/system/HTTP/URL.php +++ b/system/HTTP/URL.php @@ -39,26 +39,34 @@ class URL //-------------------------------------------------------------------- /** - * Returns an instance representing the current URL. + * Returns an instance representing the base URL. + * + * @param string $uri Additional URI string to include * * @return static */ - final public static function base() + final public static function base(string $uri = '') { - return new static(''); + // Base URLs never include the index page + $config = clone config('App'); + $config->indexPage = ''; + + return new static($uri, $config); } /** - * Returns an instance representing the current URL. + * Returns an instance representing a routed URL. * - * @param string $uri + * @param string $uri Named route, reverse route, or URI string * * @return static */ final public static function to(string $uri) { + $uri = rtrim($uri, '/ '); + // Check for a named or reverse-route - if ($route = Services::routes()->reverseRoute($uri)) + if ($uri !== '' && $route = Services::routes()->reverseRoute($uri)) { return new static($route); } diff --git a/tests/system/HTTP/URLTest.php b/tests/system/HTTP/URLTest.php index dcce022d7810..43afef947302 100644 --- a/tests/system/HTTP/URLTest.php +++ b/tests/system/HTTP/URLTest.php @@ -151,17 +151,16 @@ public function testConstructorPath(string $path, string $expected) /** * @dataProvider configProvider */ - public function testConstructorConfig(array $configs, string $baseURL) + public function testConstructorConfig(array $configs, string $baseURL, string $siteURL) { foreach ($configs as $key => $value) { $this->setConfig($key, $value); } - $url = new URL('testaroo', $this->config); - $expected = rtrim($baseURL, '/') . '/testaroo'; + $url = new URL('testaroo', $this->config); - $this->assertSame($expected, (string) $url); + $this->assertSame($siteURL, (string) $url); } /** @@ -180,7 +179,7 @@ public function testCurrent(string $uri, string $expected = null) /** * @dataProvider configProvider */ - public function testBase(array $configs, string $expected) + public function testBase(array $configs, string $baseURL, string $siteURL) { foreach ($configs as $key => $value) { @@ -191,7 +190,14 @@ public function testBase(array $configs, string $expected) $url = URL::base(); - $this->assertSame($expected, (string) $url); + $this->assertSame($baseURL, (string) $url); + } + + public function testBaseWithUri() + { + $url = URL::base('images/cat.gif'); + + $this->assertSame('http://example.com/images/cat.gif', (string) $url); } public function testTo() @@ -270,25 +276,29 @@ public function configProvider(): array [ 'baseURL' => 'http://bananas.com', ], - 'http://bananas.com/index.php', + 'http://bananas.com/', + 'http://bananas.com/index.php/testaroo', ], [ [ 'baseURL' => 'http://bananas.com/', ], - 'http://bananas.com/index.php', + 'http://bananas.com/', + 'http://bananas.com/index.php/testaroo', ], [ [ 'baseURL' => 'http://bananas.com/subfolder/', ], - 'http://bananas.com/subfolder/index.php', + 'http://bananas.com/subfolder/', + 'http://bananas.com/subfolder/index.php/testaroo', ], [ [ 'indexPage' => '', ], 'http://example.com/', + 'http://example.com/testaroo', ], [ [ @@ -296,26 +306,30 @@ public function configProvider(): array 'indexPage' => '', ], 'http://bananas.com/subfolder/', + 'http://bananas.com/subfolder/testaroo', ], [ [ 'forceGlobalSecureRequests' => true, ], - 'https://example.com/index.php', + 'https://example.com/', + 'https://example.com/index.php/testaroo', ], [ [ 'baseURL' => 'http://bananas.com/', 'forceGlobalSecureRequests' => true, ], - 'https://bananas.com/index.php', + 'https://bananas.com/', + 'https://bananas.com/index.php/testaroo', ], [ [ 'baseURL' => 'https://bananas.com/subfolder/', 'forceGlobalSecureRequests' => true, ], - 'https://bananas.com/subfolder/index.php', + 'https://bananas.com/subfolder/', + 'https://bananas.com/subfolder/index.php/testaroo', ], ]; }