From 9889db626941959d992e97f693ed6550dbe54647 Mon Sep 17 00:00:00 2001 From: Jim Parry Date: Wed, 3 Apr 2019 11:51:20 -0700 Subject: [PATCH] More RouteCollection tests for overwriting. Closes #1692 --- tests/system/Router/RouteCollectionTest.php | 105 ++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index 1ea43d7ee12b..4ae6211aa5f9 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -1094,4 +1094,109 @@ public function testOffsetParameters() $this->assertEquals($expected, $routes->getRoutes()); } + //-------------------------------------------------------------------- + // 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('/')); + } + }