| 16591 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
|
|
4 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
5 |
*
|
|
|
6 |
* Licensed under The MIT License
|
|
|
7 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
8 |
* Redistributions of files must retain the above copyright notice.
|
|
|
9 |
*
|
|
|
10 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
11 |
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
|
|
12 |
* @package Cake.Test.Case.Routing.Filter
|
|
|
13 |
* @since CakePHP(tm) v 2.2
|
|
|
14 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
App::uses('AssetDispatcher', 'Routing/Filter');
|
|
|
18 |
App::uses('CakeEvent', 'Event');
|
|
|
19 |
App::uses('CakeResponse', 'Network');
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Class AssetDispatcherTest
|
|
|
23 |
*
|
|
|
24 |
* @package Cake.Test.Case.Routing.Filter
|
|
|
25 |
*/
|
|
|
26 |
class AssetDispatcherTest extends CakeTestCase {
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* tearDown method
|
|
|
30 |
*
|
|
|
31 |
* @return void
|
|
|
32 |
*/
|
|
|
33 |
public function tearDown() {
|
|
|
34 |
parent::tearDown();
|
|
|
35 |
Configure::write('Dispatcher.filters', array());
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* test that asset filters work for theme and plugin assets
|
|
|
40 |
*
|
|
|
41 |
* @return void
|
|
|
42 |
* @triggers DispatcherTest $this, compact('request', 'response')
|
|
|
43 |
* @triggers DispatcherTest $this, compact('request', 'response')
|
|
|
44 |
* @triggers DispatcherTest $this, compact('request', 'response')
|
|
|
45 |
* @triggers DispatcherTest $this, compact('request', 'response')
|
|
|
46 |
* @triggers DispatcherTest $this, compact('request', 'response')
|
|
|
47 |
* @triggers DispatcherTest $this, compact('request', 'response')
|
|
|
48 |
*/
|
|
|
49 |
public function testAssetFilterForThemeAndPlugins() {
|
|
|
50 |
$filter = new AssetDispatcher();
|
|
|
51 |
$response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
52 |
Configure::write('Asset.filter', array(
|
|
|
53 |
'js' => '',
|
|
|
54 |
'css' => ''
|
|
|
55 |
));
|
|
|
56 |
App::build(array(
|
|
|
57 |
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
|
|
|
58 |
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
|
|
|
59 |
), App::RESET);
|
|
|
60 |
|
|
|
61 |
$request = new CakeRequest('theme/test_theme/ccss/cake.generic.css');
|
|
|
62 |
$event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
|
|
|
63 |
$this->assertSame($response, $filter->beforeDispatch($event));
|
|
|
64 |
$this->assertTrue($event->isStopped());
|
|
|
65 |
|
|
|
66 |
$request = new CakeRequest('theme/test_theme/cjs/debug_kit.js');
|
|
|
67 |
$event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
|
|
|
68 |
$this->assertSame($response, $filter->beforeDispatch($event));
|
|
|
69 |
$this->assertTrue($event->isStopped());
|
|
|
70 |
|
|
|
71 |
$request = new CakeRequest('test_plugin/ccss/cake.generic.css');
|
|
|
72 |
$event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
|
|
|
73 |
$this->assertSame($response, $filter->beforeDispatch($event));
|
|
|
74 |
$this->assertTrue($event->isStopped());
|
|
|
75 |
|
|
|
76 |
$request = new CakeRequest('test_plugin/cjs/debug_kit.js');
|
|
|
77 |
$event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
|
|
|
78 |
$this->assertSame($response, $filter->beforeDispatch($event));
|
|
|
79 |
$this->assertTrue($event->isStopped());
|
|
|
80 |
|
|
|
81 |
$request = new CakeRequest('css/ccss/debug_kit.css');
|
|
|
82 |
$event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
|
|
|
83 |
$this->assertNull($filter->beforeDispatch($event));
|
|
|
84 |
$this->assertFalse($event->isStopped());
|
|
|
85 |
|
|
|
86 |
$request = new CakeRequest('js/cjs/debug_kit.js');
|
|
|
87 |
$event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
|
|
|
88 |
$this->assertNull($filter->beforeDispatch($event));
|
|
|
89 |
$this->assertFalse($event->isStopped());
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* AssetDispatcher should not 404 extensions that could be handled
|
|
|
94 |
* by Routing.
|
|
|
95 |
*
|
|
|
96 |
* @return void
|
|
|
97 |
* @triggers DispatcherTest $this, compact('request', 'response')
|
|
|
98 |
*/
|
|
|
99 |
public function testNoHandleRoutedExtension() {
|
|
|
100 |
$filter = new AssetDispatcher();
|
|
|
101 |
$response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
102 |
Configure::write('Asset.filter', array(
|
|
|
103 |
'js' => '',
|
|
|
104 |
'css' => ''
|
|
|
105 |
));
|
|
|
106 |
App::build(array(
|
|
|
107 |
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
|
|
|
108 |
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
|
|
|
109 |
), App::RESET);
|
|
|
110 |
Router::parseExtensions('json');
|
|
|
111 |
Router::connect('/test_plugin/api/v1/:action', array('controller' => 'api'));
|
|
|
112 |
CakePlugin::load('TestPlugin');
|
|
|
113 |
|
|
|
114 |
$request = new CakeRequest('test_plugin/api/v1/forwarding.json');
|
|
|
115 |
$event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
|
|
|
116 |
$this->assertNull($filter->beforeDispatch($event));
|
|
|
117 |
$this->assertFalse($event->isStopped(), 'Events for routed extensions should not be stopped');
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Tests that $response->checkNotModified() is called and bypasses
|
|
|
122 |
* file dispatching
|
|
|
123 |
*
|
|
|
124 |
* @return void
|
|
|
125 |
* @triggers DispatcherTest $this, compact('request', 'response')
|
|
|
126 |
* @triggers DispatcherTest $this, compact('request', 'response')
|
|
|
127 |
*/
|
|
|
128 |
public function testNotModified() {
|
|
|
129 |
$filter = new AssetDispatcher();
|
|
|
130 |
Configure::write('Asset.filter', array(
|
|
|
131 |
'js' => '',
|
|
|
132 |
'css' => ''
|
|
|
133 |
));
|
|
|
134 |
App::build(array(
|
|
|
135 |
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
|
|
|
136 |
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
|
|
|
137 |
));
|
|
|
138 |
$time = filemtime(App::themePath('TestTheme') . 'webroot' . DS . 'img' . DS . 'cake.power.gif');
|
|
|
139 |
$time = new DateTime('@' . $time);
|
|
|
140 |
|
|
|
141 |
$response = $this->getMock('CakeResponse', array('send', 'checkNotModified'));
|
|
|
142 |
$request = new CakeRequest('theme/test_theme/img/cake.power.gif');
|
|
|
143 |
|
|
|
144 |
$response->expects($this->once())->method('checkNotModified')
|
|
|
145 |
->with($request)
|
|
|
146 |
->will($this->returnValue(true));
|
|
|
147 |
$event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
|
|
|
148 |
|
|
|
149 |
ob_start();
|
|
|
150 |
$this->assertSame($response, $filter->beforeDispatch($event));
|
|
|
151 |
ob_end_clean();
|
|
|
152 |
$this->assertEquals(200, $response->statusCode());
|
|
|
153 |
$this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified());
|
|
|
154 |
|
|
|
155 |
$response = $this->getMock('CakeResponse', array('_sendHeader', 'checkNotModified'));
|
|
|
156 |
$request = new CakeRequest('theme/test_theme/img/cake.power.gif');
|
|
|
157 |
|
|
|
158 |
$response->expects($this->once())->method('checkNotModified')
|
|
|
159 |
->with($request)
|
|
|
160 |
->will($this->returnValue(true));
|
|
|
161 |
$response->expects($this->never())->method('send');
|
|
|
162 |
$event = new CakeEvent('DispatcherTest', $this, compact('request', 'response'));
|
|
|
163 |
|
|
|
164 |
$this->assertSame($response, $filter->beforeDispatch($event));
|
|
|
165 |
$this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified());
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
/**
|
|
|
169 |
* Test that no exceptions are thrown for //index.php type URLs.
|
|
|
170 |
*
|
|
|
171 |
* @return void
|
|
|
172 |
* @triggers Dispatcher.beforeRequest $this, compact('request', 'response')
|
|
|
173 |
*/
|
|
|
174 |
public function test404OnDoubleSlash() {
|
|
|
175 |
$filter = new AssetDispatcher();
|
|
|
176 |
|
|
|
177 |
$response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
178 |
$request = new CakeRequest('//index.php');
|
|
|
179 |
$event = new CakeEvent('Dispatcher.beforeRequest', $this, compact('request', 'response'));
|
|
|
180 |
|
|
|
181 |
$this->assertNull($filter->beforeDispatch($event));
|
|
|
182 |
$this->assertFalse($event->isStopped());
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
/**
|
|
|
186 |
* Test that attempts to traverse directories are prevented.
|
|
|
187 |
*
|
|
|
188 |
* @return void
|
|
|
189 |
* @triggers Dispatcher.beforeRequest $this, compact('request', 'response')
|
|
|
190 |
*/
|
|
|
191 |
public function test404OnDoubleDot() {
|
|
|
192 |
App::build(array(
|
|
|
193 |
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
|
|
|
194 |
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
|
|
|
195 |
), App::RESET);
|
|
|
196 |
|
|
|
197 |
$response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
198 |
$request = new CakeRequest('theme/test_theme/../../../../../../VERSION.txt');
|
|
|
199 |
$event = new CakeEvent('Dispatcher.beforeRequest', $this, compact('request', 'response'));
|
|
|
200 |
|
|
|
201 |
$response->expects($this->never())->method('send');
|
|
|
202 |
|
|
|
203 |
$filter = new AssetDispatcher();
|
|
|
204 |
$this->assertNull($filter->beforeDispatch($event));
|
|
|
205 |
$this->assertFalse($event->isStopped());
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
/**
|
|
|
209 |
* Test that attempts to traverse directories with urlencoded paths fail.
|
|
|
210 |
*
|
|
|
211 |
* @return void
|
|
|
212 |
* @triggers Dispatcher.beforeRequest $this, compact('request', 'response')
|
|
|
213 |
*/
|
|
|
214 |
public function test404OnDoubleDotEncoded() {
|
|
|
215 |
App::build(array(
|
|
|
216 |
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
|
|
|
217 |
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
|
|
|
218 |
), App::RESET);
|
|
|
219 |
|
|
|
220 |
$response = $this->getMock('CakeResponse', array('_sendHeader', 'send'));
|
|
|
221 |
$request = new CakeRequest('theme/test_theme/%2e./%2e./%2e./%2e./%2e./%2e./VERSION.txt');
|
|
|
222 |
$event = new CakeEvent('Dispatcher.beforeRequest', $this, compact('request', 'response'));
|
|
|
223 |
|
|
|
224 |
$response->expects($this->never())->method('send');
|
|
|
225 |
|
|
|
226 |
$filter = new AssetDispatcher();
|
|
|
227 |
$this->assertNull($filter->beforeDispatch($event));
|
|
|
228 |
$this->assertFalse($event->isStopped());
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
/**
|
|
|
232 |
* Test asset content length is unset
|
|
|
233 |
*
|
|
|
234 |
* If content length is unset, then the webserver can figure it out.
|
|
|
235 |
*
|
|
|
236 |
* @outputBuffering enabled
|
|
|
237 |
* @return void
|
|
|
238 |
*/
|
|
|
239 |
public function testAssetContentLength() {
|
|
|
240 |
Router::reload();
|
|
|
241 |
Configure::write('Dispatcher.filters', array('AssetDispatcher'));
|
|
|
242 |
App::build(array(
|
|
|
243 |
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
|
|
|
244 |
));
|
|
|
245 |
|
|
|
246 |
$url = 'theme/test_theme/css/test_asset.css';
|
|
|
247 |
$file = 'View/Themed/TestTheme/webroot/css/test_asset.css';
|
|
|
248 |
|
|
|
249 |
$request = new CakeRequest($url);
|
|
|
250 |
$response = $this->getMock('CakeResponse', array('_sendHeader', 'send'));
|
|
|
251 |
$event = new CakeEvent('Dispatcher.beforeRequest', $this, compact('request', 'response'));
|
|
|
252 |
|
|
|
253 |
$filter = new AssetDispatcher();
|
|
|
254 |
$filter->beforeDispatch($event);
|
|
|
255 |
$result = ob_get_clean();
|
|
|
256 |
|
|
|
257 |
$path = CAKE . 'Test' . DS . 'test_app' . DS . str_replace('/', DS, $file);
|
|
|
258 |
$file = file_get_contents($path);
|
|
|
259 |
$this->assertEquals($file, $result);
|
|
|
260 |
|
|
|
261 |
$expected = filesize($path);
|
|
|
262 |
$headers = $response->header();
|
|
|
263 |
$this->assertFalse($headers['Content-Length']);
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
}
|