Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 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 sets $this->ext when jQuery sends its wonky-ish headers
202
 * and the application is configured to handle multiple extensions
203
 *
204
 * @return void
205
 */
206
	public function testInitializeContentTypeWithjQueryAcceptAndMultiplesExtensions() {
207
		$_SERVER['HTTP_ACCEPT'] = 'application/json, application/javascript, */*; q=0.01';
208
		$this->assertNull($this->RequestHandler->ext);
209
		Router::parseExtensions('rss', 'json');
210
 
211
		$this->RequestHandler->initialize($this->Controller);
212
		$this->assertEquals('json', $this->RequestHandler->ext);
213
	}
214
 
215
/**
216
 * Test that RequestHandler does not set $this->ext when multiple accepts are sent.
217
 *
218
 * @return void
219
 */
220
	public function testInitializeNoContentTypeWithSingleAccept() {
221
		$_SERVER['HTTP_ACCEPT'] = 'application/json, text/html, */*; q=0.01';
222
		$this->assertNull($this->RequestHandler->ext);
223
		Router::parseExtensions('json');
224
 
225
		$this->RequestHandler->initialize($this->Controller);
226
		$this->assertNull($this->RequestHandler->ext);
227
	}
228
 
229
/**
230
 * Test that ext is set to the first listed extension with multiple accepted
231
 * content types.
232
 * Having multiple types accepted with same weight, means the client lets the
233
 * server choose the returned content type.
234
 *
235
 * @return void
236
 */
237
	public function testInitializeNoContentTypeWithMultipleAcceptedTypes() {
238
		$_SERVER['HTTP_ACCEPT'] = 'application/json, application/javascript, application/xml, */*; q=0.01';
239
		$this->assertNull($this->RequestHandler->ext);
240
		Router::parseExtensions('xml', 'json');
241
 
242
		$this->RequestHandler->initialize($this->Controller);
243
		$this->assertEquals('xml', $this->RequestHandler->ext);
244
 
245
		$this->RequestHandler->ext = null;
246
		Router::setExtensions(array('json', 'xml'), false);
247
 
248
		$this->RequestHandler->initialize($this->Controller);
249
		$this->assertEquals('json', $this->RequestHandler->ext);
250
	}
251
 
252
/**
253
 * Test that ext is set to type with highest weight
254
 *
255
 * @return void
256
 */
257
	public function testInitializeContentTypeWithMultipleAcceptedTypes() {
258
		$_SERVER['HTTP_ACCEPT'] = 'text/csv;q=1.0, application/json;q=0.8, application/xml;q=0.7';
259
		$this->assertNull($this->RequestHandler->ext);
260
		Router::parseExtensions('xml', 'json');
261
 
262
		$this->RequestHandler->initialize($this->Controller);
263
		$this->assertEquals('json', $this->RequestHandler->ext);
264
	}
265
 
266
/**
267
 * Test that ext is not set with confusing android accepts headers.
268
 *
269
 * @return void
270
 */
271
	public function testInitializeAmbiguousAndroidAccepts() {
272
		$_SERVER['HTTP_ACCEPT'] = 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
273
		$this->assertNull($this->RequestHandler->ext);
274
		Router::parseExtensions('html', 'xml');
275
 
276
		$this->RequestHandler->initialize($this->Controller);
277
		$this->assertNull($this->RequestHandler->ext);
278
	}
279
 
280
/**
281
 * Test that the headers sent by firefox are not treated as XML requests.
282
 *
283
 * @return void
284
 */
285
	public function testInititalizeFirefoxHeaderNotXml() {
286
		$_SERVER['HTTP_ACCEPT'] = 'text/html,application/xhtml+xml,application/xml;image/png,image/jpeg,image/*;q=0.9,*/*;q=0.8';
287
		Router::parseExtensions('xml', 'json');
288
 
289
		$this->RequestHandler->initialize($this->Controller);
290
		$this->assertNull($this->RequestHandler->ext);
291
	}
292
 
293
/**
294
 * Test that a type mismatch doesn't incorrectly set the ext
295
 *
296
 * @return void
297
 */
298
	public function testInitializeContentTypeAndExtensionMismatch() {
299
		$this->assertNull($this->RequestHandler->ext);
300
		$extensions = Router::extensions();
301
		Router::parseExtensions('xml');
302
 
303
		$this->Controller->request = $this->getMock('CakeRequest');
304
		$this->Controller->request->expects($this->any())
305
			->method('accepts')
306
			->will($this->returnValue(array('application/json')));
307
 
308
		$this->RequestHandler->initialize($this->Controller);
309
		$this->assertNull($this->RequestHandler->ext);
310
 
311
		call_user_func_array(array('Router', 'parseExtensions'), $extensions);
312
	}
313
 
314
/**
315
 * testViewClassMap method
316
 *
317
 * @return void
318
 */
319
	public function testViewClassMap() {
320
		$this->RequestHandler->settings = array('viewClassMap' => array('json' => 'CustomJson'));
321
		$this->RequestHandler->initialize($this->Controller);
322
		$result = $this->RequestHandler->viewClassMap();
323
		$expected = array(
324
			'json' => 'CustomJson',
325
			'xml' => 'Xml'
326
		);
327
		$this->assertEquals($expected, $result);
328
 
329
		$result = $this->RequestHandler->viewClassMap('xls', 'Excel.Excel');
330
		$expected = array(
331
			'json' => 'CustomJson',
332
			'xml' => 'Xml',
333
			'xls' => 'Excel.Excel'
334
		);
335
		$this->assertEquals($expected, $result);
336
 
337
		$this->RequestHandler->renderAs($this->Controller, 'json');
338
		$this->assertEquals('CustomJson', $this->Controller->viewClass);
339
	}
340
 
341
/**
342
 * testDisabling method
343
 *
344
 * @return void
345
 */
346
	public function testDisabling() {
347
		$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
348
		$this->_init();
349
		$this->RequestHandler->initialize($this->Controller);
350
		$this->Controller->beforeFilter();
351
		$this->RequestHandler->startup($this->Controller);
352
		$this->assertEquals(true, $this->Controller->params['isAjax']);
353
	}
354
 
355
/**
356
 * testAutoAjaxLayout method
357
 *
358
 * @return void
359
 */
360
	public function testAutoAjaxLayout() {
361
		$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
362
		$this->RequestHandler->startup($this->Controller);
363
		$this->assertEquals($this->Controller->layout, $this->RequestHandler->ajaxLayout);
364
 
365
		$this->_init();
366
		$this->Controller->request->params['ext'] = 'js';
367
		$this->RequestHandler->initialize($this->Controller);
368
		$this->RequestHandler->startup($this->Controller);
369
		$this->assertNotEquals('ajax', $this->Controller->layout);
370
 
371
		unset($_SERVER['HTTP_X_REQUESTED_WITH']);
372
	}
373
 
374
/**
375
 * testStartupCallback method
376
 *
377
 * @return void
378
 */
379
	public function testStartupCallback() {
380
		$_SERVER['REQUEST_METHOD'] = 'PUT';
381
		$_SERVER['CONTENT_TYPE'] = 'application/xml';
382
		$this->Controller->request = $this->getMock('CakeRequest', array('_readInput'));
383
		$this->RequestHandler->startup($this->Controller);
384
		$this->assertTrue(is_array($this->Controller->data));
385
		$this->assertFalse(is_object($this->Controller->data));
386
	}
387
 
388
/**
389
 * testStartupCallback with charset.
390
 *
391
 * @return void
392
 */
393
	public function testStartupCallbackCharset() {
394
		$_SERVER['REQUEST_METHOD'] = 'PUT';
395
		$_SERVER['CONTENT_TYPE'] = 'application/xml; charset=UTF-8';
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
 * Test mapping a new type and having startup process it.
404
 *
405
 * @return void
406
 */
407
	public function testStartupCustomTypeProcess() {
408
		if (!function_exists('str_getcsv')) {
409
			$this->markTestSkipped('Need "str_getcsv" for this test.');
410
		}
411
		$_SERVER['REQUEST_METHOD'] = 'POST';
412
		$_SERVER['CONTENT_TYPE'] = 'text/csv';
413
		$this->Controller->request = $this->getMock('CakeRequest', array('_readInput'));
414
		$this->Controller->request->expects($this->once())
415
			->method('_readInput')
416
			->will($this->returnValue('"A","csv","string"'));
417
		$this->RequestHandler->addInputType('csv', array('str_getcsv'));
418
		$this->RequestHandler->startup($this->Controller);
419
		$expected = array(
420
			'A', 'csv', 'string'
421
		);
422
		$this->assertEquals($expected, $this->Controller->request->data);
423
	}
424
 
425
/**
426
 * testNonAjaxRedirect method
427
 *
428
 * @return void
429
 */
430
	public function testNonAjaxRedirect() {
431
		$this->RequestHandler->initialize($this->Controller);
432
		$this->RequestHandler->startup($this->Controller);
433
		$this->assertNull($this->RequestHandler->beforeRedirect($this->Controller, '/'));
434
	}
435
 
436
/**
437
 * test that redirects with ajax and no URL don't do anything.
438
 *
439
 * @return void
440
 */
441
	public function testAjaxRedirectWithNoUrl() {
442
		$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
443
		$this->Controller->response = $this->getMock('CakeResponse');
444
 
445
		$this->Controller->response->expects($this->never())
446
			->method('body');
447
 
448
		$this->RequestHandler->initialize($this->Controller);
449
		$this->RequestHandler->startup($this->Controller);
450
		$this->assertNull($this->RequestHandler->beforeRedirect($this->Controller, null));
451
	}
452
 
453
/**
454
 * testRenderAs method
455
 *
456
 * @return void
457
 */
458
	public function testRenderAs() {
459
		$this->assertFalse(in_array('Rss', $this->Controller->helpers));
460
		$this->RequestHandler->renderAs($this->Controller, 'rss');
461
		$this->assertTrue(in_array('Rss', $this->Controller->helpers));
462
 
463
		$this->Controller->viewPath = 'request_handler_test\\rss';
464
		$this->RequestHandler->renderAs($this->Controller, 'js');
465
		$this->assertEquals('request_handler_test' . DS . 'js', $this->Controller->viewPath);
466
	}
467
 
468
/**
469
 * test that attachment headers work with renderAs
470
 *
471
 * @return void
472
 */
473
	public function testRenderAsWithAttachment() {
474
		$this->RequestHandler->request = $this->getMock('CakeRequest');
475
		$this->RequestHandler->request->expects($this->any())
476
			->method('parseAccept')
477
			->will($this->returnValue(array('1.0' => array('application/xml'))));
478
 
479
		$this->RequestHandler->response = $this->getMock('CakeResponse', array('type', 'download', 'charset'));
480
		$this->RequestHandler->response->expects($this->at(0))
481
			->method('type')
482
			->with('application/xml');
483
		$this->RequestHandler->response->expects($this->at(1))
484
			->method('charset')
485
			->with('UTF-8');
486
		$this->RequestHandler->response->expects($this->at(2))
487
			->method('download')
488
			->with('myfile.xml');
489
 
490
		$this->RequestHandler->renderAs($this->Controller, 'xml', array('attachment' => 'myfile.xml'));
491
 
492
		$this->assertEquals('Xml', $this->Controller->viewClass);
493
	}
494
 
495
/**
496
 * test that respondAs works as expected.
497
 *
498
 * @return void
499
 */
500
	public function testRespondAs() {
501
		$this->RequestHandler->response = $this->getMock('CakeResponse', array('type'));
502
		$this->RequestHandler->response->expects($this->at(0))->method('type')
503
			->with('application/json');
504
		$this->RequestHandler->response->expects($this->at(1))->method('type')
505
			->with('text/xml');
506
 
507
		$result = $this->RequestHandler->respondAs('json');
508
		$this->assertTrue($result);
509
		$result = $this->RequestHandler->respondAs('text/xml');
510
		$this->assertTrue($result);
511
	}
512
 
513
/**
514
 * test that attachment headers work with respondAs
515
 *
516
 * @return void
517
 */
518
	public function testRespondAsWithAttachment() {
519
		$this->RequestHandler = $this->getMock(
520
			'RequestHandlerComponent',
521
			array('_header'),
522
			array(&$this->Controller->Components)
523
		);
524
		$this->RequestHandler->response = $this->getMock('CakeResponse', array('type', 'download'));
525
		$this->RequestHandler->request = $this->getMock('CakeRequest');
526
 
527
		$this->RequestHandler->request->expects($this->once())
528
			->method('parseAccept')
529
			->will($this->returnValue(array('1.0' => array('application/xml'))));
530
 
531
		$this->RequestHandler->response->expects($this->once())->method('download')
532
			->with('myfile.xml');
533
		$this->RequestHandler->response->expects($this->once())->method('type')
534
			->with('application/xml');
535
 
536
		$result = $this->RequestHandler->respondAs('xml', array('attachment' => 'myfile.xml'));
537
		$this->assertTrue($result);
538
	}
539
 
540
/**
541
 * test that calling renderAs() more than once continues to work.
542
 *
543
 * @link #6466
544
 * @return void
545
 */
546
	public function testRenderAsCalledTwice() {
547
		$this->RequestHandler->renderAs($this->Controller, 'print');
548
		$this->assertEquals('RequestHandlerTest' . DS . 'print', $this->Controller->viewPath);
549
		$this->assertEquals('print', $this->Controller->layoutPath);
550
 
551
		$this->RequestHandler->renderAs($this->Controller, 'js');
552
		$this->assertEquals('RequestHandlerTest' . DS . 'js', $this->Controller->viewPath);
553
		$this->assertEquals('js', $this->Controller->layoutPath);
554
		$this->assertTrue(in_array('Js', $this->Controller->helpers));
555
	}
556
 
557
/**
558
 * testRequestClientTypes method
559
 *
560
 * @return void
561
 */
562
	public function testRequestClientTypes() {
563
		$_SERVER['HTTP_X_PROTOTYPE_VERSION'] = '1.5';
564
		$this->assertEquals('1.5', $this->RequestHandler->getAjaxVersion());
565
 
566
		unset($_SERVER['HTTP_X_REQUESTED_WITH'], $_SERVER['HTTP_X_PROTOTYPE_VERSION']);
567
		$this->assertFalse($this->RequestHandler->getAjaxVersion());
568
	}
569
 
570
/**
571
 * Tests the detection of various Flash versions
572
 *
573
 * @return void
574
 */
575
	public function testFlashDetection() {
576
		$request = $this->getMock('CakeRequest');
577
		$request->expects($this->once())->method('is')
578
			->with('flash')
579
			->will($this->returnValue(true));
580
 
581
		$this->RequestHandler->request = $request;
582
		$this->assertTrue($this->RequestHandler->isFlash());
583
	}
584
 
585
/**
586
 * testRequestContentTypes method
587
 *
588
 * @return void
589
 */
590
	public function testRequestContentTypes() {
591
		$_SERVER['REQUEST_METHOD'] = 'GET';
592
		$this->assertNull($this->RequestHandler->requestedWith());
593
 
594
		$_SERVER['REQUEST_METHOD'] = 'POST';
595
		$_SERVER['CONTENT_TYPE'] = 'application/json';
596
		$this->assertEquals('json', $this->RequestHandler->requestedWith());
597
 
598
		$result = $this->RequestHandler->requestedWith(array('json', 'xml'));
599
		$this->assertEquals('json', $result);
600
 
601
		$result = $this->RequestHandler->requestedWith(array('rss', 'atom'));
602
		$this->assertFalse($result);
603
 
604
		$_SERVER['REQUEST_METHOD'] = 'DELETE';
605
		$this->assertEquals('json', $this->RequestHandler->requestedWith());
606
 
607
		$_SERVER['REQUEST_METHOD'] = 'POST';
608
		unset($_SERVER['CONTENT_TYPE']);
609
		$_SERVER['HTTP_CONTENT_TYPE'] = 'application/json';
610
 
611
		$result = $this->RequestHandler->requestedWith(array('json', 'xml'));
612
		$this->assertEquals('json', $result);
613
 
614
		$result = $this->RequestHandler->requestedWith(array('rss', 'atom'));
615
		$this->assertFalse($result);
616
 
617
		$_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
618
		$this->assertTrue($this->RequestHandler->isXml());
619
		$this->assertFalse($this->RequestHandler->isAtom());
620
		$this->assertFalse($this->RequestHandler->isRSS());
621
 
622
		$_SERVER['HTTP_ACCEPT'] = 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
623
		$this->assertTrue($this->RequestHandler->isAtom());
624
		$this->assertFalse($this->RequestHandler->isRSS());
625
 
626
		$_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
627
		$this->assertFalse($this->RequestHandler->isAtom());
628
		$this->assertTrue($this->RequestHandler->isRSS());
629
 
630
		$this->assertFalse($this->RequestHandler->isWap());
631
		$_SERVER['HTTP_ACCEPT'] = 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*';
632
		$this->assertTrue($this->RequestHandler->isWap());
633
 
634
		$_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
635
	}
636
 
637
/**
638
 * testResponseContentType method
639
 *
640
 * @return void
641
 */
642
	public function testResponseContentType() {
643
		$this->assertEquals('html', $this->RequestHandler->responseType());
644
		$this->assertTrue($this->RequestHandler->respondAs('atom'));
645
		$this->assertEquals('atom', $this->RequestHandler->responseType());
646
	}
647
 
648
/**
649
 * testMobileDeviceDetection method
650
 *
651
 * @return void
652
 */
653
	public function testMobileDeviceDetection() {
654
		$request = $this->getMock('CakeRequest');
655
		$request->expects($this->once())->method('is')
656
			->with('mobile')
657
			->will($this->returnValue(true));
658
 
659
		$this->RequestHandler->request = $request;
660
		$this->assertTrue($this->RequestHandler->isMobile());
661
	}
662
 
663
/**
664
 * testRequestProperties method
665
 *
666
 * @return void
667
 */
668
	public function testRequestProperties() {
669
		$request = $this->getMock('CakeRequest');
670
		$request->expects($this->once())->method('is')
671
			->with('ssl')
672
			->will($this->returnValue(true));
673
 
674
		$this->RequestHandler->request = $request;
675
		$this->assertTrue($this->RequestHandler->isSsl());
676
	}
677
 
678
/**
679
 * testRequestMethod method
680
 *
681
 * @return void
682
 */
683
	public function testRequestMethod() {
684
		$request = $this->getMock('CakeRequest');
685
		$request->expects($this->at(0))->method('is')
686
			->with('get')
687
			->will($this->returnValue(true));
688
 
689
		$request->expects($this->at(1))->method('is')
690
			->with('post')
691
			->will($this->returnValue(false));
692
 
693
		$request->expects($this->at(2))->method('is')
694
			->with('delete')
695
			->will($this->returnValue(true));
696
 
697
		$request->expects($this->at(3))->method('is')
698
			->with('put')
699
			->will($this->returnValue(false));
700
 
701
		$this->RequestHandler->request = $request;
702
		$this->assertTrue($this->RequestHandler->isGet());
703
		$this->assertFalse($this->RequestHandler->isPost());
704
		$this->assertTrue($this->RequestHandler->isDelete());
705
		$this->assertFalse($this->RequestHandler->isPut());
706
	}
707
 
708
/**
709
 * test that map alias converts aliases to content types.
710
 *
711
 * @return void
712
 */
713
	public function testMapAlias() {
714
		$result = $this->RequestHandler->mapAlias('xml');
715
		$this->assertEquals('application/xml', $result);
716
 
717
		$result = $this->RequestHandler->mapAlias('text/html');
718
		$this->assertNull($result);
719
 
720
		$result = $this->RequestHandler->mapAlias('wap');
721
		$this->assertEquals('text/vnd.wap.wml', $result);
722
 
723
		$result = $this->RequestHandler->mapAlias(array('xml', 'js', 'json'));
724
		$expected = array('application/xml', 'application/javascript', 'application/json');
725
		$this->assertEquals($expected, $result);
726
	}
727
 
728
/**
729
 * test accepts() on the component
730
 *
731
 * @return void
732
 */
733
	public function testAccepts() {
734
		$_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';
735
		$this->assertTrue($this->RequestHandler->accepts(array('js', 'xml', 'html')));
736
		$this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo')));
737
 
738
		$_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
739
		$this->assertFalse($this->RequestHandler->accepts('rss'));
740
	}
741
 
742
/**
743
 * test accepts and prefers methods.
744
 *
745
 * @return void
746
 */
747
	public function testPrefers() {
748
		$_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
749
		$this->assertNotEquals('rss', $this->RequestHandler->prefers());
750
		$this->RequestHandler->ext = 'rss';
751
		$this->assertEquals('rss', $this->RequestHandler->prefers());
752
		$this->assertFalse($this->RequestHandler->prefers('xml'));
753
		$this->assertEquals('xml', $this->RequestHandler->prefers(array('js', 'xml', 'xhtml')));
754
		$this->assertFalse($this->RequestHandler->prefers(array('red', 'blue')));
755
		$this->assertEquals('xhtml', $this->RequestHandler->prefers(array('js', 'json', 'xhtml')));
756
		$this->assertTrue($this->RequestHandler->prefers(array('rss')), 'Should return true if input matches ext.');
757
		$this->assertFalse($this->RequestHandler->prefers(array('html')), 'No match with ext, return false.');
758
 
759
		$_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';
760
		$this->_init();
761
		$this->assertEquals('xml', $this->RequestHandler->prefers());
762
 
763
		$_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
764
		$this->assertEquals('html', $this->RequestHandler->prefers());
765
		$this->assertFalse($this->RequestHandler->prefers('rss'));
766
	}
767
 
768
/**
769
 * testCustomContent method
770
 *
771
 * @return void
772
 */
773
	public function testCustomContent() {
774
		$_SERVER['HTTP_ACCEPT'] = 'text/x-mobile,text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5';
775
		$this->RequestHandler->setContent('mobile', 'text/x-mobile');
776
		$this->RequestHandler->startup($this->Controller);
777
		$this->assertEquals('mobile', $this->RequestHandler->prefers());
778
	}
779
 
780
/**
781
 * testClientProperties method
782
 *
783
 * @return void
784
 */
785
	public function testClientProperties() {
786
		$request = $this->getMock('CakeRequest');
787
		$request->expects($this->once())->method('referer');
788
		$request->expects($this->once())->method('clientIp')->will($this->returnValue(false));
789
 
790
		$this->RequestHandler->request = $request;
791
 
792
		$this->RequestHandler->getReferer();
793
		$this->RequestHandler->getClientIP(false);
794
	}
795
 
796
/**
797
 * test that ajax requests involving redirects trigger requestAction instead.
798
 *
799
 * @return void
800
 */
801
	public function testAjaxRedirectAsRequestAction() {
802
		App::build(array(
803
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
804
		), App::RESET);
805
 
806
		$this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
807
		$this->Controller->request = $this->getMock('CakeRequest');
808
		$this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
809
		$this->Controller->RequestHandler->request = $this->Controller->request;
810
		$this->Controller->RequestHandler->response = $this->Controller->response;
811
		$this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
812
		$this->Controller->RequestHandler->expects($this->once())->method('_stop');
813
 
814
		ob_start();
815
		$this->Controller->RequestHandler->beforeRedirect(
816
			$this->Controller, array('controller' => 'request_handler_test', 'action' => 'destination')
817
		);
818
		$result = ob_get_clean();
819
		$this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
820
 
821
		App::build();
822
	}
823
 
824
/**
825
 * test that ajax requests involving redirects don't force no layout
826
 * this would cause the ajax layout to not be rendered.
827
 *
828
 * @return void
829
 */
830
	public function testAjaxRedirectAsRequestActionStillRenderingLayout() {
831
		App::build(array(
832
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
833
		), App::RESET);
834
 
835
		$this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
836
		$this->Controller->request = $this->getMock('CakeRequest');
837
		$this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
838
		$this->Controller->RequestHandler->request = $this->Controller->request;
839
		$this->Controller->RequestHandler->response = $this->Controller->response;
840
		$this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
841
		$this->Controller->RequestHandler->expects($this->once())->method('_stop');
842
 
843
		ob_start();
844
		$this->Controller->RequestHandler->beforeRedirect(
845
			$this->Controller, array('controller' => 'request_handler_test', 'action' => 'ajax2_layout')
846
		);
847
		$result = ob_get_clean();
848
		$this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
849
		$this->assertRegExp('/Ajax!/', $result, 'Layout was not rendered.');
850
 
851
		App::build();
852
	}
853
 
854
/**
855
 * test that the beforeRedirect callback properly converts
856
 * array URLs into their correct string ones, and adds base => false so
857
 * the correct URLs are generated.
858
 *
859
 * @link https://cakephp.lighthouseapp.com/projects/42648-cakephp-1x/tickets/276
860
 * @return void
861
 */
862
	public function testBeforeRedirectCallbackWithArrayUrl() {
863
		$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
864
 
865
		Router::setRequestInfo(array(
866
			array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'named' => array(), 'form' => array(), 'url' => array('url' => 'accounts/')),
867
			array('base' => '/officespace', 'here' => '/officespace/accounts/', 'webroot' => '/officespace/')
868
		));
869
 
870
		$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
871
		$RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader'));
872
		$RequestHandler->request = new CakeRequest('posts/index');
873
		$RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader'));
874
 
875
		ob_start();
876
		$RequestHandler->beforeRedirect(
877
			$this->Controller,
878
			array('controller' => 'request_handler_test', 'action' => 'param_method', 'first', 'second')
879
		);
880
		$result = ob_get_clean();
881
		$this->assertEquals('one: first two: second', $result);
882
	}
883
 
884
/**
885
 * assure that beforeRedirect with a status code will correctly set the status header
886
 *
887
 * @return void
888
 */
889
	public function testBeforeRedirectCallingHeader() {
890
		$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
891
 
892
		$controller = $this->getMock('Controller', array('header'));
893
		$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
894
		$RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader', 'statusCode'));
895
		$RequestHandler->request = $this->getMock('CakeRequest');
896
		$RequestHandler->request->expects($this->once())->method('is')
897
			->with('ajax')
898
			->will($this->returnValue(true));
899
 
900
		$RequestHandler->response->expects($this->once())->method('statusCode')->with(403);
901
 
902
		ob_start();
903
		$RequestHandler->beforeRedirect($controller, 'request_handler_test/param_method/first/second', 403);
904
		ob_get_clean();
905
	}
906
 
907
/**
908
 * @expectedException CakeException
909
 * @return void
910
 */
911
	public function testAddInputTypeException() {
912
		$this->RequestHandler->addInputType('csv', array('I am not callable'));
913
	}
914
 
915
/**
916
 * Test checkNotModified method
917
 *
918
 * @return void
919
 */
920
	public function testCheckNotModifiedByEtagStar() {
921
		$_SERVER['HTTP_IF_NONE_MATCH'] = '*';
922
		$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
923
		$RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
924
		$RequestHandler->response->etag('something');
925
		$RequestHandler->response->expects($this->once())->method('notModified');
926
		$this->assertFalse($RequestHandler->beforeRender($this->Controller));
927
	}
928
 
929
/**
930
 * Test checkNotModified method
931
 *
932
 * @return void
933
 */
934
	public function testCheckNotModifiedByEtagExact() {
935
		$_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
936
		$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
937
		$RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
938
		$RequestHandler->response->etag('something', true);
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 testCheckNotModifiedByEtagAndTime() {
949
		$_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
950
		$_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
951
		$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
952
		$RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
953
		$RequestHandler->response->etag('something', true);
954
		$RequestHandler->response->modified('2012-01-01 00:00:00');
955
		$RequestHandler->response->expects($this->once())->method('notModified');
956
		$this->assertFalse($RequestHandler->beforeRender($this->Controller));
957
	}
958
 
959
/**
960
 * Test checkNotModified method
961
 *
962
 * @return void
963
 */
964
	public function testCheckNotModifiedNoInfo() {
965
		$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
966
		$RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
967
		$RequestHandler->response->expects($this->never())->method('notModified');
968
		$this->assertNull($RequestHandler->beforeRender($this->Controller));
969
	}
970
}