Skip to content

Commit

Permalink
Use site_url for RedirectResponse. Fixes #2119
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnieezell committed Jan 17, 2020
1 parent 01fd252 commit b3287a7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
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();
}
}
14 changes: 10 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 Down Expand Up @@ -158,28 +159,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

0 comments on commit b3287a7

Please sign in to comment.