Browse Source

Merge pull request #3308 from cakephp/asset-dispatcher-fix

Don't 404 extensions that could be handled by routing.
ADmad 12 years ago
parent
commit
3f88709513

+ 2 - 8
lib/Cake/Routing/Filter/AssetDispatcher.php

@@ -51,19 +51,12 @@ class AssetDispatcher extends DispatcherFilter {
 		}
 
 		$assetFile = $this->_getAssetFile($url);
-		if ($assetFile === null) {
+		if ($assetFile === null || !file_exists($assetFile)) {
 			return null;
 		}
-
 		$response = $event->data['response'];
 		$event->stopPropagation();
 
-		if (!file_exists($assetFile)) {
-			$response->statusCode(404);
-			$response->send();
-			return $response;
-		}
-
 		$response->modified(filemtime($assetFile));
 		if ($response->checkNotModified($event->data['request'])) {
 			return $response;
@@ -71,6 +64,7 @@ class AssetDispatcher extends DispatcherFilter {
 
 		$pathSegments = explode('.', $url);
 		$ext = array_pop($pathSegments);
+
 		$this->_deliverAsset($response, $assetFile, $ext);
 		return $response;
 	}

+ 27 - 17
lib/Cake/Test/Case/Routing/Filter/AssetDispatcherTest.php

@@ -84,6 +84,33 @@ class AssetDispatcherTest extends CakeTestCase {
 	}
 
 /**
+ * AssetDispatcher should not 404 extensions that could be handled
+ * by Routing.
+ *
+ * @return void
+ */
+	public function testNoHandleRoutedExtension() {
+		$filter = new AssetDispatcher();
+		$response = $this->getMock('CakeResponse', array('_sendHeader'));
+		Configure::write('Asset.filter', array(
+			'js' => '',
+			'css' => ''
+		));
+		App::build(array(
+			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
+			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
+		), App::RESET);
+		Router::parseExtensions('json');
+		Router::connect('/test_plugin/api/v1/:action', array('controller' => 'api'));
+		CakePlugin::load('TestPlugin');
+
+		$request = new CakeRequest('test_plugin/api/v1/forwarding.json');
+		$event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
+		$this->assertNull($filter->beforeDispatch($event));
+		$this->assertFalse($event->isStopped(), 'Events for routed extensions should not be stopped');
+	}
+
+/**
  * Tests that $response->checkNotModified() is called and bypasses
  * file dispatching
  *
@@ -130,23 +157,6 @@ class AssetDispatcherTest extends CakeTestCase {
 	}
 
 /**
- * Test 404 status code is set on missing asset.
- *
- * @return void
- */
-	public function test404OnMissingFile() {
-		$filter = new AssetDispatcher();
-
-		$response = $this->getMock('CakeResponse', array('_sendHeader'));
-		$request = new CakeRequest('/theme/test_theme/img/nope.gif');
-		$event = new CakeEvent('Dispatcher.beforeRequest', $this, compact('request', 'response'));
-
-		$response = $filter->beforeDispatch($event);
-		$this->assertTrue($event->isStopped());
-		$this->assertEquals(404, $response->statusCode());
-	}
-
-/**
  * Test that no exceptions are thrown for //index.php type URLs.
  *
  * @return void