From c25394278b6847e1d99052b9dd58ca8a4359b125 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 9 Jul 2011 14:21:32 -0400 Subject: [PATCH] Returning response objects from controllers wasn't working correctly. --- lib/Cake/Routing/Dispatcher.php | 4 ++- lib/Cake/Test/Case/Routing/DispatcherTest.php | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Routing/Dispatcher.php b/lib/Cake/Routing/Dispatcher.php index 90a70a039..0203b9ff1 100644 --- a/lib/Cake/Routing/Dispatcher.php +++ b/lib/Cake/Routing/Dispatcher.php @@ -110,12 +110,14 @@ class Dispatcher { $controller->constructClasses(); $controller->startupProcess(); + $render = true; $result = $controller->invokeAction($request); if ($result instanceof CakeResponse) { + $render = false; $response = $result; } - if ($controller->autoRender) { + if ($render && $controller->autoRender) { $response = $controller->render(); } elseif ($response->body() === null) { $response->body($result); diff --git a/lib/Cake/Test/Case/Routing/DispatcherTest.php b/lib/Cake/Test/Case/Routing/DispatcherTest.php index 46de1d30a..1a039aa12 100644 --- a/lib/Cake/Test/Case/Routing/DispatcherTest.php +++ b/lib/Cake/Test/Case/Routing/DispatcherTest.php @@ -167,6 +167,15 @@ class SomePagesController extends AppController { return true; } +/** + * Test method for returning responses. + * + * @return CakeResponse + */ + public function responseGenerator() { + return new CakeResponse(array('body' => 'new response')); + } + /** * redirect method overriding * @@ -806,6 +815,23 @@ class DispatcherTest extends CakeTestCase { $this->assertEqual($controller->params['pass'][0], 'something. .', 'Period was chopped off. %s'); } +/** + * Test that Dispatcher handles actions that return response objects. + * + * @return void + */ + public function testDispatchActionReturnsResponse() { + $Dispatcher = new Dispatcher(); + $request = new CakeRequest('some_pages/responseGenerator'); + $response = $this->getMock('CakeResponse', array('_sendHeader')); + + ob_start(); + $Dispatcher->dispatch($request, $response); + $result = ob_get_clean(); + + $this->assertEquals('new response', $result); + } + /** * testAdminDispatch method *