Skip to content

Commit ba8aa10

Browse files
authored
Merge pull request #3057 from Instrye/fix-redirection
Fix. redirect custom HTTP Code not work
2 parents 4358bec + 5aa5d94 commit ba8aa10

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

system/HTTP/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ public function redirect(string $uri, string $method = 'auto', int $code = null)
783783
{
784784
if ($method !== 'refresh')
785785
{
786-
$code = ($_SERVER['REQUEST_METHOD'] !== 'GET') ? 303 : 307;
786+
$code = ($_SERVER['REQUEST_METHOD'] !== 'GET') ? 303 : ($code === 302 ? 307 : $code);
787787
}
788788
}
789789

tests/system/CodeIgniterTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,56 @@ public function testRunRedirectionWithURINotSet()
360360
$this->assertEquals('http://example.com/pages/notset', $response->getHeader('Location')->getValue());
361361
}
362362

363+
public function testRunRedirectionWithHTTPCode303()
364+
{
365+
$_SERVER['argv'] = [
366+
'index.php',
367+
'example',
368+
];
369+
$_SERVER['argc'] = 2;
370+
$_SERVER['REQUEST_URI'] = '/example';
371+
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
372+
$_SERVER['REQUEST_METHOD'] = 'POST';
373+
374+
// Inject mock router.
375+
$routes = Services::routes();
376+
$routes->addRedirect('example', 'pages/notset', 301);
377+
378+
$router = Services::router($routes, Services::request());
379+
Services::injectMock('router', $router);
380+
381+
ob_start();
382+
$this->codeigniter->useSafeOutput(true)->run();
383+
ob_get_clean();
384+
$response = $this->getPrivateProperty($this->codeigniter, 'response');
385+
$this->assertEquals('303', $response->getStatusCode());
386+
}
387+
388+
public function testRunRedirectionWithHTTPCode301()
389+
{
390+
$_SERVER['argv'] = [
391+
'index.php',
392+
'example',
393+
];
394+
$_SERVER['argc'] = 2;
395+
$_SERVER['REQUEST_URI'] = '/example';
396+
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
397+
$_SERVER['REQUEST_METHOD'] = 'GET';
398+
399+
// Inject mock router.
400+
$routes = Services::routes();
401+
$routes->addRedirect('example', 'pages/notset', 301);
402+
403+
$router = Services::router($routes, Services::request());
404+
Services::injectMock('router', $router);
405+
406+
ob_start();
407+
$this->codeigniter->useSafeOutput(true)->run();
408+
ob_get_clean();
409+
$response = $this->getPrivateProperty($this->codeigniter, 'response');
410+
$this->assertEquals('301', $response->getStatusCode());
411+
}
412+
363413
/**
364414
* The method after all test, reset Servces:: config
365415
* Can't use static::tearDownAfterClass. This will cause a buffer exception

0 commit comments

Comments
 (0)