From d78d3250736c5d2f48c5cfc1690fba8b98cc222b Mon Sep 17 00:00:00 2001 From: Loz Calver Date: Fri, 26 Jun 2015 10:40:43 +0100 Subject: [PATCH] FIX: RedirectorPage_Controller shouldn't attempt redirection if the response is finished (fixes #1230) --- code/model/RedirectorPage.php | 3 ++- tests/model/RedirectorPageTest.php | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/code/model/RedirectorPage.php b/code/model/RedirectorPage.php index 55021b23..56fb76f2 100644 --- a/code/model/RedirectorPage.php +++ b/code/model/RedirectorPage.php @@ -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; } diff --git a/tests/model/RedirectorPageTest.php b/tests/model/RedirectorPageTest.php index 24afdab8..5bb7f90f 100644 --- a/tests/model/RedirectorPageTest.php +++ b/tests/model/RedirectorPageTest.php @@ -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'); + } + }