Skip to content

Commit

Permalink
base_url should pay attention to https requests. Fixes #867
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnieezell committed Dec 15, 2017
1 parent b445d51 commit 8af9f35
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
7 changes: 7 additions & 0 deletions system/Helpers/url_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ function base_url($path = '', string $scheme = null): string
$url = $url->resolveRelativeURI($path);
}

// If the scheme wasn't provided, check to
// see if it was a secure request
if (empty($scheme) && \CodeIgniter\Config\Services::request()->isSecure())
{
$scheme = 'https';
}

if ( ! empty($scheme))
{
$url->setScheme($scheme);
Expand Down
29 changes: 29 additions & 0 deletions tests/system/Helpers/URLHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ public function testSiteURLBasics()
$this->assertEquals('http://example.com/index.php/', site_url('', null, $config));
}

public function testSiteURLHTTPS()
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/';
$_SERVER['HTTPS'] = 'on';

$config = new App();
$config->baseURL = '';
$config->indexPage = 'index.php';
$request = Services::request($config);
$request->uri = new URI('http://example.com/');

Services::injectMock('request', $request);

$this->assertEquals('https://example.com/index.php/', site_url('', null, $config));
}

public function testSiteURLNoIndex()
{
$_SERVER['HTTP_HOST'] = 'example.com';
Expand Down Expand Up @@ -273,6 +290,18 @@ public function testBaseURLWithSegments()

//--------------------------------------------------------------------

/**
* @see https://github.com/bcit-ci/CodeIgniter4/issues/867
*/
public function testBaseURLHTTPS()
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/';
$_SERVER['HTTPS'] = 'on';

$this->assertEquals('https://example.com/blog/post/123', base_url('blog/post/123'));
}

/**
* @see https://github.com/bcit-ci/CodeIgniter4/issues/240
*/
Expand Down

0 comments on commit 8af9f35

Please sign in to comment.