Merge pull request #1231 from kinglozzer/pulls/redirectorpage-finished-response

FIX: RedirectorPage_Controller shouldn't attempt redirection if the response is finished (fixes #1230)
This commit is contained in:
Daniel Hensby 2015-06-26 16:46:52 +01:00
commit 284a0f0e13
2 changed files with 25 additions and 1 deletions

View File

@ -161,7 +161,8 @@ class RedirectorPage_Controller extends Page_Controller {
public function init() {
parent::init();
if($link = $this->redirectionLink()) {
// Check we don't already have a redirect code set
if(!$this->getResponse()->isFinished() && $link = $this->redirectionLink()) {
$this->redirect($link, 301);
return;
}

View File

@ -3,6 +3,7 @@
class RedirectorPageTest extends FunctionalTest {
protected static $fixture_file = 'RedirectorPageTest.yml';
protected static $use_draft_site = true;
protected $autoFollowRedirection = false;
public function testGoodRedirectors() {
/* For good redirectors, the final destination URL will be returned */
@ -50,4 +51,26 @@ class RedirectorPageTest extends FunctionalTest {
$page->write();
$this->assertEquals($page->ExternalURL, 'http://google.com', 'onBeforeWrite will not double prefix if written again!');
}
/**
* Test that we can trigger a redirection before RedirectorPage_Controller::init() is called
*/
public function testRedirectRespectsFinishedResponse() {
$page = $this->objFromFixture('RedirectorPage', 'goodinternal');
RedirectorPage_Controller::add_extension('RedirectorPageTest_RedirectExtension');
$response = $this->get($page->regularLink());
$this->assertEquals(302, $response->getStatusCode());
$this->assertEquals('/foo', $response->getHeader('Location'));
RedirectorPage_Controller::remove_extension('RedirectorPageTest_RedirectExtension');
}
}
class RedirectorPageTest_RedirectExtension extends Extension implements TestOnly {
public function onBeforeInit() {
$this->owner->redirect('/foo');
}
}