Skip to content

Commit 57af142

Browse files
committed
Test and exception handler for backwards compatibility.
1 parent 6cffb87 commit 57af142

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

system/CodeIgniter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use CodeIgniter\HTTP\Request;
2525
use CodeIgniter\HTTP\ResponseInterface;
2626
use CodeIgniter\HTTP\URI;
27+
use CodeIgniter\Router\Exceptions\RedirectException as DeprecatedRedirectException;
2728
use CodeIgniter\Router\RouteCollectionInterface;
2829
use CodeIgniter\Router\Router;
2930
use Config\App;
@@ -343,7 +344,7 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
343344

344345
try {
345346
$this->response = $this->handleRequest($routes, config(Cache::class), $returnResponse);
346-
} catch (RedirectException $e) {
347+
} catch (RedirectException|DeprecatedRedirectException $e) {
347348
$this->outputBufferingEnd();
348349
$logger = Services::logger();
349350
$logger->info('REDIRECTED ROUTE at ' . $e->getMessage());

tests/system/CodeIgniterTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use CodeIgniter\Config\Services;
1515
use CodeIgniter\Exceptions\ConfigException;
1616
use CodeIgniter\HTTP\Response;
17+
use CodeIgniter\Router\Exceptions\RedirectException;
1718
use CodeIgniter\Router\RouteCollection;
1819
use CodeIgniter\Test\CIUnitTestCase;
1920
use CodeIgniter\Test\Filters\CITestStreamFilter;
@@ -570,6 +571,29 @@ public function testRunRedirectionWithPOSTAndHTTPCode301()
570571
$this->assertSame(301, $response->getStatusCode());
571572
}
572573

574+
/**
575+
* test for deprecated \CodeIgniter\Router\Exceptions\RedirectException for backward compatibility
576+
*/
577+
public function testRedirectExceptionDeprecated(): void
578+
{
579+
$_SERVER['argv'] = ['index.php', '/'];
580+
$_SERVER['argc'] = 2;
581+
582+
// Inject mock router.
583+
$routes = Services::routes();
584+
$routes->get('/', static function () {
585+
throw new RedirectException('redirect-exception', 503);
586+
});
587+
588+
$router = Services::router($routes, Services::incomingrequest());
589+
Services::injectMock('router', $router);
590+
591+
$response = $this->codeigniter->run($routes, true);
592+
593+
$this->assertSame(503, $response->getStatusCode());
594+
$this->assertSame('http://example.com/redirect-exception', $response->getHeaderLine('Location'));
595+
}
596+
573597
public function testStoresPreviousURL()
574598
{
575599
$_SERVER['argv'] = ['index.php', '/'];

0 commit comments

Comments
 (0)