Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove $_SERVER['HTTP_HOST'] in RouteCollection #5962

Merged
merged 6 commits into from
May 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions system/Router/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,20 @@ class RouteCollection implements RouteCollectionInterface
*/
protected $prioritizeDetected = false;

/**
* The current hostname from $_SERVER['HTTP_HOST']
*/
private ?string $httpHost = null;

/**
* Constructor
*/
public function __construct(FileLocator $locator, Modules $moduleConfig)
{
$this->fileLocator = $locator;
$this->moduleConfig = $moduleConfig;

$this->httpHost = Services::request()->getServer('HTTP_HOST');
}

/**
Expand Down Expand Up @@ -1176,7 +1183,7 @@ protected function create(string $verb, string $from, $to, ?array $options = nul
// Hostname limiting?
if (! empty($options['hostname'])) {
// @todo determine if there's a way to whitelist hosts?
if (isset($_SERVER['HTTP_HOST']) && strtolower($_SERVER['HTTP_HOST']) !== strtolower($options['hostname'])) {
if (isset($this->httpHost) && strtolower($this->httpHost) !== strtolower($options['hostname'])) {
return;
}

Expand Down Expand Up @@ -1302,7 +1309,7 @@ private function getMethodParams(string $from): string
private function checkSubdomains($subdomains): bool
{
// CLI calls can't be on subdomain.
if (! isset($_SERVER['HTTP_HOST'])) {
if (! isset($this->httpHost)) {
return false;
}

Expand Down Expand Up @@ -1337,7 +1344,7 @@ private function determineCurrentSubdomain()
// We have to ensure that a scheme exists
// on the URL else parse_url will mis-interpret
// 'host' as the 'path'.
$url = $_SERVER['HTTP_HOST'];
$url = $this->httpHost;
if (strpos($url, 'http') !== 0) {
$url = 'http://' . $url;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/system/Commands/GenerateKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

/**
* @internal
*
* @group SeparateProcess
*/
final class GenerateKeyTest extends CIUnitTestCase
{
Expand Down
2 changes: 2 additions & 0 deletions tests/system/CommonFunctionsSendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

/**
* @internal
*
* @group SeparateProcess
*/
final class CommonFunctionsSendTest extends CIUnitTestCase
{
Expand Down
2 changes: 2 additions & 0 deletions tests/system/CommonFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
* @backupGlobals enabled
*
* @internal
*
* @group SeparateProcess
*/
final class CommonFunctionsTest extends CIUnitTestCase
{
Expand Down
2 changes: 2 additions & 0 deletions tests/system/Config/BaseConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

/**
* @internal
*
* @group SeparateProcess
*/
final class BaseConfigTest extends CIUnitTestCase
{
Expand Down
2 changes: 2 additions & 0 deletions tests/system/Config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

/**
* @internal
*
* @group SeparateProcess
*/
final class ConfigTest extends CIUnitTestCase
{
Expand Down
2 changes: 2 additions & 0 deletions tests/system/Config/DotEnvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @backupGlobals enabled
*
* @internal
*
* @group SeparateProcess
*/
final class DotEnvTest extends CIUnitTestCase
{
Expand Down
2 changes: 2 additions & 0 deletions tests/system/Config/ServicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@

/**
* @internal
*
* @group SeparateProcess
*/
final class ServicesTest extends CIUnitTestCase
{
Expand Down
2 changes: 2 additions & 0 deletions tests/system/Events/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

/**
* @internal
*
* @group SeparateProcess
*/
final class EventsTest extends CIUnitTestCase
{
Expand Down
2 changes: 2 additions & 0 deletions tests/system/Filters/HoneypotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* @backupGlobals enabled
*
* @internal
*
* @group SeparateProcess
*/
final class HoneypotTest extends CIUnitTestCase
{
Expand Down
2 changes: 2 additions & 0 deletions tests/system/HTTP/ContentSecurityPolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* See https://cspvalidator.org/
*
* @internal
*
* @group SeparateProcess
*/
final class ContentSecurityPolicyTest extends CIUnitTestCase
{
Expand Down
2 changes: 2 additions & 0 deletions tests/system/HTTP/DownloadResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

/**
* @internal
*
* @group SeparateProcess
*/
final class DownloadResponseTest extends CIUnitTestCase
{
Expand Down
2 changes: 2 additions & 0 deletions tests/system/HTTP/IncomingRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* @backupGlobals enabled
*
* @internal
*
* @group SeparateProcess
*/
final class IncomingRequestTest extends CIUnitTestCase
{
Expand Down
2 changes: 2 additions & 0 deletions tests/system/HTTP/RedirectResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

/**
* @internal
*
* @group SeparateProcess
*/
final class RedirectResponseTest extends CIUnitTestCase
{
Expand Down
2 changes: 2 additions & 0 deletions tests/system/HTTP/ResponseSendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
* test cases need to be run as separate processes.
*
* @internal
*
* @group SeparateProcess
*/
final class ResponseSendTest extends CIUnitTestCase
{
Expand Down
2 changes: 2 additions & 0 deletions tests/system/Helpers/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

/**
* @internal
*
* @group SeparateProcess
*/
final class FormHelperTest extends CIUnitTestCase
{
Expand Down
48 changes: 39 additions & 9 deletions tests/system/RESTful/ResourceControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
* @preserveGlobalState disabled
*
* @internal
*
* @group SeparateProcess
*/
final class ResourceControllerTest extends CIUnitTestCase
{
Expand All @@ -53,8 +55,12 @@ protected function setUp(): void
{
parent::setUp();

$this->resetServices();
$this->resetServices(true);
$this->resetFactories();
}

private function createCodeigniter(): void
{
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';

// Inject mock router.
Expand Down Expand Up @@ -86,11 +92,14 @@ public function testResourceGet()
$_SERVER['REQUEST_URI'] = '/work';
$_SERVER['REQUEST_METHOD'] = 'GET';

$this->createCodeigniter();

ob_start();
$this->codeigniter->useSafeOutput(true)->run($this->routes);
$output = ob_get_clean();

$this->assertStringContainsString(lang('RESTful.notImplemented', ['index']), $output);
$error = json_decode($output)->messages->error;
$this->assertStringContainsString(lang('RESTful.notImplemented', ['index']), $error);
}

public function testResourceGetNew()
Expand All @@ -105,11 +114,14 @@ public function testResourceGetNew()
$_SERVER['REQUEST_URI'] = '/work/new';
$_SERVER['REQUEST_METHOD'] = 'GET';

$this->createCodeigniter();

ob_start();
$this->codeigniter->useSafeOutput(true)->run($this->routes);
$output = ob_get_clean();

$this->assertStringContainsString(lang('RESTful.notImplemented', ['new']), $output);
$error = json_decode($output)->messages->error;
$this->assertStringContainsString(lang('RESTful.notImplemented', ['new']), $error);
}

public function testResourceGetEdit()
Expand All @@ -125,11 +137,14 @@ public function testResourceGetEdit()
$_SERVER['REQUEST_URI'] = '/work/1/edit';
$_SERVER['REQUEST_METHOD'] = 'GET';

$this->createCodeigniter();

ob_start();
$this->codeigniter->useSafeOutput(true)->run($this->routes);
$output = ob_get_clean();

$this->assertStringContainsString(lang('RESTful.notImplemented', ['edit']), $output);
$error = json_decode($output)->messages->error;
$this->assertStringContainsString(lang('RESTful.notImplemented', ['edit']), $error);
}

public function testResourceGetOne()
Expand All @@ -144,11 +159,14 @@ public function testResourceGetOne()
$_SERVER['REQUEST_URI'] = '/work/1';
$_SERVER['REQUEST_METHOD'] = 'GET';

$this->createCodeigniter();

ob_start();
$this->codeigniter->useSafeOutput(true)->run($this->routes);
$output = ob_get_clean();

$this->assertStringContainsString(lang('RESTful.notImplemented', ['show']), $output);
$error = json_decode($output)->messages->error;
$this->assertStringContainsString(lang('RESTful.notImplemented', ['show']), $error);
}

public function testResourcePost()
Expand All @@ -162,11 +180,14 @@ public function testResourcePost()
$_SERVER['REQUEST_URI'] = '/work';
$_SERVER['REQUEST_METHOD'] = 'POST';

$this->createCodeigniter();

ob_start();
$this->codeigniter->useSafeOutput(true)->run($this->routes);
$output = ob_get_clean();

$this->assertStringContainsString(lang('RESTful.notImplemented', ['create']), $output);
$error = json_decode($output)->messages->error;
$this->assertStringContainsString(lang('RESTful.notImplemented', ['create']), $error);
}

public function testResourcePatch()
Expand All @@ -181,11 +202,14 @@ public function testResourcePatch()
$_SERVER['REQUEST_URI'] = '/work/123';
$_SERVER['REQUEST_METHOD'] = 'PATCH';

$this->createCodeigniter();

ob_start();
$this->codeigniter->useSafeOutput(true)->run($this->routes);
$output = ob_get_clean();

$this->assertStringContainsString(lang('RESTful.notImplemented', ['patch']), $output);
$error = json_decode($output)->messages->error;
$this->assertStringContainsString(lang('RESTful.notImplemented', ['update']), $error);
}

public function testResourcePut()
Expand All @@ -200,11 +224,14 @@ public function testResourcePut()
$_SERVER['REQUEST_URI'] = '/work/123';
$_SERVER['REQUEST_METHOD'] = 'PUT';

$this->createCodeigniter();

ob_start();
$this->codeigniter->useSafeOutput(true)->run($this->routes);
$output = ob_get_clean();

$this->assertStringContainsString(lang('RESTful.notImplemented', ['put']), $output);
$error = json_decode($output)->messages->error;
$this->assertStringContainsString(lang('RESTful.notImplemented', ['update']), $error);
}

public function testResourceDelete()
Expand All @@ -219,11 +246,14 @@ public function testResourceDelete()
$_SERVER['REQUEST_URI'] = '/work/123';
$_SERVER['REQUEST_METHOD'] = 'DELETE';

$this->createCodeigniter();

ob_start();
$this->codeigniter->useSafeOutput(true)->run($this->routes);
$output = ob_get_clean();

$this->assertStringContainsString(lang('RESTful.notImplemented', ['delete']), $output);
$error = json_decode($output)->messages->error;
$this->assertStringContainsString(lang('RESTful.notImplemented', ['delete']), $error);
}

public function testModel()
Expand Down
Loading