From 95dbae8acf4f87a53bc0a04147a4efe22ae91faa Mon Sep 17 00:00:00 2001 From: Mark Story Date: Mon, 3 May 2010 22:07:13 -0400 Subject: [PATCH] Making RequestHandler component carry along a passed status code. This fixes issues where RequestHandler hijacks an redirect(). Fixes #658 --- .../controller/components/request_handler.php | 8 ++++++- .../components/request_handler.test.php | 22 +++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/cake/libs/controller/components/request_handler.php b/cake/libs/controller/components/request_handler.php index 6d2c1e96c..d99b9498a 100644 --- a/cake/libs/controller/components/request_handler.php +++ b/cake/libs/controller/components/request_handler.php @@ -262,7 +262,7 @@ class RequestHandlerComponent extends Object { * @param mixed $url A string or array containing the redirect location * @access public */ - function beforeRedirect(&$controller, $url) { + function beforeRedirect(&$controller, $url, $status = null) { if (!$this->isAjax()) { return; } @@ -272,6 +272,12 @@ class RequestHandlerComponent extends Object { if (is_array($url)) { $url = Router::url($url + array('base' => false)); } + if (!empty($status)) { + $statusCode = $controller->httpCodes($status); + $code = key($statusCode); + $msg = $statusCode[$code]; + $controller->header("HTTP/1.1 {$code} {$msg}"); + } echo $this->requestAction($url, array('return')); $this->_stop(); } diff --git a/cake/tests/cases/libs/controller/components/request_handler.test.php b/cake/tests/cases/libs/controller/components/request_handler.test.php index 955a99d5b..ff3aa6ad7 100644 --- a/cake/tests/cases/libs/controller/components/request_handler.test.php +++ b/cake/tests/cases/libs/controller/components/request_handler.test.php @@ -21,6 +21,7 @@ App::import('Controller', 'Controller', false); App::import('Component', array('RequestHandler')); Mock::generatePartial('RequestHandlerComponent', 'NoStopRequestHandler', array('_stop')); +Mock::generatePartial('Controller', 'RequestHandlerMockController', array('header')); /** * RequestHandlerTestController class @@ -602,9 +603,7 @@ class RequestHandlerComponentTest extends CakeTestCase { */ function testBeforeRedirectCallbackWithArrayUrl() { $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; - App::build(array( - 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS) - ), true); + Router::setRequestInfo(array( array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'named' => array(), 'form' => array(), 'url' => array('url' => 'accounts/'), 'bare' => 0), array('base' => '/officespace', 'here' => '/officespace/accounts/', 'webroot' => '/officespace/') @@ -619,7 +618,22 @@ class RequestHandlerComponentTest extends CakeTestCase { ); $result = ob_get_clean(); $this->assertEqual($result, 'one: first two: second'); - App::build(); + } + +/** + * assure that beforeRedirect with a status code will correctly set the status header + * + * @return void + */ + function testBeforeRedirectCallingHeader() { + $controller =& new RequestHandlerMockController(); + $RequestHandler =& new NoStopRequestHandler(); + + $controller->expectOnce('header', array('HTTP/1.1 403 Forbidden')); + + ob_start(); + $RequestHandler->beforeRedirect($controller, 'request_handler_test/param_method/first/second', 403); + $result = ob_get_clean(); } }