Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test RedirectResponse problem report #1486

Merged
merged 4 commits into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ function redirect(string $uri = null)

if (! empty($uri))
{
return $response->to($uri);
return $response->route($uri);
}

return $response;
Expand Down
2 changes: 0 additions & 2 deletions system/HTTP/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,8 +756,6 @@ public function redirect(string $uri, string $method = 'auto', int $code = null)

$this->setStatusCode($code);

$this->sendHeaders();

return $this;
}

Expand Down
54 changes: 54 additions & 0 deletions tests/system/CommonFunctionsSendTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

use CodeIgniter\HTTP\Response;
use Config\App;

class CommonFunctionsSendTest extends \CIUnitTestCase
{

public function setUp()
{
parent::setUp();

unset($_ENV['foo'], $_SERVER['foo']);
}

//--------------------------------------------------------------------
/**
// Make sure cookies are set by RedirectResponse this way
// See https://github.com/codeigniter4/CodeIgniter4/issues/1393
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testRedirectResponseCookiesSent()
{
$login_time = time();

$routes = service('routes');
$routes->add('user/login', 'Auth::verify', ['as' => 'login']);

$response = redirect()->route('login')
->setCookie('foo', 'onething', YEAR)
->setCookie('login_time', $login_time, YEAR);
$response->pretend(false);
$this->assertTrue($response->hasCookie('foo', 'onething'));
$this->assertTrue($response->hasCookie('login_time'));
$response->setBody('Hello');

// send it
ob_start();
$response->send();

$buffer = ob_clean();
if (ob_get_level() > 0)
{
ob_end_clean();
}

// and what actually got sent?
$this->assertHeaderEmitted('Set-Cookie: foo=onething;');
$this->assertHeaderEmitted('Set-Cookie: login_time');
}

}
25 changes: 23 additions & 2 deletions tests/system/CommonFunctionsTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use CodeIgniter\Session\Handlers\FileHandler;
use CodeIgniter\HTTP\Response;
use Config\App;
use Config\Autoload;
use CodeIgniter\Config\Services;
Expand All @@ -17,7 +18,7 @@
/**
* @backupGlobals enabled
*/
class CommomFunctionsTest extends \CIUnitTestCase
class CommonFunctionsTest extends \CIUnitTestCase
{

//--------------------------------------------------------------------
Expand Down Expand Up @@ -321,11 +322,31 @@ protected function injectSessionMock()
'cookieSecure' => false,
];

$config = (object)$defaults;
$config = (object) $defaults;

$session = new MockSession(new FileHandler($config, '127.0.0.1'), $config);
$session->setLogger(new TestLogger(new Logger()));
\CodeIgniter\Config\BaseService::injectMock('session', $session);
}

//--------------------------------------------------------------------
// Make sure cookies are set by RedirectResponse this way
// See https://github.com/codeigniter4/CodeIgniter4/issues/1393
public function testRedirectResponseCookies1()
{
$login_time = time();

$response = new Response(new App());

$routes = service('routes');
$routes->add('user/login', 'Auth::verify', ['as' => 'login']);

$answer1 = redirect()->route('login')
->setCookie('foo', 'onething', YEAR)
->setCookie('login_time', $login_time, YEAR);

$this->assertTrue($answer1->hasCookie('foo', 'onething'));
$this->assertTrue($answer1->hasCookie('login_time'));
}

}
40 changes: 39 additions & 1 deletion tests/system/HTTP/ResponseSendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class ResponseSendTest extends \CIUnitTestCase
* The tests includes a basic sanity check, to make sure that
* the body we thought would be sent actually was.
*/

//--------------------------------------------------------------------
/**
* @runInSeparateProcess
Expand Down Expand Up @@ -97,4 +96,43 @@ public function testHeadersWithCSP()
$this->assertHeaderEmitted('Content-Security-Policy:');
}

//--------------------------------------------------------------------
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
// Make sure cookies are set by RedirectResponse this way
// See https://github.com/codeigniter4/CodeIgniter4/issues/1393
public function testRedirectResponseCookies()
{
$login_time = time();

$response = new Response(new App());
$response->pretend(false);

$routes = service('routes');
$routes->add('user/login', 'Auth::verify', ['as' => 'login']);

$answer1 = $response->redirect('/login')
->setCookie('foo', 'bar', YEAR)
->setCookie('login_time', $login_time, YEAR);
$this->assertTrue($answer1->hasCookie('foo', 'bar'));
$this->assertTrue($answer1->hasCookie('login_time'));
$response->setBody('Hello');

// send it
ob_start();
$response->send();

$buffer = ob_clean();
if (ob_get_level() > 0)
{
ob_end_clean();
}

// and what actually got sent?
$this->assertHeaderEmitted('Set-Cookie: foo=bar;');
$this->assertHeaderEmitted('Set-Cookie: login_time');
}

}
16 changes: 16 additions & 0 deletions tests/system/HTTP/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,4 +481,20 @@ public function testTemporaryRedirectGet11()
$this->assertEquals(307, $response->getStatusCode());
}

//--------------------------------------------------------------------
// Make sure cookies are set by RedirectResponse this way
// See https://github.com/codeigniter4/CodeIgniter4/issues/1393
public function testRedirectResponseCookies()
{
$login_time = time();

$response = new Response(new App());
$answer1 = $response->redirect('/login')
->setCookie('foo', 'bar', YEAR)
->setCookie('login_time', $login_time, YEAR);

$this->assertTrue($answer1->hasCookie('foo'));
$this->assertTrue($answer1->hasCookie('login_time'));
}

}