Skip to content

Commit

Permalink
Merge pull request #5034 from MGatner/download-return
Browse files Browse the repository at this point in the history
Previous Responses
  • Loading branch information
MGatner authored Sep 16, 2021
2 parents 3ab742e + 8e1b280 commit cc3af87
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
10 changes: 8 additions & 2 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\DownloadResponse;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\Request;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
Expand Down Expand Up @@ -921,14 +922,19 @@ protected function gatherOutput(?Cache $cacheConfig = null, $returned = null)
public function storePreviousURL($uri)
{
// Ignore CLI requests
if (is_cli()) {
return;
if (is_cli() && ENVIRONMENT !== 'testing') {
return; // @codeCoverageIgnore
}
// Ignore AJAX requests
if (method_exists($this->request, 'isAJAX') && $this->request->isAJAX()) {
return;
}

// Ignore unroutable responses
if ($this->response instanceof DownloadResponse || $this->response instanceof RedirectResponse) {
return;
}

// This is mainly needed during testing...
if (is_string($uri)) {
$uri = new URI($uri);
Expand Down
23 changes: 20 additions & 3 deletions tests/system/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,24 @@ public function testRunRedirectionWithHTTPCode303()
$this->assertSame(303, $response->getStatusCode());
}

public function testRunRedirectionWithHTTPCode301()
public function testStoresPreviousURL()
{
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;

// Inject mock router.
$router = Services::router(null, Services::request(), false);
Services::injectMock('router', $router);

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

$this->assertArrayHasKey('_ci_previous_url', $_SESSION);
$this->assertSame('http://example.com/index.php', $_SESSION['_ci_previous_url']);
}

public function testNotStoresPreviousURL()
{
$_SERVER['argv'] = ['index.php', 'example'];
$_SERVER['argc'] = 2;
Expand All @@ -364,8 +381,8 @@ public function testRunRedirectionWithHTTPCode301()
ob_start();
$this->codeigniter->useSafeOutput(true)->run();
ob_get_clean();
$response = $this->getPrivateProperty($this->codeigniter, 'response');
$this->assertSame(301, $response->getStatusCode());

$this->assertArrayNotHasKey('_ci_previous_url', $_SESSION);
}

/**
Expand Down

0 comments on commit cc3af87

Please sign in to comment.