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

More RouteCollection tests for overwriting. Closes #1692 #1913

Merged
merged 2 commits into from
Apr 3, 2019
Merged
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
105 changes: 105 additions & 0 deletions tests/system/Router/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1208,4 +1208,109 @@ public function testRouteToWithoutSubdomainNot()
$this->assertEquals('/i/sth', $routes->reverseRoute('doc_item', 'sth'));
}

//--------------------------------------------------------------------
// Tests for router overwritting issue
// @see https://github.com/codeigniter4/CodeIgniter4/issues/1692

public function testRouteOverwritingDifferentSubdomains()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['HTTP_HOST'] = 'doc.domain.com';

$routes = $this->getCollector();
$router = new Router($routes);

$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');

$routes->get('/', 'App\Controllers\Site\CDoc::index', ['subdomain' => 'doc', 'as' => 'doc_index']);
$routes->get('/', 'Home::index', ['subdomain' => 'dev']);

$expects = '\App\Controllers\Site\CDoc';

$this->assertEquals($expects, $router->handle('/'));
}

public function testRouteOverwritingTwoRules()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['HTTP_HOST'] = 'doc.domain.com';

$routes = $this->getCollector();
$router = new Router($routes);

$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');

$routes->get('/', 'App\Controllers\Site\CDoc::index', ['subdomain' => 'doc', 'as' => 'doc_index']);
$routes->get('/', 'Home::index');

// the second rule applies, so overwrites the first
$expects = '\App\Controllers\Home';

$this->assertEquals($expects, $router->handle('/'));
}

public function testRouteOverwritingTwoRulesLastApplies()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['HTTP_HOST'] = 'doc.domain.com';

$routes = $this->getCollector();
$router = new Router($routes);

$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');

$routes->get('/', 'Home::index');
$routes->get('/', 'App\Controllers\Site\CDoc::index', ['subdomain' => 'doc', 'as' => 'doc_index']);

$expects = '\App\Controllers\Site\CDoc';

$this->assertEquals($expects, $router->handle('/'));
}

public function testRouteOverwritingMatchingSubdomain()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['HTTP_HOST'] = 'doc.domain.com';

$routes = $this->getCollector();
$router = new Router($routes);

$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');

$routes->get('/', 'Home::index', ['as' => 'ddd']);
$routes->get('/', 'App\Controllers\Site\CDoc::index', ['subdomain' => 'doc', 'as' => 'doc_index']);

$expects = '\App\Controllers\Site\CDoc';

$this->assertEquals($expects, $router->handle('/'));
}

public function testRouteOverwritingMatchingHost()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['HTTP_HOST'] = 'doc.domain.com';

$routes = $this->getCollector();
$router = new Router($routes);

$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');

$routes->get('/', 'Home::index', ['as' => 'ddd']);
$routes->get('/', 'App\Controllers\Site\CDoc::index', ['hostname' => 'doc.domain.com', 'as' => 'doc_index']);

$expects = '\App\Controllers\Site\CDoc';

$this->assertEquals($expects, $router->handle('/'));
}

}