Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 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'] = 'POST';
605
		unset($_SERVER['CONTENT_TYPE']);
606
		$_SERVER['HTTP_CONTENT_TYPE'] = 'application/json';
607
 
608
		$result = $this->RequestHandler->requestedWith(array('json', 'xml'));
609
		$this->assertEquals('json', $result);
610
 
611
		$result = $this->RequestHandler->requestedWith(array('rss', 'atom'));
612
		$this->assertFalse($result);
613
 
614
		$_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
615
		$this->assertTrue($this->RequestHandler->isXml());
616
		$this->assertFalse($this->RequestHandler->isAtom());
617
		$this->assertFalse($this->RequestHandler->isRSS());
618
 
619
		$_SERVER['HTTP_ACCEPT'] = 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
620
		$this->assertTrue($this->RequestHandler->isAtom());
621
		$this->assertFalse($this->RequestHandler->isRSS());
622
 
623
		$_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
624
		$this->assertFalse($this->RequestHandler->isAtom());
625
		$this->assertTrue($this->RequestHandler->isRSS());
626
 
627
		$this->assertFalse($this->RequestHandler->isWap());
628
		$_SERVER['HTTP_ACCEPT'] = 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*';
629
		$this->assertTrue($this->RequestHandler->isWap());
630
 
631
		$_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
632
	}
633
 
634
/**
635
 * testResponseContentType method
636
 *
637
 * @return void
638
 */
639
	public function testResponseContentType() {
640
		$this->assertEquals('html', $this->RequestHandler->responseType());
641
		$this->assertTrue($this->RequestHandler->respondAs('atom'));
642
		$this->assertEquals('atom', $this->RequestHandler->responseType());
643
	}
644
 
645
/**
646
 * testMobileDeviceDetection method
647
 *
648
 * @return void
649
 */
650
	public function testMobileDeviceDetection() {
651
		$request = $this->getMock('CakeRequest');
652
		$request->expects($this->once())->method('is')
653
			->with('mobile')
654
			->will($this->returnValue(true));
655
 
656
		$this->RequestHandler->request = $request;
657
		$this->assertTrue($this->RequestHandler->isMobile());
658
	}
659
 
660
/**
661
 * testRequestProperties method
662
 *
663
 * @return void
664
 */
665
	public function testRequestProperties() {
666
		$request = $this->getMock('CakeRequest');
667
		$request->expects($this->once())->method('is')
668
			->with('ssl')
669
			->will($this->returnValue(true));
670
 
671
		$this->RequestHandler->request = $request;
672
		$this->assertTrue($this->RequestHandler->isSsl());
673
	}
674
 
675
/**
676
 * testRequestMethod method
677
 *
678
 * @return void
679
 */
680
	public function testRequestMethod() {
681
		$request = $this->getMock('CakeRequest');
682
		$request->expects($this->at(0))->method('is')
683
			->with('get')
684
			->will($this->returnValue(true));
685
 
686
		$request->expects($this->at(1))->method('is')
687
			->with('post')
688
			->will($this->returnValue(false));
689
 
690
		$request->expects($this->at(2))->method('is')
691
			->with('delete')
692
			->will($this->returnValue(true));
693
 
694
		$request->expects($this->at(3))->method('is')
695
			->with('put')
696
			->will($this->returnValue(false));
697
 
698
		$this->RequestHandler->request = $request;
699
		$this->assertTrue($this->RequestHandler->isGet());
700
		$this->assertFalse($this->RequestHandler->isPost());
701
		$this->assertTrue($this->RequestHandler->isDelete());
702
		$this->assertFalse($this->RequestHandler->isPut());
703
	}
704
 
705
/**
706
 * test that map alias converts aliases to content types.
707
 *
708
 * @return void
709
 */
710
	public function testMapAlias() {
711
		$result = $this->RequestHandler->mapAlias('xml');
712
		$this->assertEquals('application/xml', $result);
713
 
714
		$result = $this->RequestHandler->mapAlias('text/html');
715
		$this->assertNull($result);
716
 
717
		$result = $this->RequestHandler->mapAlias('wap');
718
		$this->assertEquals('text/vnd.wap.wml', $result);
719
 
720
		$result = $this->RequestHandler->mapAlias(array('xml', 'js', 'json'));
721
		$expected = array('application/xml', 'application/javascript', 'application/json');
722
		$this->assertEquals($expected, $result);
723
	}
724
 
725
/**
726
 * test accepts() on the component
727
 *
728
 * @return void
729
 */
730
	public function testAccepts() {
731
		$_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';
732
		$this->assertTrue($this->RequestHandler->accepts(array('js', 'xml', 'html')));
733
		$this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo')));
734
 
735
		$_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
736
		$this->assertFalse($this->RequestHandler->accepts('rss'));
737
	}
738
 
739
/**
740
 * test accepts and prefers methods.
741
 *
742
 * @return void
743
 */
744
	public function testPrefers() {
745
		$_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
746
		$this->assertNotEquals('rss', $this->RequestHandler->prefers());
747
		$this->RequestHandler->ext = 'rss';
748
		$this->assertEquals('rss', $this->RequestHandler->prefers());
749
		$this->assertFalse($this->RequestHandler->prefers('xml'));
750
		$this->assertEquals('xml', $this->RequestHandler->prefers(array('js', 'xml', 'xhtml')));
751
		$this->assertFalse($this->RequestHandler->prefers(array('red', 'blue')));
752
		$this->assertEquals('xhtml', $this->RequestHandler->prefers(array('js', 'json', 'xhtml')));
753
		$this->assertTrue($this->RequestHandler->prefers(array('rss')), 'Should return true if input matches ext.');
754
		$this->assertFalse($this->RequestHandler->prefers(array('html')), 'No match with ext, return false.');
755
 
756
		$_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';
757
		$this->_init();
758
		$this->assertEquals('xml', $this->RequestHandler->prefers());
759
 
760
		$_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
761
		$this->assertEquals('html', $this->RequestHandler->prefers());
762
		$this->assertFalse($this->RequestHandler->prefers('rss'));
763
	}
764
 
765
/**
766
 * testCustomContent method
767
 *
768
 * @return void
769
 */
770
	public function testCustomContent() {
771
		$_SERVER['HTTP_ACCEPT'] = 'text/x-mobile,text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5';
772
		$this->RequestHandler->setContent('mobile', 'text/x-mobile');
773
		$this->RequestHandler->startup($this->Controller);
774
		$this->assertEquals('mobile', $this->RequestHandler->prefers());
775
	}
776
 
777
/**
778
 * testClientProperties method
779
 *
780
 * @return void
781
 */
782
	public function testClientProperties() {
783
		$request = $this->getMock('CakeRequest');
784
		$request->expects($this->once())->method('referer');
785
		$request->expects($this->once())->method('clientIp')->will($this->returnValue(false));
786
 
787
		$this->RequestHandler->request = $request;
788
 
789
		$this->RequestHandler->getReferer();
790
		$this->RequestHandler->getClientIP(false);
791
	}
792
 
793
/**
794
 * test that ajax requests involving redirects trigger requestAction instead.
795
 *
796
 * @return void
797
 */
798
	public function testAjaxRedirectAsRequestAction() {
799
		App::build(array(
800
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
801
		), App::RESET);
802
 
803
		$this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
804
		$this->Controller->request = $this->getMock('CakeRequest');
805
		$this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
806
		$this->Controller->RequestHandler->request = $this->Controller->request;
807
		$this->Controller->RequestHandler->response = $this->Controller->response;
808
		$this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
809
		$this->Controller->RequestHandler->expects($this->once())->method('_stop');
810
 
811
		ob_start();
812
		$this->Controller->RequestHandler->beforeRedirect(
813
			$this->Controller, array('controller' => 'request_handler_test', 'action' => 'destination')
814
		);
815
		$result = ob_get_clean();
816
		$this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
817
 
818
		App::build();
819
	}
820
 
821
/**
822
 * test that ajax requests involving redirects don't force no layout
823
 * this would cause the ajax layout to not be rendered.
824
 *
825
 * @return void
826
 */
827
	public function testAjaxRedirectAsRequestActionStillRenderingLayout() {
828
		App::build(array(
829
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
830
		), App::RESET);
831
 
832
		$this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
833
		$this->Controller->request = $this->getMock('CakeRequest');
834
		$this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
835
		$this->Controller->RequestHandler->request = $this->Controller->request;
836
		$this->Controller->RequestHandler->response = $this->Controller->response;
837
		$this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
838
		$this->Controller->RequestHandler->expects($this->once())->method('_stop');
839
 
840
		ob_start();
841
		$this->Controller->RequestHandler->beforeRedirect(
842
			$this->Controller, array('controller' => 'request_handler_test', 'action' => 'ajax2_layout')
843
		);
844
		$result = ob_get_clean();
845
		$this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
846
		$this->assertRegExp('/Ajax!/', $result, 'Layout was not rendered.');
847
 
848
		App::build();
849
	}
850
 
851
/**
852
 * test that the beforeRedirect callback properly converts
853
 * array URLs into their correct string ones, and adds base => false so
854
 * the correct URLs are generated.
855
 *
856
 * @link https://cakephp.lighthouseapp.com/projects/42648-cakephp-1x/tickets/276
857
 * @return void
858
 */
859
	public function testBeforeRedirectCallbackWithArrayUrl() {
860
		$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
861
 
862
		Router::setRequestInfo(array(
863
			array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'named' => array(), 'form' => array(), 'url' => array('url' => 'accounts/')),
864
			array('base' => '/officespace', 'here' => '/officespace/accounts/', 'webroot' => '/officespace/')
865
		));
866
 
867
		$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
868
		$RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader'));
869
		$RequestHandler->request = new CakeRequest('posts/index');
870
		$RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader'));
871
 
872
		ob_start();
873
		$RequestHandler->beforeRedirect(
874
			$this->Controller,
875
			array('controller' => 'request_handler_test', 'action' => 'param_method', 'first', 'second')
876
		);
877
		$result = ob_get_clean();
878
		$this->assertEquals('one: first two: second', $result);
879
	}
880
 
881
/**
882
 * assure that beforeRedirect with a status code will correctly set the status header
883
 *
884
 * @return void
885
 */
886
	public function testBeforeRedirectCallingHeader() {
887
		$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
888
 
889
		$controller = $this->getMock('Controller', array('header'));
890
		$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
891
		$RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader', 'statusCode'));
892
		$RequestHandler->request = $this->getMock('CakeRequest');
893
		$RequestHandler->request->expects($this->once())->method('is')
894
			->with('ajax')
895
			->will($this->returnValue(true));
896
 
897
		$RequestHandler->response->expects($this->once())->method('statusCode')->with(403);
898
 
899
		ob_start();
900
		$RequestHandler->beforeRedirect($controller, 'request_handler_test/param_method/first/second', 403);
901
		ob_get_clean();
902
	}
903
 
904
/**
905
 * @expectedException CakeException
906
 * @return void
907
 */
908
	public function testAddInputTypeException() {
909
		$this->RequestHandler->addInputType('csv', array('I am not callable'));
910
	}
911
 
912
/**
913
 * Test checkNotModified method
914
 *
915
 * @return void
916
 */
917
	public function testCheckNotModifiedByEtagStar() {
918
		$_SERVER['HTTP_IF_NONE_MATCH'] = '*';
919
		$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
920
		$RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
921
		$RequestHandler->response->etag('something');
922
		$RequestHandler->response->expects($this->once())->method('notModified');
923
		$this->assertFalse($RequestHandler->beforeRender($this->Controller));
924
	}
925
 
926
/**
927
 * Test checkNotModified method
928
 *
929
 * @return void
930
 */
931
	public function testCheckNotModifiedByEtagExact() {
932
		$_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
933
		$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
934
		$RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
935
		$RequestHandler->response->etag('something', true);
936
		$RequestHandler->response->expects($this->once())->method('notModified');
937
		$this->assertFalse($RequestHandler->beforeRender($this->Controller));
938
	}
939
 
940
/**
941
 * Test checkNotModified method
942
 *
943
 * @return void
944
 */
945
	public function testCheckNotModifiedByEtagAndTime() {
946
		$_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
947
		$_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
948
		$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
949
		$RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
950
		$RequestHandler->response->etag('something', true);
951
		$RequestHandler->response->modified('2012-01-01 00:00:00');
952
		$RequestHandler->response->expects($this->once())->method('notModified');
953
		$this->assertFalse($RequestHandler->beforeRender($this->Controller));
954
	}
955
 
956
/**
957
 * Test checkNotModified method
958
 *
959
 * @return void
960
 */
961
	public function testCheckNotModifiedNoInfo() {
962
		$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
963
		$RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
964
		$RequestHandler->response->expects($this->never())->method('notModified');
965
		$this->assertNull($RequestHandler->beforeRender($this->Controller));
966
	}
967
}