Skip to content

Commit 9753dcc

Browse files
authored
Merge pull request #2871 from samsonasik/add-more-test-codeigniter-testrunforcesecure
Fixes session active detection on force_https function and add more test CodeIgniter::forceSecureAccess() run force_https()
2 parents 3cff921 + 529a4c8 commit 9753dcc

3 files changed

Lines changed: 57 additions & 10 deletions

File tree

system/Common.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,7 @@ function esc($data, string $context = 'html', string $encoding = null)
387387
* @param RequestInterface $request
388388
* @param ResponseInterface $response
389389
*
390-
* Not testable, as it will exit!
391-
*
392-
* @throws \CodeIgniter\HTTP\Exceptions\HTTPException
393-
* @codeCoverageIgnore
390+
* @throws \CodeIgniter\HTTP\Exceptions\HTTPException
394391
*/
395392
function force_https(int $duration = 31536000, RequestInterface $request = null, ResponseInterface $response = null)
396393
{
@@ -403,17 +400,21 @@ function force_https(int $duration = 31536000, RequestInterface $request = null,
403400
$response = Services::response(null, true);
404401
}
405402

406-
if (is_cli() || $request->isSecure())
403+
if (ENVIRONMENT !== 'testing' && (is_cli() || $request->isSecure()))
407404
{
405+
// @codeCoverageIgnoreStart
408406
return;
407+
// @codeCoverageIgnoreEnd
409408
}
410-
// @codeCoverageIgnoreStart
411-
// If the session library is loaded, we should regenerate
409+
410+
// If the session status is active, we should regenerate
412411
// the session ID for safety sake.
413-
if (class_exists('Session', false))
412+
if (ENVIRONMENT !== 'testing' && session_status() === PHP_SESSION_ACTIVE)
414413
{
414+
// @codeCoverageIgnoreStart
415415
Services::session(null, true)
416416
->regenerate();
417+
// @codeCoverageIgnoreEnd
417418
}
418419

419420
$baseURL = config(App::class)->baseURL;
@@ -433,8 +434,12 @@ function force_https(int $duration = 31536000, RequestInterface $request = null,
433434
$response->redirect($uri);
434435
$response->sendHeaders();
435436

436-
exit();
437-
// @codeCoverageIgnoreEnd
437+
if (ENVIRONMENT !== 'testing')
438+
{
439+
// @codeCoverageIgnoreStart
440+
exit();
441+
// @codeCoverageIgnoreEnd
442+
}
438443
}
439444
}
440445

tests/system/CodeIgniterTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,31 @@ public function testIgnoringErrorSuppressedByAt()
273273

274274
$this->assertStringContainsString('Welcome to CodeIgniter', $output);
275275
}
276+
277+
//--------------------------------------------------------------------
278+
279+
public function testRunForceSecure()
280+
{
281+
$_SERVER['argv'] = [
282+
'index.php',
283+
'/',
284+
];
285+
$_SERVER['argc'] = 2;
286+
287+
$config = new App();
288+
$config->forceGlobalSecureRequests = true;
289+
$codeigniter = new MockCodeIgniter($config);
290+
291+
$this->getPrivateMethodInvoker($codeigniter, 'getRequestObject')();
292+
$this->getPrivateMethodInvoker($codeigniter, 'getResponseObject')();
293+
294+
$response = $this->getPrivateProperty($codeigniter, 'response');
295+
$this->assertNull($response->getHeader('Location'));
296+
297+
ob_start();
298+
$codeigniter->useSafeOutput(true)->run();
299+
$output = ob_get_clean();
300+
301+
$this->assertEquals('https://example.com', $response->getHeader('Location')->getValue());
302+
}
276303
}

tests/system/CommonFunctionsTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,4 +440,19 @@ public function testViewNotSaveData()
440440
$this->assertStringContainsString('<h1>is_not</h1>', view('\Tests\Support\View\Views\simples'));
441441
}
442442

443+
//--------------------------------------------------------------------
444+
445+
/**
446+
* @runInSeparateProcess
447+
* @preserveGlobalState disabled
448+
*/
449+
public function testForceHttpsNullRequestAndResponse()
450+
{
451+
$this->assertNull(Services::response()->getHeader('Location'));
452+
453+
force_https();
454+
455+
$this->assertEquals('https://example.com', Services::response()->getHeader('Location')->getValue());
456+
}
457+
443458
}

0 commit comments

Comments
 (0)