comparison cake/tests/cases/libs/controller/components/request_handler.test.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
1 <?php
2 /**
3 * RequestHandlerComponentTest file
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
9 *
10 * Licensed under The Open Group Test Suite License
11 * Redistributions of files must retain the above copyright notice.
12 *
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
15 * @package cake
16 * @subpackage cake.tests.cases.libs.controller.components
17 * @since CakePHP(tm) v 1.2.0.5435
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
19 */
20 App::import('Controller', 'Controller', false);
21 App::import('Component', array('RequestHandler'));
22
23 Mock::generatePartial('RequestHandlerComponent', 'NoStopRequestHandler', array('_stop', '_header'));
24 Mock::generatePartial('Controller', 'RequestHandlerMockController', array('header'));
25
26 /**
27 * RequestHandlerTestController class
28 *
29 * @package cake
30 * @subpackage cake.tests.cases.libs.controller.components
31 */
32 class RequestHandlerTestController extends Controller {
33
34 /**
35 * name property
36 *
37 * @var string
38 * @access public
39 */
40 var $name = 'RequestHandlerTest';
41
42 /**
43 * uses property
44 *
45 * @var mixed null
46 * @access public
47 */
48 var $uses = null;
49
50 /**
51 * construct method
52 *
53 * @param array $params
54 * @access private
55 * @return void
56 */
57 function __construct($params = array()) {
58 foreach ($params as $key => $val) {
59 $this->{$key} = $val;
60 }
61 parent::__construct();
62 }
63
64 /**
65 * test method for ajax redirection
66 *
67 * @return void
68 */
69 function destination() {
70 $this->viewPath = 'posts';
71 $this->render('index');
72 }
73 /**
74 * test method for ajax redirection + parameter parsing
75 *
76 * @return void
77 */
78 function param_method($one = null, $two = null) {
79 echo "one: $one two: $two";
80 $this->autoRender = false;
81 }
82
83 /**
84 * test method for testing layout rendering when isAjax()
85 *
86 * @return void
87 */
88 function ajax2_layout() {
89 if ($this->autoLayout) {
90 $this->layout = 'ajax2';
91 }
92 $this->destination();
93 }
94 }
95
96 /**
97 * RequestHandlerTestDisabledController class
98 *
99 * @package cake
100 * @subpackage cake.tests.cases.libs.controller.components
101 */
102 class RequestHandlerTestDisabledController extends Controller {
103
104 /**
105 * uses property
106 *
107 * @var mixed null
108 * @access public
109 */
110 var $uses = null;
111
112 /**
113 * construct method
114 *
115 * @param array $params
116 * @access private
117 * @return void
118 */
119 function __construct($params = array()) {
120 foreach ($params as $key => $val) {
121 $this->{$key} = $val;
122 }
123 parent::__construct();
124 }
125
126 /**
127 * beforeFilter method
128 *
129 * @return void
130 * @access public
131 */
132 function beforeFilter() {
133 $this->RequestHandler->enabled = false;
134 }
135 }
136
137 /**
138 * RequestHandlerComponentTest class
139 *
140 * @package cake
141 * @subpackage cake.tests.cases.libs.controller.components
142 */
143 class RequestHandlerComponentTest extends CakeTestCase {
144
145 /**
146 * Controller property
147 *
148 * @var RequestHandlerTestController
149 * @access public
150 */
151 var $Controller;
152
153 /**
154 * RequestHandler property
155 *
156 * @var RequestHandlerComponent
157 * @access public
158 */
159 var $RequestHandler;
160
161 /**
162 * startTest method
163 *
164 * @access public
165 * @return void
166 */
167 function startTest() {
168 $this->_init();
169 }
170
171 /**
172 * init method
173 *
174 * @access protected
175 * @return void
176 */
177 function _init() {
178 $this->Controller = new RequestHandlerTestController(array('components' => array('RequestHandler')));
179 $this->Controller->constructClasses();
180 $this->RequestHandler =& $this->Controller->RequestHandler;
181 }
182
183 /**
184 * endTest method
185 *
186 * @access public
187 * @return void
188 */
189 function endTest() {
190 unset($this->RequestHandler);
191 unset($this->Controller);
192 if (!headers_sent()) {
193 header('Content-type: text/html'); //reset content type.
194 }
195 App::build();
196 }
197
198 /**
199 * testInitializeCallback method
200 *
201 * @access public
202 * @return void
203 */
204 function testInitializeCallback() {
205 $this->assertNull($this->RequestHandler->ext);
206
207 $this->_init();
208 $this->Controller->params['url']['ext'] = 'rss';
209 $this->RequestHandler->initialize($this->Controller);
210 $this->assertEqual($this->RequestHandler->ext, 'rss');
211
212 $settings = array(
213 'ajaxLayout' => 'test_ajax'
214 );
215 $this->RequestHandler->initialize($this->Controller, $settings);
216 $this->assertEqual($this->RequestHandler->ajaxLayout, 'test_ajax');
217 }
218
219 /**
220 * testDisabling method
221 *
222 * @access public
223 * @return void
224 */
225 function testDisabling() {
226 $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
227 $this->_init();
228 $this->Controller->Component->initialize($this->Controller);
229 $this->Controller->beforeFilter();
230 $this->Controller->Component->startup($this->Controller);
231 $this->assertEqual($this->Controller->params, array('isAjax' => true));
232
233 $this->Controller = new RequestHandlerTestDisabledController(array('components' => array('RequestHandler')));
234 $this->Controller->constructClasses();
235 $this->Controller->Component->initialize($this->Controller);
236 $this->Controller->beforeFilter();
237 $this->Controller->Component->startup($this->Controller);
238 $this->assertEqual($this->Controller->params, array());
239 unset($_SERVER['HTTP_X_REQUESTED_WITH']);
240 }
241
242 /**
243 * testAutoResponseType method
244 *
245 * @access public
246 * @return void
247 */
248 function testAutoResponseType() {
249 $this->Controller->ext = '.thtml';
250 $this->Controller->params['url']['ext'] = 'rss';
251 $this->RequestHandler->initialize($this->Controller);
252 $this->RequestHandler->startup($this->Controller);
253 $this->assertEqual($this->Controller->ext, '.ctp');
254 }
255
256
257 /**
258 * testAutoAjaxLayout method
259 *
260 * @access public
261 * @return void
262 */
263 function testAutoAjaxLayout() {
264 $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
265 $this->RequestHandler->startup($this->Controller);
266 $this->assertTrue($this->Controller->layout, $this->RequestHandler->ajaxLayout);
267
268 $this->_init();
269 $this->Controller->params['url']['ext'] = 'js';
270 $this->RequestHandler->initialize($this->Controller);
271 $this->RequestHandler->startup($this->Controller);
272 $this->assertNotEqual($this->Controller->layout, 'ajax');
273
274 unset($_SERVER['HTTP_X_REQUESTED_WITH']);
275 }
276
277 /**
278 * testStartupCallback method
279 *
280 * @access public
281 * @return void
282 */
283 function testStartupCallback() {
284 $_SERVER['REQUEST_METHOD'] = 'PUT';
285 $_SERVER['CONTENT_TYPE'] = 'application/xml';
286 $this->RequestHandler->startup($this->Controller);
287 $this->assertTrue(is_array($this->Controller->data));
288 $this->assertFalse(is_object($this->Controller->data));
289 }
290
291 /**
292 * testStartupCallback with charset.
293 *
294 * @return void
295 */
296 function testStartupCallbackCharset() {
297 $_SERVER['REQUEST_METHOD'] = 'PUT';
298 $_SERVER['CONTENT_TYPE'] = 'application/xml; charset=UTF-8';
299 $this->RequestHandler->startup($this->Controller);
300 $this->assertTrue(is_array($this->Controller->data));
301 $this->assertFalse(is_object($this->Controller->data));
302 }
303
304 /**
305 * testNonAjaxRedirect method
306 *
307 * @access public
308 * @return void
309 */
310 function testNonAjaxRedirect() {
311 $this->RequestHandler->initialize($this->Controller);
312 $this->RequestHandler->startup($this->Controller);
313 $this->assertNull($this->RequestHandler->beforeRedirect($this->Controller, '/'));
314 }
315
316 /**
317 * testRenderAs method
318 *
319 * @access public
320 * @return void
321 */
322 function testRenderAs() {
323 $this->assertFalse(in_array('Xml', $this->Controller->helpers));
324 $this->RequestHandler->renderAs($this->Controller, 'xml');
325 $this->assertTrue(in_array('Xml', $this->Controller->helpers));
326
327 $this->Controller->viewPath = 'request_handler_test\\xml';
328 $this->RequestHandler->renderAs($this->Controller, 'js');
329 $this->assertEqual($this->Controller->viewPath, 'request_handler_test' . DS . 'js');
330 }
331
332 /**
333 * test that respondAs works as expected.
334 *
335 * @return void
336 */
337 function testRespondAs() {
338 $RequestHandler = new NoStopRequestHandler();
339 $RequestHandler->expectAt(0, '_header', array('Content-Type: application/json'));
340 $RequestHandler->expectAt(1, '_header', array('Content-Type: text/xml'));
341
342 $result = $RequestHandler->respondAs('json');
343 $this->assertTrue($result);
344
345 $result = $RequestHandler->respondAs('text/xml');
346 $this->assertTrue($result);
347 }
348
349 /**
350 * test that attachment headers work with respondAs
351 *
352 * @return void
353 */
354 function testRespondAsWithAttachment() {
355 $RequestHandler = new NoStopRequestHandler();
356 $RequestHandler->expectAt(0, '_header', array('Content-Disposition: attachment; filename="myfile.xml"'));
357 $RequestHandler->expectAt(1, '_header', array('Content-Type: text/xml'));
358
359 $result = $RequestHandler->respondAs('xml', array('attachment' => 'myfile.xml'));
360 $this->assertTrue($result);
361 }
362
363 /**
364 * test that calling renderAs() more than once continues to work.
365 *
366 * @link #6466
367 * @return void
368 */
369 function testRenderAsCalledTwice() {
370 $this->RequestHandler->renderAs($this->Controller, 'xml');
371 $this->assertEqual($this->Controller->viewPath, 'request_handler_test' . DS . 'xml');
372 $this->assertEqual($this->Controller->layoutPath, 'xml');
373
374 $this->assertTrue(in_array('Xml', $this->Controller->helpers));
375
376 $this->RequestHandler->renderAs($this->Controller, 'js');
377 $this->assertEqual($this->Controller->viewPath, 'request_handler_test' . DS . 'js');
378 $this->assertEqual($this->Controller->layoutPath, 'js');
379 $this->assertTrue(in_array('Js', $this->Controller->helpers));
380 }
381
382 /**
383 * testRequestClientTypes method
384 *
385 * @access public
386 * @return void
387 */
388 function testRequestClientTypes() {
389 $this->assertFalse($this->RequestHandler->isFlash());
390 $_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
391 $this->assertTrue($this->RequestHandler->isFlash());
392 unset($_SERVER['HTTP_USER_AGENT'], $_SERVER['HTTP_X_REQUESTED_WITH']);
393
394 $this->assertFalse($this->RequestHandler->isAjax());
395 $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
396 $_SERVER['HTTP_X_PROTOTYPE_VERSION'] = '1.5';
397 $this->assertTrue($this->RequestHandler->isAjax());
398 $this->assertEqual($this->RequestHandler->getAjaxVersion(), '1.5');
399
400 unset($_SERVER['HTTP_X_REQUESTED_WITH'], $_SERVER['HTTP_X_PROTOTYPE_VERSION']);
401 $this->assertFalse($this->RequestHandler->isAjax());
402 $this->assertFalse($this->RequestHandler->getAjaxVersion());
403 }
404
405 /**
406 * Tests the detection of various Flash versions
407 *
408 * @access public
409 * @return void
410 */
411 function testFlashDetection() {
412 $_agent = env('HTTP_USER_AGENT');
413 $_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
414 $this->assertTrue($this->RequestHandler->isFlash());
415
416 $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash';
417 $this->assertTrue($this->RequestHandler->isFlash());
418
419 $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash Player 9';
420 $this->assertTrue($this->RequestHandler->isFlash());
421
422 $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash Player 10';
423 $this->assertTrue($this->RequestHandler->isFlash());
424
425 $_SERVER['HTTP_USER_AGENT'] = 'Shock Flash';
426 $this->assertFalse($this->RequestHandler->isFlash());
427
428 $_SERVER['HTTP_USER_AGENT'] = $_agent;
429 }
430
431 /**
432 * testRequestContentTypes method
433 *
434 * @access public
435 * @return void
436 */
437 function testRequestContentTypes() {
438 $_SERVER['REQUEST_METHOD'] = 'GET';
439 $this->assertNull($this->RequestHandler->requestedWith());
440
441 $_SERVER['REQUEST_METHOD'] = 'POST';
442 $_SERVER['CONTENT_TYPE'] = 'application/json';
443 $this->assertEqual($this->RequestHandler->requestedWith(), 'json');
444
445 $result = $this->RequestHandler->requestedWith(array('json', 'xml'));
446 $this->assertEqual($result, 'json');
447
448 $result =$this->RequestHandler->requestedWith(array('rss', 'atom'));
449 $this->assertFalse($result);
450
451 $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
452 $this->_init();
453 $this->assertTrue($this->RequestHandler->isXml());
454 $this->assertFalse($this->RequestHandler->isAtom());
455 $this->assertFalse($this->RequestHandler->isRSS());
456
457 $_SERVER['HTTP_ACCEPT'] = 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
458 $this->_init();
459 $this->assertTrue($this->RequestHandler->isAtom());
460 $this->assertFalse($this->RequestHandler->isRSS());
461
462 $_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
463 $this->_init();
464 $this->assertFalse($this->RequestHandler->isAtom());
465 $this->assertTrue($this->RequestHandler->isRSS());
466
467 $this->assertFalse($this->RequestHandler->isWap());
468 $_SERVER['HTTP_ACCEPT'] = 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*';
469 $this->_init();
470 $this->assertTrue($this->RequestHandler->isWap());
471
472 $_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
473 }
474
475 /**
476 * testResponseContentType method
477 *
478 * @access public
479 * @return void
480 */
481 function testResponseContentType() {
482 $this->assertNull($this->RequestHandler->responseType());
483 $this->assertTrue($this->RequestHandler->respondAs('atom'));
484 $this->assertEqual($this->RequestHandler->responseType(), 'atom');
485 }
486
487 /**
488 * testMobileDeviceDetection method
489 *
490 * @access public
491 * @return void
492 */
493 function testMobileDeviceDetection() {
494 $this->assertFalse($this->RequestHandler->isMobile());
495
496 $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3';
497 $this->assertTrue($this->RequestHandler->isMobile());
498
499 $_SERVER['HTTP_USER_AGENT'] = 'Some imaginary UA';
500 $this->RequestHandler->mobileUA []= 'imaginary';
501 $this->assertTrue($this->RequestHandler->isMobile());
502 array_pop($this->RequestHandler->mobileUA);
503 }
504
505 /**
506 * testRequestProperties method
507 *
508 * @access public
509 * @return void
510 */
511 function testRequestProperties() {
512 $_SERVER['HTTPS'] = 'on';
513 $this->assertTrue($this->RequestHandler->isSSL());
514
515 unset($_SERVER['HTTPS']);
516 $this->assertFalse($this->RequestHandler->isSSL());
517
518 $_ENV['SCRIPT_URI'] = 'https://localhost/';
519 $s = $_SERVER;
520 $_SERVER = array();
521 $this->assertTrue($this->RequestHandler->isSSL());
522 $_SERVER = $s;
523 }
524
525 /**
526 * testRequestMethod method
527 *
528 * @access public
529 * @return void
530 */
531 function testRequestMethod() {
532 $_SERVER['REQUEST_METHOD'] = 'GET';
533 $this->assertTrue($this->RequestHandler->isGet());
534 $this->assertFalse($this->RequestHandler->isPost());
535 $this->assertFalse($this->RequestHandler->isPut());
536 $this->assertFalse($this->RequestHandler->isDelete());
537
538 $_SERVER['REQUEST_METHOD'] = 'POST';
539 $this->assertFalse($this->RequestHandler->isGet());
540 $this->assertTrue($this->RequestHandler->isPost());
541 $this->assertFalse($this->RequestHandler->isPut());
542 $this->assertFalse($this->RequestHandler->isDelete());
543
544 $_SERVER['REQUEST_METHOD'] = 'PUT';
545 $this->assertFalse($this->RequestHandler->isGet());
546 $this->assertFalse($this->RequestHandler->isPost());
547 $this->assertTrue($this->RequestHandler->isPut());
548 $this->assertFalse($this->RequestHandler->isDelete());
549
550 $_SERVER['REQUEST_METHOD'] = 'DELETE';
551 $this->assertFalse($this->RequestHandler->isGet());
552 $this->assertFalse($this->RequestHandler->isPost());
553 $this->assertFalse($this->RequestHandler->isPut());
554 $this->assertTrue($this->RequestHandler->isDelete());
555 }
556
557 /**
558 * testClientContentPreference method
559 *
560 * @access public
561 * @return void
562 */
563 function testClientContentPreference() {
564 $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
565 $this->_init();
566 $this->assertNotEqual($this->RequestHandler->prefers(), 'rss');
567 $this->RequestHandler->ext = 'rss';
568 $this->assertEqual($this->RequestHandler->prefers(), 'rss');
569 $this->assertFalse($this->RequestHandler->prefers('xml'));
570 $this->assertEqual($this->RequestHandler->prefers(array('js', 'xml', 'xhtml')), 'xml');
571 $this->assertTrue($this->RequestHandler->accepts('xml'));
572
573 $_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';
574 $this->_init();
575 $this->assertEqual($this->RequestHandler->prefers(), 'xml');
576 $this->assertEqual($this->RequestHandler->accepts(array('js', 'xml', 'html')), 'xml');
577 $this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo')));
578
579 $_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
580 $this->_init();
581 $this->assertEqual($this->RequestHandler->prefers(), 'html');
582 $this->assertFalse($this->RequestHandler->prefers('rss'));
583 $this->assertFalse($this->RequestHandler->accepts('rss'));
584 }
585
586 /**
587 * testCustomContent method
588 *
589 * @access public
590 * @return void
591 */
592 function testCustomContent() {
593 $_SERVER['HTTP_ACCEPT'] = 'text/x-mobile,text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5';
594 $this->_init();
595 $this->RequestHandler->setContent('mobile', 'text/x-mobile');
596 $this->RequestHandler->startup($this->Controller);
597 $this->assertEqual($this->RequestHandler->prefers(), 'mobile');
598
599 $this->_init();
600 $this->RequestHandler->setContent(array('mobile' => 'text/x-mobile'));
601 $this->RequestHandler->startup($this->Controller);
602 $this->assertEqual($this->RequestHandler->prefers(), 'mobile');
603 }
604
605 /**
606 * testClientProperties method
607 *
608 * @access public
609 * @return void
610 */
611 function testClientProperties() {
612 $_SERVER['HTTP_HOST'] = 'localhost:80';
613 $this->assertEqual($this->RequestHandler->getReferer(), 'localhost');
614 $_SERVER['HTTP_HOST'] = null;
615 $_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org';
616 $this->assertEqual($this->RequestHandler->getReferer(), 'cakephp.org');
617
618 $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.5, 10.0.1.1, proxy.com';
619 $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
620 $_SERVER['REMOTE_ADDR'] = '192.168.1.3';
621 $this->assertEqual($this->RequestHandler->getClientIP(false), '192.168.1.5');
622 $this->assertEqual($this->RequestHandler->getClientIP(), '192.168.1.2');
623
624 unset($_SERVER['HTTP_X_FORWARDED_FOR']);
625 $this->assertEqual($this->RequestHandler->getClientIP(), '192.168.1.2');
626
627 unset($_SERVER['HTTP_CLIENT_IP']);
628 $this->assertEqual($this->RequestHandler->getClientIP(), '192.168.1.3');
629
630 $_SERVER['HTTP_CLIENTADDRESS'] = '10.0.1.2, 10.0.1.1';
631 $this->assertEqual($this->RequestHandler->getClientIP(), '10.0.1.2');
632 }
633
634 /**
635 * test that ajax requests involving redirects trigger requestAction instead.
636 *
637 * @return void
638 */
639 function testAjaxRedirectAsRequestAction() {
640 $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
641 $this->_init();
642 App::build(array(
643 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
644 ), true);
645
646 $this->Controller->RequestHandler = new NoStopRequestHandler($this);
647 $this->Controller->RequestHandler->expectOnce('_stop');
648
649 ob_start();
650 $this->Controller->RequestHandler->beforeRedirect(
651 $this->Controller, array('controller' => 'request_handler_test', 'action' => 'destination')
652 );
653 $result = ob_get_clean();
654 $this->assertPattern('/posts index/', $result, 'RequestAction redirect failed.');
655
656 unset($_SERVER['HTTP_X_REQUESTED_WITH']);
657 App::build();
658 }
659
660 /**
661 * test that ajax requests involving redirects don't force no layout
662 * this would cause the ajax layout to not be rendered.
663 *
664 * @return void
665 */
666 function testAjaxRedirectAsRequestActionStillRenderingLayout() {
667 $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
668 $this->_init();
669 App::build(array(
670 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
671 ), true);
672
673 $this->Controller->RequestHandler = new NoStopRequestHandler($this);
674 $this->Controller->RequestHandler->expectOnce('_stop');
675
676 ob_start();
677 $this->Controller->RequestHandler->beforeRedirect(
678 $this->Controller, array('controller' => 'request_handler_test', 'action' => 'ajax2_layout')
679 );
680 $result = ob_get_clean();
681 $this->assertPattern('/posts index/', $result, 'RequestAction redirect failed.');
682 $this->assertPattern('/Ajax!/', $result, 'Layout was not rendered.');
683
684 unset($_SERVER['HTTP_X_REQUESTED_WITH']);
685 App::build();
686 }
687
688 /**
689 * test that the beforeRedirect callback properly converts
690 * array urls into their correct string ones, and adds base => false so
691 * the correct urls are generated.
692 *
693 * @link http://cakephp.lighthouseapp.com/projects/42648-cakephp-1x/tickets/276
694 * @return void
695 */
696 function testBeforeRedirectCallbackWithArrayUrl() {
697 $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
698
699 Router::setRequestInfo(array(
700 array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'named' => array(), 'form' => array(), 'url' => array('url' => 'accounts/')),
701 array('base' => '/officespace', 'here' => '/officespace/accounts/', 'webroot' => '/officespace/')
702 ));
703
704 $RequestHandler =& new NoStopRequestHandler();
705
706 ob_start();
707 $RequestHandler->beforeRedirect(
708 $this->Controller,
709 array('controller' => 'request_handler_test', 'action' => 'param_method', 'first', 'second')
710 );
711 $result = ob_get_clean();
712 $this->assertEqual($result, 'one: first two: second');
713 }
714
715 /**
716 * assure that beforeRedirect with a status code will correctly set the status header
717 *
718 * @return void
719 */
720 function testBeforeRedirectCallingHeader() {
721 $controller =& new RequestHandlerMockController();
722 $RequestHandler =& new NoStopRequestHandler();
723
724 $controller->expectOnce('header', array('HTTP/1.1 403 Forbidden'));
725
726 ob_start();
727 $RequestHandler->beforeRedirect($controller, 'request_handler_test/param_method/first/second', 403);
728 $result = ob_get_clean();
729 }
730
731 }