Browse Source

Merge pull request #1139 from ADmad/2.3-dispatcher

Prevent response object being set as response body
Mark Story 13 years ago
parent
commit
51d2cb60fa
2 changed files with 47 additions and 1 deletions
  1. 3 1
      lib/Cake/Routing/Dispatcher.php
  2. 44 0
      lib/Cake/Test/Case/Routing/DispatcherTest.php

+ 3 - 1
lib/Cake/Routing/Dispatcher.php

@@ -192,7 +192,9 @@ class Dispatcher implements CakeEventListener {
 
 		if ($render && $controller->autoRender) {
 			$response = $controller->render();
-		} elseif ($response->body() === null) {
+		} elseif (!($result instanceof CakeResponse) &&
+			$response->body() === null
+		) {
 			$response->body($result);
 		}
 		$controller->shutdownProcess();

+ 44 - 0
lib/Cake/Test/Case/Routing/DispatcherTest.php

@@ -208,6 +208,16 @@ class SomePagesController extends AppController {
 		return new CakeResponse(array('body' => 'new response'));
 	}
 
+/**
+ * Test file sending
+ *
+ * @return CakeResponse
+ */
+	public function sendfile() {
+		$this->response->file(CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'css' . DS . 'test_asset.css');
+		return $this->response;
+	}
+
 }
 
 /**
@@ -867,6 +877,40 @@ class DispatcherTest extends CakeTestCase {
 	}
 
 /**
+ * testDispatchActionSendsFile
+ *
+ * @return void
+ */
+	public function testDispatchActionSendsFile() {
+		Router::connect('/:controller/:action');
+		$Dispatcher = new Dispatcher();
+		$request = new CakeRequest('some_pages/sendfile');
+		$response = $this->getMock('CakeResponse', array(
+			'header',
+			'type',
+			'download',
+			'_sendHeader',
+			'_setContentType',
+			'_isActive',
+			'_clearBuffer',
+			'_flushBuffer'
+		));
+
+		$response->expects($this->never())
+			->method('body');
+
+		$response->expects($this->exactly(1))
+			->method('_isActive')
+			->will($this->returnValue(true));
+
+		ob_start();
+		$Dispatcher->dispatch($request, $response);
+		$result = ob_get_clean();
+
+		$this->assertEquals("/* this is the test asset css file */\n", $result);
+	}
+
+/**
  * testAdminDispatch method
  *
  * @return void