Skip to content

Commit b3287a7

Browse files
committed
Use site_url for RedirectResponse. Fixes #2119
1 parent 01fd252 commit b3287a7

4 files changed

Lines changed: 39 additions & 9 deletions

File tree

system/HTTP/RedirectResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function route(string $route, array $params = [], int $code = 302, string
9292
throw HTTPException::forInvalidRedirectRoute($route);
9393
}
9494

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

9898
/**

system/HTTP/Response.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,9 +537,9 @@ public function getXML()
537537
* @throws \InvalidArgumentException If the body property is not string or array.
538538
*/
539539
protected function formatBody($body, string $format)
540-
{
540+
{
541541
$this->bodyFormat = ($format === 'json-unencoded' ? 'json' : $format);
542-
$mime = "application/{$this->bodyFormat}";
542+
$mime = "application/{$this->bodyFormat}";
543543
$this->setContentType($mime);
544544

545545
// Nothing much to do for a string...

tests/system/HTTP/RedirectResponseTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CodeIgniter\HTTP;
44

55
use Config\App;
6+
use CodeIgniter\Config\Config;
67
use CodeIgniter\Config\Services;
78
use CodeIgniter\Validation\Validation;
89
use CodeIgniter\Router\RouteCollection;
@@ -57,14 +58,14 @@ public function testRedirectRoute()
5758
$response->route('exampleRoute');
5859

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

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

6465
$response->route('home');
6566

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

7071
public function testRedirectRouteBad()
@@ -186,4 +187,27 @@ public function testRedirectBackMissing()
186187
$this->assertSame($response, $returned);
187188
}
188189

190+
/**
191+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/2119
192+
*/
193+
public function testRedirectRouteBaseUrl()
194+
{
195+
$config = new App();
196+
$config->baseURL = 'http://example.com/test/';
197+
Config::injectMock('App', $config);
198+
199+
$request = new MockIncomingRequest($config, new URI('http://example.com/test/'), null, new UserAgent());
200+
Services::injectMock('request', $request);
201+
202+
$response = new RedirectResponse(new App());
203+
204+
$this->routes->add('exampleRoute', 'Home::index');
205+
206+
$response->route('exampleRoute');
207+
208+
$this->assertTrue($response->hasHeader('Location'));
209+
$this->assertEquals('http://example.com/test/index.php/exampleRoute', $response->getHeaderLine('Location'));
210+
211+
Config::reset();
212+
}
189213
}

tests/system/HTTP/ResponseTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Config\Format;
77
use DateTime;
88
use DateTimeZone;
9+
use CodeIgniter\Config\Config;
910
use Tests\Support\HTTP\MockResponse;
1011

1112
class ResponseTest extends \CIUnitTestCase
@@ -158,28 +159,33 @@ public function testSetDateRemembersDateInUTC()
158159

159160
public function testSetLink()
160161
{
161-
$response = new Response(new App());
162+
// Ensure our URL is not getting overridden
163+
$config = new App();
164+
$config->baseURL = 'http://example.com/test';
165+
Config::injectMock('App', $config);
166+
167+
$response = new Response($config);
162168
$pager = \Config\Services::pager();
163169

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

167173
$this->assertEquals(
168-
'<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()
174+
'<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()
169175
);
170176

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

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

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

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

0 commit comments

Comments
 (0)