Making RequestHandler component carry along a passed status code. This fixes issues where RequestHandler hijacks an redirect(). Fixes #658

This commit is contained in:
Mark Story 2010-05-03 22:07:13 -04:00
parent c48f6d6536
commit 95dbae8acf
2 changed files with 25 additions and 5 deletions

View file

@ -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();
}

View file

@ -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();
}
}