Skip to content

Commit

Permalink
Fix base URL logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner committed May 5, 2021
1 parent 03e0bec commit 4f3ba28
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
20 changes: 14 additions & 6 deletions system/HTTP/URL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
38 changes: 26 additions & 12 deletions tests/system/HTTP/URLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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)
{
Expand All @@ -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()
Expand Down Expand Up @@ -270,52 +276,60 @@ 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',
],
[
[
'baseURL' => 'http://bananas.com/subfolder/',
'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',
],
];
}
Expand Down

0 comments on commit 4f3ba28

Please sign in to comment.