From e23c4ffad90797cef2762be9a8e4f04beca9e657 Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 17 May 2013 16:32:46 -0400 Subject: [PATCH] Fix empty response bodies when redirect URL's are empty. When redirecting XHR requests to an empty URL the response body should not be overwritten. Fixes #3835 --- .../Component/RequestHandlerComponent.php | 3 +++ .../Component/RequestHandlerComponentTest.php | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/Cake/Controller/Component/RequestHandlerComponent.php b/lib/Cake/Controller/Component/RequestHandlerComponent.php index 85a51f33891..89e50b3036f 100644 --- a/lib/Cake/Controller/Component/RequestHandlerComponent.php +++ b/lib/Cake/Controller/Component/RequestHandlerComponent.php @@ -244,6 +244,9 @@ public function beforeRedirect(Controller $controller, $url, $status = null, $ex if (!$this->request->is('ajax')) { return; } + if (empty($url)) { + return; + } $_SERVER['REQUEST_METHOD'] = 'GET'; foreach ($_POST as $key => $val) { unset($_POST[$key]); diff --git a/lib/Cake/Test/Case/Controller/Component/RequestHandlerComponentTest.php b/lib/Cake/Test/Case/Controller/Component/RequestHandlerComponentTest.php index 45c65604823..8beb0ce7564 100644 --- a/lib/Cake/Test/Case/Controller/Component/RequestHandlerComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/RequestHandlerComponentTest.php @@ -411,6 +411,23 @@ public function testNonAjaxRedirect() { $this->assertNull($this->RequestHandler->beforeRedirect($this->Controller, '/')); } +/** + * test that redirects with ajax and no url don't do anything. + * + * @return void + */ + public function testAjaxRedirectWithNoUrl() { + $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; + $this->Controller->response = $this->getMock('CakeResponse'); + + $this->Controller->response->expects($this->never()) + ->method('body'); + + $this->RequestHandler->initialize($this->Controller); + $this->RequestHandler->startup($this->Controller); + $this->assertNull($this->RequestHandler->beforeRedirect($this->Controller, null)); + } + /** * testRenderAs method *