Skip to content

Commit

Permalink
Adding smart header checking to return 304 response in AssetDispatche…
Browse files Browse the repository at this point in the history
…r filter
  • Loading branch information
lorenzo committed Apr 19, 2012
1 parent 826699a commit fd8971b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/Cake/Routing/Filter/AssetDispatcher.php
Expand Up @@ -80,7 +80,10 @@ public function beforeDispatch($event) {

if ($assetFile !== null) {
$event->stopPropagation();
$this->_deliverAsset($response, $assetFile, $ext);
$response->modified(filemtime($assetFile));
if (!$response->checkNotModified($event->data['request'])) {
$this->_deliverAsset($response, $assetFile, $ext);
}
return $response;
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/Cake/Routing/Filter/CacheDispatcher.php
Expand Up @@ -61,6 +61,7 @@ public function beforeDispatch($event) {
$view = new View($controller);
$result = $view->renderCache($filename, microtime(true));
if ($result !== false) {
$event->stopPropagation();
$event->data['response']->body($result);
return $event->data['response'];
}
Expand Down
50 changes: 50 additions & 0 deletions lib/Cake/Test/Case/Routing/Filter/AssetDispatcherTest.php
Expand Up @@ -44,6 +44,10 @@ public function testAssetFilterForThemeAndPlugins() {
'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);

$request = new CakeRequest('theme/test_theme/ccss/cake.generic.css');
$event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
Expand Down Expand Up @@ -75,4 +79,50 @@ public function testAssetFilterForThemeAndPlugins() {
$this->assertNull($filter->beforeDispatch($event));
$this->assertFalse($event->isStopped());
}

/**
* Tests that $response->checkNotModified() is called and bypasses
* file dispatching
*
* @return void
**/
public function testNotModified() {
$filter = new AssetDispatcher();
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)
));
$time = filemtime(App::themePath('TestTheme') . 'webroot' . DS . 'img' . DS . 'cake.power.gif');
$time = new DateTime(date('Y-m-d H:i:s', $time), new DateTimeZone('UTC'));

$response = $this->getMock('CakeResponse', array('send', 'checkNotModified'));
$request = new CakeRequest('theme/test_theme/img/cake.power.gif');

$response->expects($this->once())->method('checkNotModified')
->with($request)
->will($this->returnValue(true));
$event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));

ob_start();
$this->assertSame($response, $filter->beforeDispatch($event));
ob_end_clean();
$this->assertEquals(200, $response->statusCode());
$this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified());

$response = $this->getMock('CakeResponse', array('_sendHeader', 'checkNotModified'));
$request = new CakeRequest('theme/test_theme/img/cake.power.gif');

$response->expects($this->once())->method('checkNotModified')
->with($request)
->will($this->returnValue(true));
$response->expects($this->never())->method('send');
$event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));

$this->assertSame($response, $filter->beforeDispatch($event));
$this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified());
}
}

0 comments on commit fd8971b

Please sign in to comment.