mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Adding smart header checking to return 304 response in AssetDispatcher filter
This commit is contained in:
parent
826699a670
commit
fd8971ba06
3 changed files with 55 additions and 1 deletions
|
@ -80,7 +80,10 @@ class AssetDispatcher extends DispatcherFilter {
|
|||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -61,6 +61,7 @@ class CacheDispatcher extends DispatcherFilter {
|
|||
$view = new View($controller);
|
||||
$result = $view->renderCache($filename, microtime(true));
|
||||
if ($result !== false) {
|
||||
$event->stopPropagation();
|
||||
$event->data['response']->body($result);
|
||||
return $event->data['response'];
|
||||
}
|
||||
|
|
|
@ -44,6 +44,10 @@ class AssetDispatcherTest extends CakeTestCase {
|
|||
'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'));
|
||||
|
@ -75,4 +79,50 @@ class AssetDispatcherTest extends CakeTestCase {
|
|||
$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());
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue