Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Properly return the response object instead of dieing.
  • Loading branch information
Mark Scherer committed Apr 22, 2015
1 parent 5ad0e34 commit f576e68
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
11 changes: 5 additions & 6 deletions src/Controller/Component/RequestHandlerComponent.php
Expand Up @@ -31,7 +31,7 @@
* Request object for handling alternative HTTP requests
*
* Alternative HTTP requests can come from wireless units like mobile phones, palmtop computers,
* and the like. These units have no use for Ajax requests, and this Component can tell how Cake
* and the like. These units have no use for AJAX requests, and this Component can tell how Cake
* should respond to the different needs of a handheld computer and a desktop machine.
*
* @link http://book.cakephp.org/3.0/en/controllers/components/request-handling.html
Expand Down Expand Up @@ -142,7 +142,7 @@ public function implementedEvents()
* Checks to see if a specific content type has been requested and sets RequestHandler::$ext
* accordingly. Checks the following in order: 1. The '_ext' value parsed by the Router. 2. A specific
* AJAX type request indicated by the presence of a header. 3. The Accept header. With the exception
* of an ajax request indicated using the second header based method above, the type must have
* of an AJAX request indicated using the second header based method above, the type must have
* been configured in {@link Cake\Routing\Router}.
*
* @param array $config The config data.
Expand Down Expand Up @@ -273,12 +273,12 @@ public function convertXml($xml)
}

/**
* Handles (fakes) redirects for Ajax requests using requestAction()
* Handles (fakes) redirects for AJAX requests using requestAction()
*
* @param Event $event The Controller.beforeRedirect event.
* @param string|array $url A string or array containing the redirect location
* @param \Cake\Network\Response $response The response object.
* @return void
* @return void|\Cake\Network\Response The response object if the redirect is caught.
*/
public function beforeRedirect(Event $event, $url, Response $response)
{
Expand All @@ -301,8 +301,7 @@ public function beforeRedirect(Event $event, $url, Response $response)
]
]));
$response->statusCode(200);
$response->send();
$response->stop();
return $response;
}

/**
Expand Down
35 changes: 13 additions & 22 deletions tests/TestCase/Controller/Component/RequestHandlerComponentTest.php
Expand Up @@ -810,16 +810,13 @@ public function testAjaxRedirectAsRequestAction()
$this->Controller->RequestHandler->request = $this->Controller->request;
$this->Controller->RequestHandler->response = $this->Controller->response;
$this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
$this->Controller->response->expects($this->once())->method('stop');

ob_start();
$this->Controller->RequestHandler->beforeRedirect(
$response = $this->Controller->RequestHandler->beforeRedirect(
$event,
['controller' => 'request_handler_test', 'action' => 'destination'],
['controller' => 'RequestHandlerTest', 'action' => 'destination'],
$this->Controller->response
);
$result = ob_get_clean();
$this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
$this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
}

/**
Expand All @@ -841,17 +838,14 @@ public function testAjaxRedirectAsRequestActionStatusCode()
$this->Controller->RequestHandler->request = $this->Controller->request;
$this->Controller->RequestHandler->response = $this->Controller->response;
$this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
$this->Controller->response->expects($this->once())->method('stop');

ob_start();
$this->Controller->RequestHandler->beforeRedirect(
$response = $this->Controller->RequestHandler->beforeRedirect(
$event,
['controller' => 'request_handler_test', 'action' => 'destination'],
['controller' => 'RequestHandlerTest', 'action' => 'destination'],
$this->Controller->response
);
$result = ob_get_clean();
$this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
$this->assertSame(200, $this->Controller->response->statusCode());
$this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
$this->assertSame(200, $response->statusCode());
}

/**
Expand All @@ -873,17 +867,14 @@ public function testAjaxRedirectAsRequestActionStillRenderingLayout()
$this->Controller->RequestHandler->request = $this->Controller->request;
$this->Controller->RequestHandler->response = $this->Controller->response;
$this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
$this->Controller->response->expects($this->once())->method('stop');

ob_start();
$this->Controller->RequestHandler->beforeRedirect(
$response = $this->Controller->RequestHandler->beforeRedirect(
$event,
['controller' => 'request_handler_test', 'action' => 'ajax2_layout'],
['controller' => 'RequestHandlerTest', 'action' => 'ajax2_layout'],
$this->Controller->response
);
$result = ob_get_clean();
$this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
$this->assertRegExp('/Ajax!/', $result, 'Layout was not rendered.');
$this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
$this->assertRegExp('/Ajax!/', $response->body(), 'Layout was not rendered.');
}

/**
Expand Down Expand Up @@ -912,9 +903,9 @@ public function testBeforeRedirectCallbackWithArrayUrl()
$RequestHandler->response = $this->Controller->response;

ob_start();
$RequestHandler->beforeRedirect(
$response = $RequestHandler->beforeRedirect(
$event,
['controller' => 'request_handler_test', 'action' => 'param_method', 'first', 'second'],
['controller' => 'RequestHandlerTest', 'action' => 'param_method', 'first', 'second'],
$this->Controller->response
);
$result = ob_get_clean();
Expand Down

0 comments on commit f576e68

Please sign in to comment.