Skip to content

Commit

Permalink
Merge pull request #2496 from codeigniter4/redirectresponseurl
Browse files Browse the repository at this point in the history
Use site_url for RedirectResponse. Fixes #2119
  • Loading branch information
lonnieezell authored Jan 22, 2020
2 parents fd56585 + 4b8fb33 commit b51d8a5
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 47 deletions.
6 changes: 3 additions & 3 deletions app/Config/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ class Database extends \CodeIgniter\Database\Config
'hostname' => '127.0.0.1',
'username' => '',
'password' => '',
'database' => '',
'DBDriver' => '',
'DBPrefix' => 'db_', // Needed to ensure we're working correctly with prefixes live. DO NOT REMOVE.
'database' => ':memory:',
'DBDriver' => 'SQLite3',
'DBPrefix' => 'db_', // Needed to ensure we're working correctly with prefixes live. DO NOT REMOVE FOR CI DEVS
'pConnect' => false,
'DBDebug' => (ENVIRONMENT !== 'production'),
'cacheOn' => false,
Expand Down
2 changes: 1 addition & 1 deletion system/HTTP/RedirectResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function route(string $route, array $params = [], int $code = 302, string
throw HTTPException::forInvalidRedirectRoute($route);
}

return $this->redirect(base_url($route), $method, $code);
return $this->redirect(site_url($route), $method, $code);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions system/HTTP/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,9 @@ public function getXML()
* @throws \InvalidArgumentException If the body property is not string or array.
*/
protected function formatBody($body, string $format)
{
{
$this->bodyFormat = ($format === 'json-unencoded' ? 'json' : $format);
$mime = "application/{$this->bodyFormat}";
$mime = "application/{$this->bodyFormat}";
$this->setContentType($mime);

// Nothing much to do for a string...
Expand Down
28 changes: 26 additions & 2 deletions tests/system/HTTP/RedirectResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CodeIgniter\HTTP;

use Config\App;
use CodeIgniter\Config\Config;
use CodeIgniter\Config\Services;
use CodeIgniter\Validation\Validation;
use CodeIgniter\Router\RouteCollection;
Expand Down Expand Up @@ -57,14 +58,14 @@ public function testRedirectRoute()
$response->route('exampleRoute');

$this->assertTrue($response->hasHeader('Location'));
$this->assertEquals('http://example.com/exampleRoute', $response->getHeaderLine('Location'));
$this->assertEquals('http://example.com/index.php/exampleRoute', $response->getHeaderLine('Location'));

$this->routes->add('exampleRoute', 'Home::index', ['as' => 'home']);

$response->route('home');

$this->assertTrue($response->hasHeader('Location'));
$this->assertEquals('http://example.com/exampleRoute', $response->getHeaderLine('Location'));
$this->assertEquals('http://example.com/index.php/exampleRoute', $response->getHeaderLine('Location'));
}

public function testRedirectRouteBad()
Expand Down Expand Up @@ -186,4 +187,27 @@ public function testRedirectBackMissing()
$this->assertSame($response, $returned);
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/2119
*/
public function testRedirectRouteBaseUrl()
{
$config = new App();
$config->baseURL = 'http://example.com/test/';
Config::injectMock('App', $config);

$request = new MockIncomingRequest($config, new URI('http://example.com/test/'), null, new UserAgent());
Services::injectMock('request', $request);

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

$this->routes->add('exampleRoute', 'Home::index');

$response->route('exampleRoute');

$this->assertTrue($response->hasHeader('Location'));
$this->assertEquals('http://example.com/test/index.php/exampleRoute', $response->getHeaderLine('Location'));

Config::reset();
}
}
15 changes: 11 additions & 4 deletions tests/system/HTTP/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Config\Format;
use DateTime;
use DateTimeZone;
use CodeIgniter\Config\Config;
use Tests\Support\HTTP\MockResponse;

class ResponseTest extends \CIUnitTestCase
Expand All @@ -20,6 +21,7 @@ protected function setUp(): void
public function tearDown(): void
{
$_SERVER = $this->server;
Config::reset();
}

public function testCanSetStatusCode()
Expand Down Expand Up @@ -158,28 +160,33 @@ public function testSetDateRemembersDateInUTC()

public function testSetLink()
{
$response = new Response(new App());
// Ensure our URL is not getting overridden
$config = new App();
$config->baseURL = 'http://example.com/test';
Config::injectMock('App', $config);

$response = new Response($config);
$pager = \Config\Services::pager();

$pager->store('default', 3, 10, 200);
$response->setLink($pager);

$this->assertEquals(
'<http://example.com?page=1>; rel="first",<http://example.com?page=2>; rel="prev",<http://example.com?page=4>; rel="next",<http://example.com?page=20>; rel="last"', $response->getHeader('Link')->getValue()
'<http://example.com/test/?page=1>; rel="first",<http://example.com/test/?page=2>; rel="prev",<http://example.com/test/?page=4>; rel="next",<http://example.com/test/?page=20>; rel="last"', $response->getHeader('Link')->getValue()
);

$pager->store('default', 1, 10, 200);
$response->setLink($pager);

$this->assertEquals(
'<http://example.com?page=2>; rel="next",<http://example.com?page=20>; rel="last"', $response->getHeader('Link')->getValue()
'<http://example.com/test/?page=2>; rel="next",<http://example.com/test/?page=20>; rel="last"', $response->getHeader('Link')->getValue()
);

$pager->store('default', 20, 10, 200);
$response->setLink($pager);

$this->assertEquals(
'<http://example.com?page=1>; rel="first",<http://example.com?page=19>; rel="prev"', $response->getHeader('Link')->getValue()
'<http://example.com/test/?page=1>; rel="first",<http://example.com/test/?page=19>; rel="prev"', $response->getHeader('Link')->getValue()
);
}

Expand Down
35 changes: 0 additions & 35 deletions tests/system/Helpers/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,41 +173,6 @@ public function testFormOpenWithHidden()
$this->assertEquals($expected, form_open('foo/bar', $attributes, $hidden));
}

// ------------------------------------------------------------------------
//FIXME This needs dynamic filters to complete
// public function testFormOpenWithCSRF()
// {
// $config = new App();
// $config->baseURL = '';
// $config->indexPage = 'index.php';
// $request = Services::request($config);
// $request->uri = new URI('http://example.com/');
//
// Services::injectMock('request', $request);
//
// $filters = Services::filters();
// $filters->globals['before'][] = 'csrf'; // force CSRF
// $before = $filters->globals['before'];
//
// $Value = csrf_hash();
// $Name = csrf_token();
// $expected = <<<EOH
//<form action="http://example.com/index.php/foo/bar" name="form" id="form" method="POST" accept-charset="utf-8">
//<input type="hidden" name="foo" value="bar" style="display: none;" />
//<input type="hidden" name="$Name" value="$Value" style="display: none;" />
//
//EOH;
//
// $attributes = [
// 'name' => 'form',
// 'id' => 'form',
// 'method' => 'POST'
// ];
// $hidden = [
// 'foo' => 'bar'
// ];
// $this->assertEquals($expected, form_open('foo/bar', $attributes, $hidden));
// }
// ------------------------------------------------------------------------
public function testFormOpenMultipart()
{
Expand Down
7 changes: 7 additions & 0 deletions tests/system/Helpers/URLHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ protected function setUp(): void
Services::reset();
}

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

$_SERVER = [];
}

//--------------------------------------------------------------------
// Test site_url

Expand Down

0 comments on commit b51d8a5

Please sign in to comment.