pictcode / lib / Cake / Test / Case / Controller / Component / RequestHandlerComponentTest.php @ 635eef61
履歴 | 表示 | アノテート | ダウンロード (31.327 KB)
1 |
<?php
|
---|---|
2 |
/**
|
3 |
* RequestHandlerComponentTest file
|
4 |
*
|
5 |
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
7 |
*
|
8 |
* Licensed under The MIT License
|
9 |
* For full copyright and license information, please see the LICENSE.txt
|
10 |
* Redistributions of files must retain the above copyright notice
|
11 |
*
|
12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
13 |
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
14 |
* @package Cake.Test.Case.Controller.Component
|
15 |
* @since CakePHP(tm) v 1.2.0.5435
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
17 |
*/
|
18 |
|
19 |
App::uses('Controller', 'Controller'); |
20 |
App::uses('RequestHandlerComponent', 'Controller/Component'); |
21 |
App::uses('CakeRequest', 'Network'); |
22 |
App::uses('CakeResponse', 'Network'); |
23 |
App::uses('Router', 'Routing'); |
24 |
App::uses('JsonView', 'View'); |
25 |
|
26 |
/**
|
27 |
* RequestHandlerTestController class
|
28 |
*
|
29 |
* @package Cake.Test.Case.Controller.Component
|
30 |
*/
|
31 |
class RequestHandlerTestController extends Controller { |
32 |
|
33 |
/**
|
34 |
* uses property
|
35 |
*
|
36 |
* @var mixed
|
37 |
*/
|
38 |
public $uses = null; |
39 |
|
40 |
/**
|
41 |
* test method for ajax redirection
|
42 |
*
|
43 |
* @return void
|
44 |
*/
|
45 |
public function destination() { |
46 |
$this->viewPath = 'Posts'; |
47 |
$this->render('index'); |
48 |
} |
49 |
|
50 |
/**
|
51 |
* test method for ajax redirection + parameter parsing
|
52 |
*
|
53 |
* @return void
|
54 |
*/
|
55 |
public function param_method($one = null, $two = null) { |
56 |
echo "one: $one two: $two"; |
57 |
$this->autoRender = false; |
58 |
} |
59 |
|
60 |
/**
|
61 |
* test method for testing layout rendering when isAjax()
|
62 |
*
|
63 |
* @return void
|
64 |
*/
|
65 |
public function ajax2_layout() { |
66 |
if ($this->autoLayout) { |
67 |
$this->layout = 'ajax2'; |
68 |
} |
69 |
$this->destination();
|
70 |
} |
71 |
|
72 |
} |
73 |
|
74 |
/**
|
75 |
* CustomJsonView class
|
76 |
*
|
77 |
* @package Cake.Test.Case.Controller.Component
|
78 |
*/
|
79 |
class CustomJsonView extends JsonView { |
80 |
|
81 |
} |
82 |
|
83 |
/**
|
84 |
* RequestHandlerComponentTest class
|
85 |
*
|
86 |
* @package Cake.Test.Case.Controller.Component
|
87 |
*/
|
88 |
class RequestHandlerComponentTest extends CakeTestCase { |
89 |
|
90 |
/**
|
91 |
* Controller property
|
92 |
*
|
93 |
* @var RequestHandlerTestController
|
94 |
*/
|
95 |
public $Controller; |
96 |
|
97 |
/**
|
98 |
* RequestHandler property
|
99 |
*
|
100 |
* @var RequestHandlerComponent
|
101 |
*/
|
102 |
public $RequestHandler; |
103 |
|
104 |
/**
|
105 |
* setUp method
|
106 |
*
|
107 |
* @return void
|
108 |
*/
|
109 |
public function setUp() { |
110 |
parent::setUp();
|
111 |
$this->_init();
|
112 |
} |
113 |
|
114 |
/**
|
115 |
* init method
|
116 |
*
|
117 |
* @return void
|
118 |
*/
|
119 |
protected function _init() { |
120 |
$request = new CakeRequest('controller_posts/index'); |
121 |
$response = new CakeResponse(); |
122 |
$this->Controller = new RequestHandlerTestController($request, $response); |
123 |
$this->Controller->constructClasses(); |
124 |
$this->RequestHandler = new RequestHandlerComponent($this->Controller->Components); |
125 |
$this->_extensions = Router::extensions(); |
126 |
} |
127 |
|
128 |
/**
|
129 |
* tearDown method
|
130 |
*
|
131 |
* @return void
|
132 |
*/
|
133 |
public function tearDown() { |
134 |
parent::tearDown();
|
135 |
unset($this->RequestHandler, $this->Controller); |
136 |
if (!headers_sent()) { |
137 |
header('Content-type: text/html'); //reset content type. |
138 |
} |
139 |
call_user_func_array('Router::parseExtensions', $this->_extensions); |
140 |
} |
141 |
|
142 |
/**
|
143 |
* Test that the constructor sets the settings.
|
144 |
*
|
145 |
* @return void
|
146 |
*/
|
147 |
public function testConstructorSettings() { |
148 |
$settings = array( |
149 |
'ajaxLayout' => 'test_ajax', |
150 |
'viewClassMap' => array('json' => 'MyPlugin.MyJson') |
151 |
); |
152 |
$Collection = new ComponentCollection(); |
153 |
$Collection->init($this->Controller); |
154 |
$RequestHandler = new RequestHandlerComponent($Collection, $settings); |
155 |
$this->assertEquals('test_ajax', $RequestHandler->ajaxLayout); |
156 |
$this->assertEquals(array('json' => 'MyPlugin.MyJson'), $RequestHandler->settings['viewClassMap']); |
157 |
} |
158 |
|
159 |
/**
|
160 |
* testInitializeCallback method
|
161 |
*
|
162 |
* @return void
|
163 |
*/
|
164 |
public function testInitializeCallback() { |
165 |
$this->assertNull($this->RequestHandler->ext); |
166 |
$this->Controller->request->params['ext'] = 'rss'; |
167 |
$this->RequestHandler->initialize($this->Controller); |
168 |
$this->assertEquals('rss', $this->RequestHandler->ext); |
169 |
} |
170 |
|
171 |
/**
|
172 |
* test that a mapped Accept-type header will set $this->ext correctly.
|
173 |
*
|
174 |
* @return void
|
175 |
*/
|
176 |
public function testInitializeContentTypeSettingExt() { |
177 |
$this->assertNull($this->RequestHandler->ext); |
178 |
|
179 |
$_SERVER['HTTP_ACCEPT'] = 'application/json'; |
180 |
Router::parseExtensions('json'); |
181 |
|
182 |
$this->RequestHandler->initialize($this->Controller); |
183 |
$this->assertEquals('json', $this->RequestHandler->ext); |
184 |
} |
185 |
|
186 |
/**
|
187 |
* Test that RequestHandler sets $this->ext when jQuery sends its wonky-ish headers.
|
188 |
*
|
189 |
* @return void
|
190 |
*/
|
191 |
public function testInitializeContentTypeWithjQueryAccept() { |
192 |
$_SERVER['HTTP_ACCEPT'] = 'application/json, application/javascript, */*; q=0.01'; |
193 |
$this->assertNull($this->RequestHandler->ext); |
194 |
Router::parseExtensions('json'); |
195 |
|
196 |
$this->RequestHandler->initialize($this->Controller); |
197 |
$this->assertEquals('json', $this->RequestHandler->ext); |
198 |
} |
199 |
|
200 |
/**
|
201 |
* Test that RequestHandler does not set extension to csv for text/plain mimetype
|
202 |
*
|
203 |
* @return void
|
204 |
*/
|
205 |
public function testInitializeContentTypeWithjQueryTextPlainAccept() { |
206 |
$_SERVER['HTTP_ACCEPT'] = 'text/plain, */*; q=0.01'; |
207 |
$this->assertNull($this->RequestHandler->ext); |
208 |
Router::parseExtensions('csv'); |
209 |
|
210 |
$this->RequestHandler->initialize($this->Controller); |
211 |
$this->assertNull($this->RequestHandler->ext); |
212 |
} |
213 |
|
214 |
/**
|
215 |
* Test that RequestHandler sets $this->ext when jQuery sends its wonky-ish headers
|
216 |
* and the application is configured to handle multiple extensions
|
217 |
*
|
218 |
* @return void
|
219 |
*/
|
220 |
public function testInitializeContentTypeWithjQueryAcceptAndMultiplesExtensions() { |
221 |
$_SERVER['HTTP_ACCEPT'] = 'application/json, application/javascript, */*; q=0.01'; |
222 |
$this->assertNull($this->RequestHandler->ext); |
223 |
Router::parseExtensions('rss', 'json'); |
224 |
|
225 |
$this->RequestHandler->initialize($this->Controller); |
226 |
$this->assertEquals('json', $this->RequestHandler->ext); |
227 |
} |
228 |
|
229 |
/**
|
230 |
* Test that RequestHandler does not set $this->ext when multiple accepts are sent.
|
231 |
*
|
232 |
* @return void
|
233 |
*/
|
234 |
public function testInitializeNoContentTypeWithSingleAccept() { |
235 |
$_SERVER['HTTP_ACCEPT'] = 'application/json, text/html, */*; q=0.01'; |
236 |
$this->assertNull($this->RequestHandler->ext); |
237 |
Router::parseExtensions('json'); |
238 |
|
239 |
$this->RequestHandler->initialize($this->Controller); |
240 |
$this->assertNull($this->RequestHandler->ext); |
241 |
} |
242 |
|
243 |
/**
|
244 |
* Test that ext is set to the first listed extension with multiple accepted
|
245 |
* content types.
|
246 |
* Having multiple types accepted with same weight, means the client lets the
|
247 |
* server choose the returned content type.
|
248 |
*
|
249 |
* @return void
|
250 |
*/
|
251 |
public function testInitializeNoContentTypeWithMultipleAcceptedTypes() { |
252 |
$_SERVER['HTTP_ACCEPT'] = 'application/json, application/javascript, application/xml, */*; q=0.01'; |
253 |
$this->assertNull($this->RequestHandler->ext); |
254 |
Router::parseExtensions('xml', 'json'); |
255 |
|
256 |
$this->RequestHandler->initialize($this->Controller); |
257 |
$this->assertEquals('xml', $this->RequestHandler->ext); |
258 |
|
259 |
$this->RequestHandler->ext = null; |
260 |
Router::setExtensions(array('json', 'xml'), false); |
261 |
|
262 |
$this->RequestHandler->initialize($this->Controller); |
263 |
$this->assertEquals('json', $this->RequestHandler->ext); |
264 |
} |
265 |
|
266 |
/**
|
267 |
* Test that ext is set to type with highest weight
|
268 |
*
|
269 |
* @return void
|
270 |
*/
|
271 |
public function testInitializeContentTypeWithMultipleAcceptedTypes() { |
272 |
$_SERVER['HTTP_ACCEPT'] = 'text/csv;q=1.0, application/json;q=0.8, application/xml;q=0.7'; |
273 |
$this->assertNull($this->RequestHandler->ext); |
274 |
Router::parseExtensions('xml', 'json'); |
275 |
|
276 |
$this->RequestHandler->initialize($this->Controller); |
277 |
$this->assertEquals('json', $this->RequestHandler->ext); |
278 |
} |
279 |
|
280 |
/**
|
281 |
* Test that ext is not set with confusing android accepts headers.
|
282 |
*
|
283 |
* @return void
|
284 |
*/
|
285 |
public function testInitializeAmbiguousAndroidAccepts() { |
286 |
$_SERVER['HTTP_ACCEPT'] = 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'; |
287 |
$this->assertNull($this->RequestHandler->ext); |
288 |
Router::parseExtensions('html', 'xml'); |
289 |
|
290 |
$this->RequestHandler->initialize($this->Controller); |
291 |
$this->assertNull($this->RequestHandler->ext); |
292 |
} |
293 |
|
294 |
/**
|
295 |
* Test that the headers sent by firefox are not treated as XML requests.
|
296 |
*
|
297 |
* @return void
|
298 |
*/
|
299 |
public function testInititalizeFirefoxHeaderNotXml() { |
300 |
$_SERVER['HTTP_ACCEPT'] = 'text/html,application/xhtml+xml,application/xml;image/png,image/jpeg,image/*;q=0.9,*/*;q=0.8'; |
301 |
Router::parseExtensions('xml', 'json'); |
302 |
|
303 |
$this->RequestHandler->initialize($this->Controller); |
304 |
$this->assertNull($this->RequestHandler->ext); |
305 |
} |
306 |
|
307 |
/**
|
308 |
* Test that a type mismatch doesn't incorrectly set the ext
|
309 |
*
|
310 |
* @return void
|
311 |
*/
|
312 |
public function testInitializeContentTypeAndExtensionMismatch() { |
313 |
$this->assertNull($this->RequestHandler->ext); |
314 |
$extensions = Router::extensions(); |
315 |
Router::parseExtensions('xml'); |
316 |
|
317 |
$this->Controller->request = $this->getMock('CakeRequest'); |
318 |
$this->Controller->request->expects($this->any()) |
319 |
->method('accepts')
|
320 |
->will($this->returnValue(array('application/json'))); |
321 |
|
322 |
$this->RequestHandler->initialize($this->Controller); |
323 |
$this->assertNull($this->RequestHandler->ext); |
324 |
|
325 |
call_user_func_array(array('Router', 'parseExtensions'), $extensions); |
326 |
} |
327 |
|
328 |
/**
|
329 |
* testViewClassMap method
|
330 |
*
|
331 |
* @return void
|
332 |
*/
|
333 |
public function testViewClassMap() { |
334 |
$this->RequestHandler->settings = array('viewClassMap' => array('json' => 'CustomJson')); |
335 |
$this->RequestHandler->initialize($this->Controller); |
336 |
$result = $this->RequestHandler->viewClassMap(); |
337 |
$expected = array( |
338 |
'json' => 'CustomJson', |
339 |
'xml' => 'Xml' |
340 |
); |
341 |
$this->assertEquals($expected, $result); |
342 |
|
343 |
$result = $this->RequestHandler->viewClassMap('xls', 'Excel.Excel'); |
344 |
$expected = array( |
345 |
'json' => 'CustomJson', |
346 |
'xml' => 'Xml', |
347 |
'xls' => 'Excel.Excel' |
348 |
); |
349 |
$this->assertEquals($expected, $result); |
350 |
|
351 |
$this->RequestHandler->renderAs($this->Controller, 'json'); |
352 |
$this->assertEquals('CustomJson', $this->Controller->viewClass); |
353 |
} |
354 |
|
355 |
/**
|
356 |
* testDisabling method
|
357 |
*
|
358 |
* @return void
|
359 |
*/
|
360 |
public function testDisabling() { |
361 |
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; |
362 |
$this->_init();
|
363 |
$this->RequestHandler->initialize($this->Controller); |
364 |
$this->Controller->beforeFilter(); |
365 |
$this->RequestHandler->startup($this->Controller); |
366 |
$this->assertEquals(true, $this->Controller->params['isAjax']); |
367 |
} |
368 |
|
369 |
/**
|
370 |
* testAutoAjaxLayout method
|
371 |
*
|
372 |
* @return void
|
373 |
*/
|
374 |
public function testAutoAjaxLayout() { |
375 |
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; |
376 |
$this->RequestHandler->startup($this->Controller); |
377 |
$this->assertEquals($this->Controller->layout, $this->RequestHandler->ajaxLayout); |
378 |
|
379 |
$this->_init();
|
380 |
$this->Controller->request->params['ext'] = 'js'; |
381 |
$this->RequestHandler->initialize($this->Controller); |
382 |
$this->RequestHandler->startup($this->Controller); |
383 |
$this->assertNotEquals('ajax', $this->Controller->layout); |
384 |
|
385 |
unset($_SERVER['HTTP_X_REQUESTED_WITH']); |
386 |
} |
387 |
|
388 |
/**
|
389 |
* testStartupCallback method
|
390 |
*
|
391 |
* @return void
|
392 |
*/
|
393 |
public function testStartupCallback() { |
394 |
$_SERVER['REQUEST_METHOD'] = 'PUT'; |
395 |
$_SERVER['CONTENT_TYPE'] = 'application/xml'; |
396 |
$this->Controller->request = $this->getMock('CakeRequest', array('_readInput')); |
397 |
$this->RequestHandler->startup($this->Controller); |
398 |
$this->assertTrue(is_array($this->Controller->data)); |
399 |
$this->assertFalse(is_object($this->Controller->data)); |
400 |
} |
401 |
|
402 |
/**
|
403 |
* testStartupCallback with charset.
|
404 |
*
|
405 |
* @return void
|
406 |
*/
|
407 |
public function testStartupCallbackCharset() { |
408 |
$_SERVER['REQUEST_METHOD'] = 'PUT'; |
409 |
$_SERVER['CONTENT_TYPE'] = 'application/xml; charset=UTF-8'; |
410 |
$this->Controller->request = $this->getMock('CakeRequest', array('_readInput')); |
411 |
$this->RequestHandler->startup($this->Controller); |
412 |
$this->assertTrue(is_array($this->Controller->data)); |
413 |
$this->assertFalse(is_object($this->Controller->data)); |
414 |
} |
415 |
|
416 |
/**
|
417 |
* Test mapping a new type and having startup process it.
|
418 |
*
|
419 |
* @return void
|
420 |
*/
|
421 |
public function testStartupCustomTypeProcess() { |
422 |
if (!function_exists('str_getcsv')) { |
423 |
$this->markTestSkipped('Need "str_getcsv" for this test.'); |
424 |
} |
425 |
$_SERVER['REQUEST_METHOD'] = 'POST'; |
426 |
$_SERVER['CONTENT_TYPE'] = 'text/csv'; |
427 |
$this->Controller->request = $this->getMock('CakeRequest', array('_readInput')); |
428 |
$this->Controller->request->expects($this->once()) |
429 |
->method('_readInput')
|
430 |
->will($this->returnValue('"A","csv","string"')); |
431 |
$this->RequestHandler->addInputType('csv', array('str_getcsv')); |
432 |
$this->RequestHandler->startup($this->Controller); |
433 |
$expected = array( |
434 |
'A', 'csv', 'string' |
435 |
); |
436 |
$this->assertEquals($expected, $this->Controller->request->data); |
437 |
} |
438 |
|
439 |
/**
|
440 |
* testNonAjaxRedirect method
|
441 |
*
|
442 |
* @return void
|
443 |
*/
|
444 |
public function testNonAjaxRedirect() { |
445 |
$this->RequestHandler->initialize($this->Controller); |
446 |
$this->RequestHandler->startup($this->Controller); |
447 |
$this->assertNull($this->RequestHandler->beforeRedirect($this->Controller, '/')); |
448 |
} |
449 |
|
450 |
/**
|
451 |
* test that redirects with ajax and no URL don't do anything.
|
452 |
*
|
453 |
* @return void
|
454 |
*/
|
455 |
public function testAjaxRedirectWithNoUrl() { |
456 |
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; |
457 |
$this->Controller->response = $this->getMock('CakeResponse'); |
458 |
|
459 |
$this->Controller->response->expects($this->never()) |
460 |
->method('body');
|
461 |
|
462 |
$this->RequestHandler->initialize($this->Controller); |
463 |
$this->RequestHandler->startup($this->Controller); |
464 |
$this->assertNull($this->RequestHandler->beforeRedirect($this->Controller, null)); |
465 |
} |
466 |
|
467 |
/**
|
468 |
* testRenderAs method
|
469 |
*
|
470 |
* @return void
|
471 |
*/
|
472 |
public function testRenderAs() { |
473 |
$this->assertFalse(in_array('Rss', $this->Controller->helpers)); |
474 |
$this->RequestHandler->renderAs($this->Controller, 'rss'); |
475 |
$this->assertTrue(in_array('Rss', $this->Controller->helpers)); |
476 |
|
477 |
$this->Controller->viewPath = 'request_handler_test\\rss'; |
478 |
$this->RequestHandler->renderAs($this->Controller, 'js'); |
479 |
$this->assertEquals('request_handler_test' . DS . 'js', $this->Controller->viewPath); |
480 |
} |
481 |
|
482 |
/**
|
483 |
* test that attachment headers work with renderAs
|
484 |
*
|
485 |
* @return void
|
486 |
*/
|
487 |
public function testRenderAsWithAttachment() { |
488 |
$this->RequestHandler->request = $this->getMock('CakeRequest'); |
489 |
$this->RequestHandler->request->expects($this->any()) |
490 |
->method('parseAccept')
|
491 |
->will($this->returnValue(array('1.0' => array('application/xml')))); |
492 |
|
493 |
$this->RequestHandler->response = $this->getMock('CakeResponse', array('type', 'download', 'charset')); |
494 |
$this->RequestHandler->response->expects($this->at(0)) |
495 |
->method('type')
|
496 |
->with('application/xml');
|
497 |
$this->RequestHandler->response->expects($this->at(1)) |
498 |
->method('charset')
|
499 |
->with('UTF-8');
|
500 |
$this->RequestHandler->response->expects($this->at(2)) |
501 |
->method('download')
|
502 |
->with('myfile.xml');
|
503 |
|
504 |
$this->RequestHandler->renderAs($this->Controller, 'xml', array('attachment' => 'myfile.xml')); |
505 |
|
506 |
$this->assertEquals('Xml', $this->Controller->viewClass); |
507 |
} |
508 |
|
509 |
/**
|
510 |
* test that respondAs works as expected.
|
511 |
*
|
512 |
* @return void
|
513 |
*/
|
514 |
public function testRespondAs() { |
515 |
$this->RequestHandler->response = $this->getMock('CakeResponse', array('type')); |
516 |
$this->RequestHandler->response->expects($this->at(0))->method('type') |
517 |
->with('application/json');
|
518 |
$this->RequestHandler->response->expects($this->at(1))->method('type') |
519 |
->with('text/xml');
|
520 |
|
521 |
$result = $this->RequestHandler->respondAs('json'); |
522 |
$this->assertTrue($result); |
523 |
$result = $this->RequestHandler->respondAs('text/xml'); |
524 |
$this->assertTrue($result); |
525 |
} |
526 |
|
527 |
/**
|
528 |
* test that attachment headers work with respondAs
|
529 |
*
|
530 |
* @return void
|
531 |
*/
|
532 |
public function testRespondAsWithAttachment() { |
533 |
$this->RequestHandler = $this->getMock( |
534 |
'RequestHandlerComponent',
|
535 |
array('_header'), |
536 |
array(&$this->Controller->Components) |
537 |
); |
538 |
$this->RequestHandler->response = $this->getMock('CakeResponse', array('type', 'download')); |
539 |
$this->RequestHandler->request = $this->getMock('CakeRequest'); |
540 |
|
541 |
$this->RequestHandler->request->expects($this->once()) |
542 |
->method('parseAccept')
|
543 |
->will($this->returnValue(array('1.0' => array('application/xml')))); |
544 |
|
545 |
$this->RequestHandler->response->expects($this->once())->method('download') |
546 |
->with('myfile.xml');
|
547 |
$this->RequestHandler->response->expects($this->once())->method('type') |
548 |
->with('application/xml');
|
549 |
|
550 |
$result = $this->RequestHandler->respondAs('xml', array('attachment' => 'myfile.xml')); |
551 |
$this->assertTrue($result); |
552 |
} |
553 |
|
554 |
/**
|
555 |
* test that calling renderAs() more than once continues to work.
|
556 |
*
|
557 |
* @link #6466
|
558 |
* @return void
|
559 |
*/
|
560 |
public function testRenderAsCalledTwice() { |
561 |
$this->RequestHandler->renderAs($this->Controller, 'print'); |
562 |
$this->assertEquals('RequestHandlerTest' . DS . 'print', $this->Controller->viewPath); |
563 |
$this->assertEquals('print', $this->Controller->layoutPath); |
564 |
|
565 |
$this->RequestHandler->renderAs($this->Controller, 'js'); |
566 |
$this->assertEquals('RequestHandlerTest' . DS . 'js', $this->Controller->viewPath); |
567 |
$this->assertEquals('js', $this->Controller->layoutPath); |
568 |
$this->assertTrue(in_array('Js', $this->Controller->helpers)); |
569 |
} |
570 |
|
571 |
/**
|
572 |
* testRequestClientTypes method
|
573 |
*
|
574 |
* @return void
|
575 |
*/
|
576 |
public function testRequestClientTypes() { |
577 |
$_SERVER['HTTP_X_PROTOTYPE_VERSION'] = '1.5'; |
578 |
$this->assertEquals('1.5', $this->RequestHandler->getAjaxVersion()); |
579 |
|
580 |
unset($_SERVER['HTTP_X_REQUESTED_WITH'], $_SERVER['HTTP_X_PROTOTYPE_VERSION']); |
581 |
$this->assertFalse($this->RequestHandler->getAjaxVersion()); |
582 |
} |
583 |
|
584 |
/**
|
585 |
* Tests the detection of various Flash versions
|
586 |
*
|
587 |
* @return void
|
588 |
*/
|
589 |
public function testFlashDetection() { |
590 |
$request = $this->getMock('CakeRequest'); |
591 |
$request->expects($this->once())->method('is') |
592 |
->with('flash')
|
593 |
->will($this->returnValue(true)); |
594 |
|
595 |
$this->RequestHandler->request = $request; |
596 |
$this->assertTrue($this->RequestHandler->isFlash()); |
597 |
} |
598 |
|
599 |
/**
|
600 |
* testRequestContentTypes method
|
601 |
*
|
602 |
* @return void
|
603 |
*/
|
604 |
public function testRequestContentTypes() { |
605 |
$_SERVER['REQUEST_METHOD'] = 'GET'; |
606 |
$this->assertNull($this->RequestHandler->requestedWith()); |
607 |
|
608 |
$_SERVER['REQUEST_METHOD'] = 'POST'; |
609 |
$_SERVER['CONTENT_TYPE'] = 'application/json'; |
610 |
$this->assertEquals('json', $this->RequestHandler->requestedWith()); |
611 |
|
612 |
$result = $this->RequestHandler->requestedWith(array('json', 'xml')); |
613 |
$this->assertEquals('json', $result); |
614 |
|
615 |
$result = $this->RequestHandler->requestedWith(array('rss', 'atom')); |
616 |
$this->assertFalse($result); |
617 |
|
618 |
$_SERVER['REQUEST_METHOD'] = 'DELETE'; |
619 |
$this->assertEquals('json', $this->RequestHandler->requestedWith()); |
620 |
|
621 |
$_SERVER['REQUEST_METHOD'] = 'POST'; |
622 |
unset($_SERVER['CONTENT_TYPE']); |
623 |
$_SERVER['HTTP_CONTENT_TYPE'] = 'application/json'; |
624 |
|
625 |
$result = $this->RequestHandler->requestedWith(array('json', 'xml')); |
626 |
$this->assertEquals('json', $result); |
627 |
|
628 |
$result = $this->RequestHandler->requestedWith(array('rss', 'atom')); |
629 |
$this->assertFalse($result); |
630 |
|
631 |
$_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'; |
632 |
$this->assertTrue($this->RequestHandler->isXml()); |
633 |
$this->assertFalse($this->RequestHandler->isAtom()); |
634 |
$this->assertFalse($this->RequestHandler->isRSS()); |
635 |
|
636 |
$_SERVER['HTTP_ACCEPT'] = 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'; |
637 |
$this->assertTrue($this->RequestHandler->isAtom()); |
638 |
$this->assertFalse($this->RequestHandler->isRSS()); |
639 |
|
640 |
$_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'; |
641 |
$this->assertFalse($this->RequestHandler->isAtom()); |
642 |
$this->assertTrue($this->RequestHandler->isRSS()); |
643 |
|
644 |
$this->assertFalse($this->RequestHandler->isWap()); |
645 |
$_SERVER['HTTP_ACCEPT'] = 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*'; |
646 |
$this->assertTrue($this->RequestHandler->isWap()); |
647 |
|
648 |
$_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'; |
649 |
} |
650 |
|
651 |
/**
|
652 |
* testResponseContentType method
|
653 |
*
|
654 |
* @return void
|
655 |
*/
|
656 |
public function testResponseContentType() { |
657 |
$this->assertEquals('html', $this->RequestHandler->responseType()); |
658 |
$this->assertTrue($this->RequestHandler->respondAs('atom')); |
659 |
$this->assertEquals('atom', $this->RequestHandler->responseType()); |
660 |
} |
661 |
|
662 |
/**
|
663 |
* testMobileDeviceDetection method
|
664 |
*
|
665 |
* @return void
|
666 |
*/
|
667 |
public function testMobileDeviceDetection() { |
668 |
$request = $this->getMock('CakeRequest'); |
669 |
$request->expects($this->once())->method('is') |
670 |
->with('mobile')
|
671 |
->will($this->returnValue(true)); |
672 |
|
673 |
$this->RequestHandler->request = $request; |
674 |
$this->assertTrue($this->RequestHandler->isMobile()); |
675 |
} |
676 |
|
677 |
/**
|
678 |
* testRequestProperties method
|
679 |
*
|
680 |
* @return void
|
681 |
*/
|
682 |
public function testRequestProperties() { |
683 |
$request = $this->getMock('CakeRequest'); |
684 |
$request->expects($this->once())->method('is') |
685 |
->with('ssl')
|
686 |
->will($this->returnValue(true)); |
687 |
|
688 |
$this->RequestHandler->request = $request; |
689 |
$this->assertTrue($this->RequestHandler->isSsl()); |
690 |
} |
691 |
|
692 |
/**
|
693 |
* testRequestMethod method
|
694 |
*
|
695 |
* @return void
|
696 |
*/
|
697 |
public function testRequestMethod() { |
698 |
$request = $this->getMock('CakeRequest'); |
699 |
$request->expects($this->at(0))->method('is') |
700 |
->with('get')
|
701 |
->will($this->returnValue(true)); |
702 |
|
703 |
$request->expects($this->at(1))->method('is') |
704 |
->with('post')
|
705 |
->will($this->returnValue(false)); |
706 |
|
707 |
$request->expects($this->at(2))->method('is') |
708 |
->with('delete')
|
709 |
->will($this->returnValue(true)); |
710 |
|
711 |
$request->expects($this->at(3))->method('is') |
712 |
->with('put')
|
713 |
->will($this->returnValue(false)); |
714 |
|
715 |
$this->RequestHandler->request = $request; |
716 |
$this->assertTrue($this->RequestHandler->isGet()); |
717 |
$this->assertFalse($this->RequestHandler->isPost()); |
718 |
$this->assertTrue($this->RequestHandler->isDelete()); |
719 |
$this->assertFalse($this->RequestHandler->isPut()); |
720 |
} |
721 |
|
722 |
/**
|
723 |
* test that map alias converts aliases to content types.
|
724 |
*
|
725 |
* @return void
|
726 |
*/
|
727 |
public function testMapAlias() { |
728 |
$result = $this->RequestHandler->mapAlias('xml'); |
729 |
$this->assertEquals('application/xml', $result); |
730 |
|
731 |
$result = $this->RequestHandler->mapAlias('text/html'); |
732 |
$this->assertNull($result); |
733 |
|
734 |
$result = $this->RequestHandler->mapAlias('wap'); |
735 |
$this->assertEquals('text/vnd.wap.wml', $result); |
736 |
|
737 |
$result = $this->RequestHandler->mapAlias(array('xml', 'js', 'json')); |
738 |
$expected = array('application/xml', 'application/javascript', 'application/json'); |
739 |
$this->assertEquals($expected, $result); |
740 |
} |
741 |
|
742 |
/**
|
743 |
* test accepts() on the component
|
744 |
*
|
745 |
* @return void
|
746 |
*/
|
747 |
public function testAccepts() { |
748 |
$_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'; |
749 |
$this->assertTrue($this->RequestHandler->accepts(array('js', 'xml', 'html'))); |
750 |
$this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo'))); |
751 |
|
752 |
$_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5'; |
753 |
$this->assertFalse($this->RequestHandler->accepts('rss')); |
754 |
} |
755 |
|
756 |
/**
|
757 |
* test accepts and prefers methods.
|
758 |
*
|
759 |
* @return void
|
760 |
*/
|
761 |
public function testPrefers() { |
762 |
$_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'; |
763 |
$this->assertNotEquals('rss', $this->RequestHandler->prefers()); |
764 |
$this->RequestHandler->ext = 'rss'; |
765 |
$this->assertEquals('rss', $this->RequestHandler->prefers()); |
766 |
$this->assertFalse($this->RequestHandler->prefers('xml')); |
767 |
$this->assertEquals('xml', $this->RequestHandler->prefers(array('js', 'xml', 'xhtml'))); |
768 |
$this->assertFalse($this->RequestHandler->prefers(array('red', 'blue'))); |
769 |
$this->assertEquals('xhtml', $this->RequestHandler->prefers(array('js', 'json', 'xhtml'))); |
770 |
$this->assertTrue($this->RequestHandler->prefers(array('rss')), 'Should return true if input matches ext.'); |
771 |
$this->assertFalse($this->RequestHandler->prefers(array('html')), 'No match with ext, return false.'); |
772 |
|
773 |
$_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'; |
774 |
$this->_init();
|
775 |
$this->assertEquals('xml', $this->RequestHandler->prefers()); |
776 |
|
777 |
$_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5'; |
778 |
$this->assertEquals('html', $this->RequestHandler->prefers()); |
779 |
$this->assertFalse($this->RequestHandler->prefers('rss')); |
780 |
} |
781 |
|
782 |
/**
|
783 |
* testCustomContent method
|
784 |
*
|
785 |
* @return void
|
786 |
*/
|
787 |
public function testCustomContent() { |
788 |
$_SERVER['HTTP_ACCEPT'] = 'text/x-mobile,text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5'; |
789 |
$this->RequestHandler->setContent('mobile', 'text/x-mobile'); |
790 |
$this->RequestHandler->startup($this->Controller); |
791 |
$this->assertEquals('mobile', $this->RequestHandler->prefers()); |
792 |
} |
793 |
|
794 |
/**
|
795 |
* testClientProperties method
|
796 |
*
|
797 |
* @return void
|
798 |
*/
|
799 |
public function testClientProperties() { |
800 |
$request = $this->getMock('CakeRequest'); |
801 |
$request->expects($this->once())->method('referer'); |
802 |
$request->expects($this->once())->method('clientIp')->will($this->returnValue(false)); |
803 |
|
804 |
$this->RequestHandler->request = $request; |
805 |
|
806 |
$this->RequestHandler->getReferer(); |
807 |
$this->RequestHandler->getClientIP(false); |
808 |
} |
809 |
|
810 |
/**
|
811 |
* test that ajax requests involving redirects trigger requestAction instead.
|
812 |
*
|
813 |
* @return void
|
814 |
*/
|
815 |
public function testAjaxRedirectAsRequestAction() { |
816 |
App::build(array( |
817 |
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS) |
818 |
), App::RESET); |
819 |
|
820 |
$this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components)); |
821 |
$this->Controller->request = $this->getMock('CakeRequest'); |
822 |
$this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader')); |
823 |
$this->Controller->RequestHandler->request = $this->Controller->request; |
824 |
$this->Controller->RequestHandler->response = $this->Controller->response; |
825 |
$this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true)); |
826 |
$this->Controller->RequestHandler->expects($this->once())->method('_stop'); |
827 |
|
828 |
ob_start(); |
829 |
$this->Controller->RequestHandler->beforeRedirect( |
830 |
$this->Controller, array('controller' => 'request_handler_test', 'action' => 'destination') |
831 |
); |
832 |
$result = ob_get_clean();
|
833 |
$this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.'); |
834 |
|
835 |
App::build();
|
836 |
} |
837 |
|
838 |
/**
|
839 |
* test that ajax requests involving redirects don't force no layout
|
840 |
* this would cause the ajax layout to not be rendered.
|
841 |
*
|
842 |
* @return void
|
843 |
*/
|
844 |
public function testAjaxRedirectAsRequestActionStillRenderingLayout() { |
845 |
App::build(array( |
846 |
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS) |
847 |
), App::RESET); |
848 |
|
849 |
$this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components)); |
850 |
$this->Controller->request = $this->getMock('CakeRequest'); |
851 |
$this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader')); |
852 |
$this->Controller->RequestHandler->request = $this->Controller->request; |
853 |
$this->Controller->RequestHandler->response = $this->Controller->response; |
854 |
$this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true)); |
855 |
$this->Controller->RequestHandler->expects($this->once())->method('_stop'); |
856 |
|
857 |
ob_start(); |
858 |
$this->Controller->RequestHandler->beforeRedirect( |
859 |
$this->Controller, array('controller' => 'request_handler_test', 'action' => 'ajax2_layout') |
860 |
); |
861 |
$result = ob_get_clean();
|
862 |
$this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.'); |
863 |
$this->assertRegExp('/Ajax!/', $result, 'Layout was not rendered.'); |
864 |
|
865 |
App::build();
|
866 |
} |
867 |
|
868 |
/**
|
869 |
* test that the beforeRedirect callback properly converts
|
870 |
* array URLs into their correct string ones, and adds base => false so
|
871 |
* the correct URLs are generated.
|
872 |
*
|
873 |
* @link https://cakephp.lighthouseapp.com/projects/42648-cakephp-1x/tickets/276
|
874 |
* @return void
|
875 |
*/
|
876 |
public function testBeforeRedirectCallbackWithArrayUrl() { |
877 |
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; |
878 |
|
879 |
Router::setRequestInfo(array( |
880 |
array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'named' => array(), 'form' => array(), 'url' => array('url' => 'accounts/')), |
881 |
array('base' => '/officespace', 'here' => '/officespace/accounts/', 'webroot' => '/officespace/') |
882 |
)); |
883 |
|
884 |
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components)); |
885 |
$RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader')); |
886 |
$RequestHandler->request = new CakeRequest('posts/index'); |
887 |
$RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader')); |
888 |
|
889 |
ob_start(); |
890 |
$RequestHandler->beforeRedirect(
|
891 |
$this->Controller, |
892 |
array('controller' => 'request_handler_test', 'action' => 'param_method', 'first', 'second') |
893 |
); |
894 |
$result = ob_get_clean();
|
895 |
$this->assertEquals('one: first two: second', $result); |
896 |
} |
897 |
|
898 |
/**
|
899 |
* assure that beforeRedirect with a status code will correctly set the status header
|
900 |
*
|
901 |
* @return void
|
902 |
*/
|
903 |
public function testBeforeRedirectCallingHeader() { |
904 |
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; |
905 |
|
906 |
$controller = $this->getMock('Controller', array('header')); |
907 |
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components)); |
908 |
$RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader', 'statusCode')); |
909 |
$RequestHandler->request = $this->getMock('CakeRequest'); |
910 |
$RequestHandler->request->expects($this->once())->method('is') |
911 |
->with('ajax')
|
912 |
->will($this->returnValue(true)); |
913 |
|
914 |
$RequestHandler->response->expects($this->once())->method('statusCode')->with(403); |
915 |
|
916 |
ob_start(); |
917 |
$RequestHandler->beforeRedirect($controller, 'request_handler_test/param_method/first/second', 403); |
918 |
ob_get_clean(); |
919 |
} |
920 |
|
921 |
/**
|
922 |
* @expectedException CakeException
|
923 |
* @return void
|
924 |
*/
|
925 |
public function testAddInputTypeException() { |
926 |
$this->RequestHandler->addInputType('csv', array('I am not callable')); |
927 |
} |
928 |
|
929 |
/**
|
930 |
* Test checkNotModified method
|
931 |
*
|
932 |
* @return void
|
933 |
*/
|
934 |
public function testCheckNotModifiedByEtagStar() { |
935 |
$_SERVER['HTTP_IF_NONE_MATCH'] = '*'; |
936 |
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components)); |
937 |
$RequestHandler->response = $this->getMock('CakeResponse', array('notModified')); |
938 |
$RequestHandler->response->etag('something'); |
939 |
$RequestHandler->response->expects($this->once())->method('notModified'); |
940 |
$this->assertFalse($RequestHandler->beforeRender($this->Controller)); |
941 |
} |
942 |
|
943 |
/**
|
944 |
* Test checkNotModified method
|
945 |
*
|
946 |
* @return void
|
947 |
*/
|
948 |
public function testCheckNotModifiedByEtagExact() { |
949 |
$_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"'; |
950 |
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components)); |
951 |
$RequestHandler->response = $this->getMock('CakeResponse', array('notModified')); |
952 |
$RequestHandler->response->etag('something', true); |
953 |
$RequestHandler->response->expects($this->once())->method('notModified'); |
954 |
$this->assertFalse($RequestHandler->beforeRender($this->Controller)); |
955 |
} |
956 |
|
957 |
/**
|
958 |
* Test checkNotModified method
|
959 |
*
|
960 |
* @return void
|
961 |
*/
|
962 |
public function testCheckNotModifiedByEtagAndTime() { |
963 |
$_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"'; |
964 |
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00'; |
965 |
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components)); |
966 |
$RequestHandler->response = $this->getMock('CakeResponse', array('notModified')); |
967 |
$RequestHandler->response->etag('something', true); |
968 |
$RequestHandler->response->modified('2012-01-01 00:00:00'); |
969 |
$RequestHandler->response->expects($this->once())->method('notModified'); |
970 |
$this->assertFalse($RequestHandler->beforeRender($this->Controller)); |
971 |
} |
972 |
|
973 |
/**
|
974 |
* Test checkNotModified method
|
975 |
*
|
976 |
* @return void
|
977 |
*/
|
978 |
public function testCheckNotModifiedNoInfo() { |
979 |
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components)); |
980 |
$RequestHandler->response = $this->getMock('CakeResponse', array('notModified')); |
981 |
$RequestHandler->response->expects($this->never())->method('notModified'); |
982 |
$this->assertNull($RequestHandler->beforeRender($this->Controller)); |
983 |
} |
984 |
} |