Permalink
Browse files

Making RequestHandler component carry along a passed status code. This

…Fixes issues where RequestHandler hijacks an redirect().  Fixes #658
  • Loading branch information...
1 parent c48f6d6 commit 95dbae8acf4f87a53bc0a04147a4efe22ae91faa @markstory markstory committed May 4, 2010
@@ -262,7 +262,7 @@ function startup(&$controller) {
* @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 @@ function beforeRedirect(&$controller, $url) {
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();
}
@@ -21,6 +21,7 @@
App::import('Component', array('RequestHandler'));
Mock::generatePartial('RequestHandlerComponent', 'NoStopRequestHandler', array('_stop'));
+Mock::generatePartial('Controller', 'RequestHandlerMockController', array('header'));
/**
* RequestHandlerTestController class
@@ -602,9 +603,7 @@ function testAjaxRedirectAsRequestAction() {
*/
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 @@ function testBeforeRedirectCallbackWithArrayUrl() {
);
$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();
}
}

0 comments on commit 95dbae8

Please sign in to comment.