Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * CakeRequest Test case file.
4
 *
5
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
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://cakephp.org CakePHP(tm) Project
14
 * @package       Cake.Test.Case.Network
15
 * @since         CakePHP(tm) v 2.0
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('Dispatcher', 'Routing');
20
App::uses('Xml', 'Utility');
21
App::uses('CakeRequest', 'Network');
22
 
23
/**
24
 * Class TestCakeRequest
25
 *
26
 * @package       Cake.Test.Case.Network
27
 */
28
class TestCakeRequest extends CakeRequest {
29
 
30
/**
31
 * reConstruct method
32
 *
33
 * @param string $url
34
 * @param boolean $parseEnvironment
35
 * @return void
36
 */
37
	public function reConstruct($url = 'some/path', $parseEnvironment = true) {
38
		$this->_base();
39
		if (empty($url)) {
40
			$url = $this->_url();
41
		}
42
		if ($url[0] === '/') {
43
			$url = substr($url, 1);
44
		}
45
		$this->url = $url;
46
 
47
		if ($parseEnvironment) {
48
			$this->_processPost();
49
			$this->_processGet();
50
			$this->_processFiles();
51
		}
52
		$this->here = $this->base . '/' . $this->url;
53
	}
54
 
55
}
56
 
57
/**
58
 * Class CakeRequestTest
59
 */
60
class CakeRequestTest extends CakeTestCase {
61
 
62
/**
63
 * Setup callback
64
 *
65
 * @return void
66
 */
67
	public function setUp() {
68
		parent::setUp();
69
		$this->_app = Configure::read('App');
70
		$this->_case = null;
71
		if (isset($_GET['case'])) {
72
			$this->_case = $_GET['case'];
73
			unset($_GET['case']);
74
		}
75
 
76
		Configure::write('App.baseUrl', false);
77
	}
78
 
79
/**
80
 * TearDown
81
 *
82
 * @return void
83
 */
84
	public function tearDown() {
85
		parent::tearDown();
86
		if (!empty($this->_case)) {
87
			$_GET['case'] = $this->_case;
88
		}
89
		Configure::write('App', $this->_app);
90
	}
91
 
92
/**
93
 * Test that the autoparse = false constructor works.
94
 *
95
 * @return void
96
 */
97
	public function testNoAutoParseConstruction() {
98
		$_GET = array(
99
			'one' => 'param'
100
		);
101
		$request = new CakeRequest(null, false);
102
		$this->assertFalse(isset($request->query['one']));
103
	}
104
 
105
/**
106
 * Test construction
107
 *
108
 * @return void
109
 */
110
	public function testConstructionGetParsing() {
111
		$_GET = array(
112
			'one' => 'param',
113
			'two' => 'banana'
114
		);
115
		$request = new CakeRequest('some/path');
116
		$this->assertEquals($request->query, $_GET);
117
 
118
		$_GET = array(
119
			'one' => 'param',
120
			'two' => 'banana',
121
		);
122
		$request = new CakeRequest('some/path');
123
		$this->assertEquals($request->query, $_GET);
124
		$this->assertEquals('some/path', $request->url);
125
	}
126
 
127
/**
128
 * Test that querystring args provided in the URL string are parsed.
129
 *
130
 * @return void
131
 */
132
	public function testQueryStringParsingFromInputUrl() {
133
		$_GET = array();
134
		$request = new CakeRequest('some/path?one=something&two=else');
135
		$expected = array('one' => 'something', 'two' => 'else');
136
		$this->assertEquals($expected, $request->query);
137
		$this->assertEquals('some/path?one=something&two=else', $request->url);
138
	}
139
 
140
/**
141
 * Test that named arguments + querystrings are handled correctly.
142
 *
143
 * @return void
144
 */
145
	public function testQueryStringAndNamedParams() {
146
		$_SERVER['REQUEST_URI'] = '/tasks/index/page:1?ts=123456';
147
		$request = new CakeRequest();
148
		$this->assertEquals('tasks/index/page:1', $request->url);
149
 
150
		$_SERVER['REQUEST_URI'] = '/tasks/index/page:1/?ts=123456';
151
		$request = new CakeRequest();
152
		$this->assertEquals('tasks/index/page:1/', $request->url);
153
 
154
		$_SERVER['REQUEST_URI'] = '/some/path?url=http://cakephp.org';
155
		$request = new CakeRequest();
156
		$this->assertEquals('some/path', $request->url);
157
 
158
		$_SERVER['REQUEST_URI'] = Configure::read('App.fullBaseUrl') . '/other/path?url=http://cakephp.org';
159
		$request = new CakeRequest();
160
		$this->assertEquals('other/path', $request->url);
161
	}
162
 
163
/**
164
 * Test addParams() method
165
 *
166
 * @return void
167
 */
168
	public function testAddParams() {
169
		$request = new CakeRequest('some/path');
170
		$request->params = array('controller' => 'posts', 'action' => 'view');
171
		$result = $request->addParams(array('plugin' => null, 'action' => 'index'));
172
 
173
		$this->assertSame($result, $request, 'Method did not return itself. %s');
174
 
175
		$this->assertEquals('posts', $request->controller);
176
		$this->assertEquals('index', $request->action);
177
		$this->assertEquals(null, $request->plugin);
178
	}
179
 
180
/**
181
 * Test splicing in paths.
182
 *
183
 * @return void
184
 */
185
	public function testAddPaths() {
186
		$request = new CakeRequest('some/path');
187
		$request->webroot = '/some/path/going/here/';
188
		$result = $request->addPaths(array(
189
			'random' => '/something', 'webroot' => '/', 'here' => '/', 'base' => '/base_dir'
190
		));
191
 
192
		$this->assertSame($result, $request, 'Method did not return itself. %s');
193
 
194
		$this->assertEquals('/', $request->webroot);
195
		$this->assertEquals('/base_dir', $request->base);
196
		$this->assertEquals('/', $request->here);
197
		$this->assertFalse(isset($request->random));
198
	}
199
 
200
/**
201
 * Test parsing POST data into the object.
202
 *
203
 * @return void
204
 */
205
	public function testPostParsing() {
206
		$_POST = array('data' => array(
207
			'Article' => array('title')
208
		));
209
		$request = new CakeRequest('some/path');
210
		$this->assertEquals($_POST['data'], $request->data);
211
 
212
		$_POST = array('one' => 1, 'two' => 'three');
213
		$request = new CakeRequest('some/path');
214
		$this->assertEquals($_POST, $request->data);
215
 
216
		$_POST = array(
217
			'data' => array(
218
				'Article' => array('title' => 'Testing'),
219
			),
220
			'action' => 'update'
221
		);
222
		$request = new CakeRequest('some/path');
223
		$expected = array(
224
			'Article' => array('title' => 'Testing'),
225
			'action' => 'update'
226
		);
227
		$this->assertEquals($expected, $request->data);
228
 
229
		$_POST = array('data' => array(
230
			'Article' => array('title'),
231
			'Tag' => array('Tag' => array(1, 2))
232
		));
233
		$request = new CakeRequest('some/path');
234
		$this->assertEquals($_POST['data'], $request->data);
235
 
236
		$_POST = array('data' => array(
237
			'Article' => array('title' => 'some title'),
238
			'Tag' => array('Tag' => array(1, 2))
239
		));
240
		$request = new CakeRequest('some/path');
241
		$this->assertEquals($_POST['data'], $request->data);
242
 
243
		$_POST = array(
244
			'a' => array(1, 2),
245
			'b' => array(1, 2)
246
		);
247
		$request = new CakeRequest('some/path');
248
		$this->assertEquals($_POST, $request->data);
249
	}
250
 
251
/**
252
 * Test parsing PUT data into the object.
253
 *
254
 * @return void
255
 */
256
	public function testPutParsing() {
257
		$_SERVER['REQUEST_METHOD'] = 'PUT';
258
		$_SERVER['CONTENT_TYPE'] = 'application/x-www-form-urlencoded; charset=UTF-8';
259
 
260
		$data = array('data' => array(
261
			'Article' => array('title')
262
		));
263
 
264
		$request = $this->getMock('TestCakeRequest', array('_readInput'));
265
		$request->expects($this->at(0))->method('_readInput')
266
			->will($this->returnValue('data[Article][]=title'));
267
		$request->reConstruct();
268
		$this->assertEquals($data['data'], $request->data);
269
 
270
		$data = array('one' => 1, 'two' => 'three');
271
		$request = $this->getMock('TestCakeRequest', array('_readInput'));
272
		$request->expects($this->at(0))->method('_readInput')
273
			->will($this->returnValue('one=1&two=three'));
274
		$request->reConstruct();
275
		$this->assertEquals($data, $request->data);
276
 
277
		$data = array(
278
			'data' => array(
279
				'Article' => array('title' => 'Testing'),
280
			),
281
			'action' => 'update'
282
		);
283
		$request = $this->getMock('TestCakeRequest', array('_readInput'));
284
		$request->expects($this->at(0))->method('_readInput')
285
			->will($this->returnValue('data[Article][title]=Testing&action=update'));
286
		$request->reConstruct();
287
		$expected = array(
288
			'Article' => array('title' => 'Testing'),
289
			'action' => 'update'
290
		);
291
		$this->assertEquals($expected, $request->data);
292
 
293
		$_SERVER['REQUEST_METHOD'] = 'DELETE';
294
		$data = array('data' => array(
295
			'Article' => array('title'),
296
			'Tag' => array('Tag' => array(1, 2))
297
		));
298
		$request = $this->getMock('TestCakeRequest', array('_readInput'));
299
		$request->expects($this->at(0))->method('_readInput')
300
			->will($this->returnValue('data[Article][]=title&Tag[Tag][]=1&Tag[Tag][]=2'));
301
		$request->reConstruct();
302
		$this->assertEquals($data['data'], $request->data);
303
 
304
		$data = array('data' => array(
305
			'Article' => array('title' => 'some title'),
306
			'Tag' => array('Tag' => array(1, 2))
307
		));
308
		$request = $this->getMock('TestCakeRequest', array('_readInput'));
309
		$request->expects($this->at(0))->method('_readInput')
310
			->will($this->returnValue('data[Article][title]=some%20title&Tag[Tag][]=1&Tag[Tag][]=2'));
311
		$request->reConstruct();
312
		$this->assertEquals($data['data'], $request->data);
313
 
314
		$data = array(
315
			'a' => array(1, 2),
316
			'b' => array(1, 2)
317
		);
318
		$request = $this->getMock('TestCakeRequest', array('_readInput'));
319
		$request->expects($this->at(0))->method('_readInput')
320
			->will($this->returnValue('a[]=1&a[]=2&b[]=1&b[]=2'));
321
		$request->reConstruct();
322
		$this->assertEquals($data, $request->data);
323
	}
324
 
325
/**
326
 * Test parsing json PUT data into the object.
327
 *
328
 * @return void
329
 */
330
	public function testPutParsingJSON() {
331
		$_SERVER['REQUEST_METHOD'] = 'PUT';
332
		$_SERVER['CONTENT_TYPE'] = 'application/json';
333
 
334
		$request = $this->getMock('TestCakeRequest', array('_readInput'));
335
		$request->expects($this->at(0))->method('_readInput')
336
			->will($this->returnValue('{"Article":["title"]}'));
337
		$request->reConstruct();
338
		$result = $request->input('json_decode', true);
339
		$this->assertEquals(array('title'), $result['Article']);
340
	}
341
 
342
/**
343
 * Test parsing of FILES array
344
 *
345
 * @return void
346
 */
347
	public function testFilesParsing() {
348
		$_FILES = array(
349
			'data' => array(
350
				'name' => array(
351
					'File' => array(
352
							array('data' => 'cake_sqlserver_patch.patch'),
353
							array('data' => 'controller.diff'),
354
							array('data' => ''),
355
							array('data' => ''),
356
						),
357
						'Post' => array('attachment' => 'jquery-1.2.1.js'),
358
					),
359
				'type' => array(
360
					'File' => array(
361
						array('data' => ''),
362
						array('data' => ''),
363
						array('data' => ''),
364
						array('data' => ''),
365
					),
366
					'Post' => array('attachment' => 'application/x-javascript'),
367
				),
368
				'tmp_name' => array(
369
					'File' => array(
370
						array('data' => '/private/var/tmp/phpy05Ywj'),
371
						array('data' => '/private/var/tmp/php7MBztY'),
372
						array('data' => ''),
373
						array('data' => ''),
374
					),
375
					'Post' => array('attachment' => '/private/var/tmp/phpEwlrIo'),
376
				),
377
				'error' => array(
378
					'File' => array(
379
						array('data' => 0),
380
						array('data' => 0),
381
						array('data' => 4),
382
						array('data' => 4)
383
					),
384
					'Post' => array('attachment' => 0)
385
				),
386
				'size' => array(
387
					'File' => array(
388
						array('data' => 6271),
389
						array('data' => 350),
390
						array('data' => 0),
391
						array('data' => 0),
392
					),
393
					'Post' => array('attachment' => 80469)
394
				),
395
			)
396
		);
397
 
398
		$request = new CakeRequest('some/path');
399
		$expected = array(
400
			'File' => array(
401
				array(
402
					'data' => array(
403
						'name' => 'cake_sqlserver_patch.patch',
404
						'type' => '',
405
						'tmp_name' => '/private/var/tmp/phpy05Ywj',
406
						'error' => 0,
407
						'size' => 6271,
408
					)
409
				),
410
				array(
411
					'data' => array(
412
						'name' => 'controller.diff',
413
						'type' => '',
414
						'tmp_name' => '/private/var/tmp/php7MBztY',
415
						'error' => 0,
416
						'size' => 350,
417
					)
418
				),
419
				array(
420
					'data' => array(
421
						'name' => '',
422
						'type' => '',
423
						'tmp_name' => '',
424
						'error' => 4,
425
						'size' => 0,
426
					)
427
				),
428
				array(
429
					'data' => array(
430
						'name' => '',
431
						'type' => '',
432
						'tmp_name' => '',
433
						'error' => 4,
434
						'size' => 0,
435
					)
436
				),
437
			),
438
			'Post' => array(
439
				'attachment' => array(
440
					'name' => 'jquery-1.2.1.js',
441
					'type' => 'application/x-javascript',
442
					'tmp_name' => '/private/var/tmp/phpEwlrIo',
443
					'error' => 0,
444
					'size' => 80469,
445
				)
446
			)
447
		);
448
		$this->assertEquals($expected, $request->data);
449
 
450
		$_FILES = array(
451
			'data' => array(
452
				'name' => array(
453
					'Document' => array(
454
						1 => array(
455
							'birth_cert' => 'born on.txt',
456
							'passport' => 'passport.txt',
457
							'drivers_license' => 'ugly pic.jpg'
458
						),
459
						2 => array(
460
							'birth_cert' => 'aunt betty.txt',
461
							'passport' => 'betty-passport.txt',
462
							'drivers_license' => 'betty-photo.jpg'
463
						),
464
					),
465
				),
466
				'type' => array(
467
					'Document' => array(
468
						1 => array(
469
							'birth_cert' => 'application/octet-stream',
470
							'passport' => 'application/octet-stream',
471
							'drivers_license' => 'application/octet-stream',
472
						),
473
						2 => array(
474
							'birth_cert' => 'application/octet-stream',
475
							'passport' => 'application/octet-stream',
476
							'drivers_license' => 'application/octet-stream',
477
						)
478
					)
479
				),
480
				'tmp_name' => array(
481
					'Document' => array(
482
						1 => array(
483
							'birth_cert' => '/private/var/tmp/phpbsUWfH',
484
							'passport' => '/private/var/tmp/php7f5zLt',
485
							'drivers_license' => '/private/var/tmp/phpMXpZgT',
486
						),
487
						2 => array(
488
							'birth_cert' => '/private/var/tmp/php5kHZt0',
489
							'passport' => '/private/var/tmp/phpnYkOuM',
490
							'drivers_license' => '/private/var/tmp/php9Rq0P3',
491
						)
492
					)
493
				),
494
				'error' => array(
495
					'Document' => array(
496
						1 => array(
497
							'birth_cert' => 0,
498
							'passport' => 0,
499
							'drivers_license' => 0,
500
						),
501
						2 => array(
502
							'birth_cert' => 0,
503
							'passport' => 0,
504
							'drivers_license' => 0,
505
						)
506
					)
507
				),
508
				'size' => array(
509
					'Document' => array(
510
						1 => array(
511
							'birth_cert' => 123,
512
							'passport' => 458,
513
							'drivers_license' => 875,
514
						),
515
						2 => array(
516
							'birth_cert' => 876,
517
							'passport' => 976,
518
							'drivers_license' => 9783,
519
						)
520
					)
521
				)
522
			)
523
		);
524
 
525
		$request = new CakeRequest('some/path');
526
		$expected = array(
527
			'Document' => array(
528
				1 => array(
529
					'birth_cert' => array(
530
						'name' => 'born on.txt',
531
						'tmp_name' => '/private/var/tmp/phpbsUWfH',
532
						'error' => 0,
533
						'size' => 123,
534
						'type' => 'application/octet-stream',
535
					),
536
					'passport' => array(
537
						'name' => 'passport.txt',
538
						'tmp_name' => '/private/var/tmp/php7f5zLt',
539
						'error' => 0,
540
						'size' => 458,
541
						'type' => 'application/octet-stream',
542
					),
543
					'drivers_license' => array(
544
						'name' => 'ugly pic.jpg',
545
						'tmp_name' => '/private/var/tmp/phpMXpZgT',
546
						'error' => 0,
547
						'size' => 875,
548
						'type' => 'application/octet-stream',
549
					),
550
				),
551
				2 => array(
552
					'birth_cert' => array(
553
						'name' => 'aunt betty.txt',
554
						'tmp_name' => '/private/var/tmp/php5kHZt0',
555
						'error' => 0,
556
						'size' => 876,
557
						'type' => 'application/octet-stream',
558
					),
559
					'passport' => array(
560
						'name' => 'betty-passport.txt',
561
						'tmp_name' => '/private/var/tmp/phpnYkOuM',
562
						'error' => 0,
563
						'size' => 976,
564
						'type' => 'application/octet-stream',
565
					),
566
					'drivers_license' => array(
567
						'name' => 'betty-photo.jpg',
568
						'tmp_name' => '/private/var/tmp/php9Rq0P3',
569
						'error' => 0,
570
						'size' => 9783,
571
						'type' => 'application/octet-stream',
572
					),
573
				),
574
			)
575
		);
576
		$this->assertEquals($expected, $request->data);
577
 
578
		$_FILES = array(
579
			'data' => array(
580
				'name' => array('birth_cert' => 'born on.txt'),
581
				'type' => array('birth_cert' => 'application/octet-stream'),
582
				'tmp_name' => array('birth_cert' => '/private/var/tmp/phpbsUWfH'),
583
				'error' => array('birth_cert' => 0),
584
				'size' => array('birth_cert' => 123)
585
			)
586
		);
587
 
588
		$request = new CakeRequest('some/path');
589
		$expected = array(
590
			'birth_cert' => array(
591
				'name' => 'born on.txt',
592
				'type' => 'application/octet-stream',
593
				'tmp_name' => '/private/var/tmp/phpbsUWfH',
594
				'error' => 0,
595
				'size' => 123
596
			)
597
		);
598
		$this->assertEquals($expected, $request->data);
599
 
600
		$_FILES = array(
601
			'something' => array(
602
				'name' => 'something.txt',
603
				'type' => 'text/plain',
604
				'tmp_name' => '/some/file',
605
				'error' => 0,
606
				'size' => 123
607
			)
608
		);
609
		$request = new CakeRequest('some/path');
610
		$this->assertEquals($request->params['form'], $_FILES);
611
	}
612
 
613
/**
614
 * Test that files in the 0th index work.
615
 */
616
	public function testFilesZeroithIndex() {
617
		$_FILES = array(
618
 
619
				'name' => 'cake_sqlserver_patch.patch',
620
				'type' => 'text/plain',
621
				'tmp_name' => '/private/var/tmp/phpy05Ywj',
622
				'error' => 0,
623
				'size' => 6271,
624
			),
625
		);
626
 
627
		$request = new CakeRequest('some/path');
628
		$this->assertEquals($_FILES, $request->params['form']);
629
	}
630
 
631
/**
632
 * Test method overrides coming in from POST data.
633
 *
634
 * @return void
635
 */
636
	public function testMethodOverrides() {
637
		$_POST = array('_method' => 'POST');
638
		$request = new CakeRequest('some/path');
639
		$this->assertEquals(env('REQUEST_METHOD'), 'POST');
640
 
641
		$_POST = array('_method' => 'DELETE');
642
		$request = new CakeRequest('some/path');
643
		$this->assertEquals(env('REQUEST_METHOD'), 'DELETE');
644
 
645
		$_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
646
		$request = new CakeRequest('some/path');
647
		$this->assertEquals(env('REQUEST_METHOD'), 'PUT');
648
	}
649
 
650
/**
651
 * Test the clientIp method.
652
 *
653
 * @return void
654
 */
655
	public function testclientIp() {
656
		$_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.5, 10.0.1.1, proxy.com';
657
		$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
658
		$_SERVER['REMOTE_ADDR'] = '192.168.1.3';
659
		$request = new CakeRequest('some/path');
660
		$this->assertEquals('192.168.1.5', $request->clientIp(false));
661
		$this->assertEquals('192.168.1.2', $request->clientIp());
662
 
663
		unset($_SERVER['HTTP_X_FORWARDED_FOR']);
664
		$this->assertEquals('192.168.1.2', $request->clientIp());
665
 
666
		unset($_SERVER['HTTP_CLIENT_IP']);
667
		$this->assertEquals('192.168.1.3', $request->clientIp());
668
 
669
		$_SERVER['HTTP_CLIENTADDRESS'] = '10.0.1.2, 10.0.1.1';
670
		$this->assertEquals('10.0.1.2', $request->clientIp());
671
	}
672
 
673
/**
674
 * Test the referrer function.
675
 *
676
 * @return void
677
 */
678
	public function testReferer() {
679
		$request = new CakeRequest('some/path');
680
		$request->webroot = '/';
681
 
682
		$_SERVER['HTTP_REFERER'] = 'http://cakephp.org';
683
		$result = $request->referer();
684
		$this->assertSame($result, 'http://cakephp.org');
685
 
686
		$_SERVER['HTTP_REFERER'] = '';
687
		$result = $request->referer();
688
		$this->assertSame($result, '/');
689
 
690
		$_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '/some/path';
691
		$result = $request->referer(true);
692
		$this->assertSame($result, '/some/path');
693
 
694
		$_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '/some/path';
695
		$result = $request->referer(false);
696
		$this->assertSame($result, Configure::read('App.fullBaseUrl') . '/some/path');
697
 
698
		$_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '/recipes/add';
699
		$result = $request->referer(true);
700
		$this->assertSame($result, '/recipes/add');
701
 
702
		$_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org';
703
		$result = $request->referer();
704
		$this->assertSame($result, 'cakephp.org');
705
	}
706
 
707
/**
708
 * Test referer() with a base path that duplicates the
709
 * first segment.
710
 *
711
 * @return void
712
 */
713
	public function testRefererBasePath() {
714
		$request = new CakeRequest('some/path');
715
		$request->url = 'users/login';
716
		$request->webroot = '/waves/';
717
		$request->base = '/waves';
718
		$request->here = '/waves/users/login';
719
 
720
		$_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/waves/waves/add';
721
 
722
		$result = $request->referer(true);
723
		$this->assertSame($result, '/waves/add');
724
	}
725
 
726
/**
727
 * test the simple uses of is()
728
 *
729
 * @return void
730
 */
731
	public function testIsHttpMethods() {
732
		$request = new CakeRequest('some/path');
733
 
734
		$this->assertFalse($request->is('undefined-behavior'));
735
 
736
		$_SERVER['REQUEST_METHOD'] = 'GET';
737
		$this->assertTrue($request->is('get'));
738
 
739
		$_SERVER['REQUEST_METHOD'] = 'POST';
740
		$this->assertTrue($request->is('POST'));
741
 
742
		$_SERVER['REQUEST_METHOD'] = 'PUT';
743
		$this->assertTrue($request->is('put'));
744
		$this->assertFalse($request->is('get'));
745
 
746
		$_SERVER['REQUEST_METHOD'] = 'DELETE';
747
		$this->assertTrue($request->is('delete'));
748
		$this->assertTrue($request->isDelete());
749
 
750
		$_SERVER['REQUEST_METHOD'] = 'delete';
751
		$this->assertFalse($request->is('delete'));
752
	}
753
 
754
/**
755
 * Test is() with multiple types.
756
 *
757
 * @return void
758
 */
759
	public function testIsMultiple() {
760
		$request = new CakeRequest('some/path');
761
 
762
		$_SERVER['REQUEST_METHOD'] = 'GET';
763
		$this->assertTrue($request->is(array('get', 'post')));
764
 
765
		$_SERVER['REQUEST_METHOD'] = 'POST';
766
		$this->assertTrue($request->is(array('get', 'post')));
767
 
768
		$_SERVER['REQUEST_METHOD'] = 'PUT';
769
		$this->assertFalse($request->is(array('get', 'post')));
770
	}
771
 
772
/**
773
 * Test isAll()
774
 *
775
 * @return void
776
 */
777
	public function testIsAll() {
778
		$request = new CakeRequest('some/path');
779
 
780
		$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
781
		$_SERVER['REQUEST_METHOD'] = 'GET';
782
 
783
		$this->assertTrue($request->isAll(array('ajax', 'get')));
784
		$this->assertFalse($request->isAll(array('post', 'get')));
785
		$this->assertFalse($request->isAll(array('ajax', 'post')));
786
	}
787
 
788
/**
789
 * Test the method() method.
790
 *
791
 * @return void
792
 */
793
	public function testMethod() {
794
		$_SERVER['REQUEST_METHOD'] = 'delete';
795
		$request = new CakeRequest('some/path');
796
 
797
		$this->assertEquals('delete', $request->method());
798
	}
799
 
800
/**
801
 * Test host retrieval.
802
 *
803
 * @return void
804
 */
805
	public function testHost() {
806
		$_SERVER['HTTP_HOST'] = 'localhost';
807
		$request = new CakeRequest('some/path');
808
 
809
		$this->assertEquals('localhost', $request->host());
810
	}
811
 
812
/**
813
 * Test domain retrieval.
814
 *
815
 * @return void
816
 */
817
	public function testDomain() {
818
		$_SERVER['HTTP_HOST'] = 'something.example.com';
819
		$request = new CakeRequest('some/path');
820
 
821
		$this->assertEquals('example.com', $request->domain());
822
 
823
		$_SERVER['HTTP_HOST'] = 'something.example.co.uk';
824
		$this->assertEquals('example.co.uk', $request->domain(2));
825
	}
826
 
827
/**
828
 * Test getting subdomains for a host.
829
 *
830
 * @return void
831
 */
832
	public function testSubdomain() {
833
		$_SERVER['HTTP_HOST'] = 'something.example.com';
834
		$request = new CakeRequest('some/path');
835
 
836
		$this->assertEquals(array('something'), $request->subdomains());
837
 
838
		$_SERVER['HTTP_HOST'] = 'www.something.example.com';
839
		$this->assertEquals(array('www', 'something'), $request->subdomains());
840
 
841
		$_SERVER['HTTP_HOST'] = 'www.something.example.co.uk';
842
		$this->assertEquals(array('www', 'something'), $request->subdomains(2));
843
 
844
		$_SERVER['HTTP_HOST'] = 'example.co.uk';
845
		$this->assertEquals(array(), $request->subdomains(2));
846
	}
847
 
848
/**
849
 * Test ajax, flash and friends
850
 *
851
 * @return void
852
 */
853
	public function testisAjaxFlashAndFriends() {
854
		$request = new CakeRequest('some/path');
855
 
856
		$_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
857
		$this->assertTrue($request->is('flash'));
858
 
859
		$_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash';
860
		$this->assertTrue($request->is('flash'));
861
 
862
		$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
863
		$this->assertTrue($request->is('ajax'));
864
 
865
		$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHTTPREQUEST';
866
		$this->assertFalse($request->is('ajax'));
867
		$this->assertFalse($request->isAjax());
868
 
869
		$_SERVER['HTTP_USER_AGENT'] = 'Android 2.0';
870
		$this->assertTrue($request->is('mobile'));
871
		$this->assertTrue($request->isMobile());
872
 
873
		$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 5.1; rv:2.0b6pre) Gecko/20100902 Firefox/4.0b6pre Fennec/2.0b1pre';
874
		$this->assertTrue($request->is('mobile'));
875
		$this->assertTrue($request->isMobile());
876
 
877
		$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; SAMSUNG; OMNIA7)';
878
		$this->assertTrue($request->is('mobile'));
879
		$this->assertTrue($request->isMobile());
880
	}
881
 
882
/**
883
 * Test __call exceptions
884
 *
885
 * @expectedException CakeException
886
 * @return void
887
 */
888
	public function testMagicCallExceptionOnUnknownMethod() {
889
		$request = new CakeRequest('some/path');
890
		$request->IamABanana();
891
	}
892
 
893
/**
894
 * Test is(ssl)
895
 *
896
 * @return void
897
 */
898
	public function testIsSsl() {
899
		$request = new CakeRequest('some/path');
900
 
901
		$_SERVER['HTTPS'] = 1;
902
		$this->assertTrue($request->is('ssl'));
903
 
904
		$_SERVER['HTTPS'] = 'on';
905
		$this->assertTrue($request->is('ssl'));
906
 
907
		$_SERVER['HTTPS'] = '1';
908
		$this->assertTrue($request->is('ssl'));
909
 
910
		$_SERVER['HTTPS'] = 'I am not empty';
911
		$this->assertTrue($request->is('ssl'));
912
 
913
		$_SERVER['HTTPS'] = 1;
914
		$this->assertTrue($request->is('ssl'));
915
 
916
		$_SERVER['HTTPS'] = 'off';
917
		$this->assertFalse($request->is('ssl'));
918
 
919
		$_SERVER['HTTPS'] = false;
920
		$this->assertFalse($request->is('ssl'));
921
 
922
		$_SERVER['HTTPS'] = '';
923
		$this->assertFalse($request->is('ssl'));
924
	}
925
 
926
/**
927
 * Test getting request params with object properties.
928
 *
929
 * @return void
930
 */
931
	public function testMagicget() {
932
		$request = new CakeRequest('some/path');
933
		$request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
934
 
935
		$this->assertEquals('posts', $request->controller);
936
		$this->assertEquals('view', $request->action);
937
		$this->assertEquals('blogs', $request->plugin);
938
		$this->assertNull($request->banana);
939
	}
940
 
941
/**
942
 * Test isset()/empty() with overloaded properties.
943
 *
944
 * @return void
945
 */
946
	public function testMagicisset() {
947
		$request = new CakeRequest('some/path');
948
		$request->params = array(
949
			'controller' => 'posts',
950
			'action' => 'view',
951
			'plugin' => 'blogs',
952
			'named' => array()
953
		);
954
 
955
		$this->assertTrue(isset($request->controller));
956
		$this->assertFalse(isset($request->notthere));
957
		$this->assertFalse(empty($request->controller));
958
		$this->assertTrue(empty($request->named));
959
	}
960
 
961
/**
962
 * Test the array access implementation
963
 *
964
 * @return void
965
 */
966
	public function testArrayAccess() {
967
		$request = new CakeRequest('some/path');
968
		$request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
969
 
970
		$this->assertEquals('posts', $request['controller']);
971
 
972
		$request['slug'] = 'speedy-slug';
973
		$this->assertEquals('speedy-slug', $request->slug);
974
		$this->assertEquals('speedy-slug', $request['slug']);
975
 
976
		$this->assertTrue(isset($request['action']));
977
		$this->assertFalse(isset($request['wrong-param']));
978
 
979
		$this->assertTrue(isset($request['plugin']));
980
		unset($request['plugin']);
981
		$this->assertFalse(isset($request['plugin']));
982
		$this->assertNull($request['plugin']);
983
		$this->assertNull($request->plugin);
984
 
985
		$request = new CakeRequest('some/path?one=something&two=else');
986
		$this->assertTrue(isset($request['url']['one']));
987
 
988
		$request->data = array('Post' => array('title' => 'something'));
989
		$this->assertEquals('something', $request['data']['Post']['title']);
990
	}
991
 
992
/**
993
 * Test adding detectors and having them work.
994
 *
995
 * @return void
996
 */
997
	public function testAddDetector() {
998
		$request = new CakeRequest('some/path');
999
		$request->addDetector('compare', array('env' => 'TEST_VAR', 'value' => 'something'));
1000
 
1001
		$_SERVER['TEST_VAR'] = 'something';
1002
		$this->assertTrue($request->is('compare'), 'Value match failed.');
1003
 
1004
		$_SERVER['TEST_VAR'] = 'wrong';
1005
		$this->assertFalse($request->is('compare'), 'Value mis-match failed.');
1006
 
1007
		$request->addDetector('compareCamelCase', array('env' => 'TEST_VAR', 'value' => 'foo'));
1008
 
1009
		$_SERVER['TEST_VAR'] = 'foo';
1010
		$this->assertTrue($request->is('compareCamelCase'), 'Value match failed.');
1011
		$this->assertTrue($request->is('comparecamelcase'), 'detectors should be case insensitive');
1012
		$this->assertTrue($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
1013
 
1014
		$_SERVER['TEST_VAR'] = 'not foo';
1015
		$this->assertFalse($request->is('compareCamelCase'), 'Value match failed.');
1016
		$this->assertFalse($request->is('comparecamelcase'), 'detectors should be case insensitive');
1017
		$this->assertFalse($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
1018
 
1019
		$request->addDetector('banana', array('env' => 'TEST_VAR', 'pattern' => '/^ban.*$/'));
1020
		$_SERVER['TEST_VAR'] = 'banana';
1021
		$this->assertTrue($request->isBanana());
1022
 
1023
		$_SERVER['TEST_VAR'] = 'wrong value';
1024
		$this->assertFalse($request->isBanana());
1025
 
1026
		$request->addDetector('mobile', array('options' => array('Imagination')));
1027
		$_SERVER['HTTP_USER_AGENT'] = 'Imagination land';
1028
		$this->assertTrue($request->isMobile());
1029
 
1030
		$_SERVER['HTTP_USER_AGENT'] = 'iPhone 3.0';
1031
		$this->assertTrue($request->isMobile());
1032
 
1033
		$request->addDetector('callme', array('env' => 'TEST_VAR', 'callback' => array($this, 'detectCallback')));
1034
 
1035
		$request->addDetector('index', array('param' => 'action', 'value' => 'index'));
1036
		$request->params['action'] = 'index';
1037
		$this->assertTrue($request->isIndex());
1038
 
1039
		$request->params['action'] = 'add';
1040
		$this->assertFalse($request->isIndex());
1041
 
1042
		$request->return = true;
1043
		$this->assertTrue($request->isCallMe());
1044
 
1045
		$request->return = false;
1046
		$this->assertFalse($request->isCallMe());
1047
	}
1048
 
1049
/**
1050
 * Helper function for testing callbacks.
1051
 *
1052
 * @param $request
1053
 * @return boolean
1054
 */
1055
	public function detectCallback($request) {
1056
		return (bool)$request->return;
1057
	}
1058
 
1059
/**
1060
 * Test getting headers
1061
 *
1062
 * @return void
1063
 */
1064
	public function testHeader() {
1065
		$_SERVER['HTTP_HOST'] = 'localhost';
1066
		$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-ca) AppleWebKit/534.8+ (KHTML, like Gecko) Version/5.0 Safari/533.16';
1067
		$request = new CakeRequest('/', false);
1068
 
1069
		$this->assertEquals($_SERVER['HTTP_HOST'], $request->header('host'));
1070
		$this->assertEquals($_SERVER['HTTP_USER_AGENT'], $request->header('User-Agent'));
1071
	}
1072
 
1073
/**
1074
 * Test accepts() with and without parameters
1075
 *
1076
 * @return void
1077
 */
1078
	public function testAccepts() {
1079
		$_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml;q=0.9,application/xhtml+xml,text/html,text/plain,image/png';
1080
		$request = new CakeRequest('/', false);
1081
 
1082
		$result = $request->accepts();
1083
		$expected = array(
1084
			'text/xml', 'application/xhtml+xml', 'text/html', 'text/plain', 'image/png', 'application/xml'
1085
		);
1086
		$this->assertEquals($expected, $result, 'Content types differ.');
1087
 
1088
		$result = $request->accepts('text/html');
1089
		$this->assertTrue($result);
1090
 
1091
		$result = $request->accepts('image/gif');
1092
		$this->assertFalse($result);
1093
	}
1094
 
1095
/**
1096
 * Test that accept header types are trimmed for comparisons.
1097
 *
1098
 * @return void
1099
 */
1100
	public function testAcceptWithWhitespace() {
1101
		$_SERVER['HTTP_ACCEPT'] = 'text/xml  ,  text/html ,  text/plain,image/png';
1102
		$request = new CakeRequest('/', false);
1103
		$result = $request->accepts();
1104
		$expected = array(
1105
			'text/xml', 'text/html', 'text/plain', 'image/png'
1106
		);
1107
		$this->assertEquals($expected, $result, 'Content types differ.');
1108
 
1109
		$this->assertTrue($request->accepts('text/html'));
1110
	}
1111
 
1112
/**
1113
 * Content types from accepts() should respect the client's q preference values.
1114
 *
1115
 * @return void
1116
 */
1117
	public function testAcceptWithQvalueSorting() {
1118
		$_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0';
1119
		$request = new CakeRequest('/', false);
1120
		$result = $request->accepts();
1121
		$expected = array('application/xml', 'text/html', 'application/json');
1122
		$this->assertEquals($expected, $result);
1123
	}
1124
 
1125
/**
1126
 * Test the raw parsing of accept headers into the q value formatting.
1127
 *
1128
 * @return void
1129
 */
1130
	public function testParseAcceptWithQValue() {
1131
		$_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0,image/png';
1132
		$request = new CakeRequest('/', false);
1133
		$result = $request->parseAccept();
1134
		$expected = array(
1135
			'1.0' => array('application/xml', 'image/png'),
1136
			'0.8' => array('text/html'),
1137
			'0.7' => array('application/json'),
1138
		);
1139
		$this->assertEquals($expected, $result);
1140
	}
1141
 
1142
/**
1143
 * Test parsing accept with a confusing accept value.
1144
 *
1145
 * @return void
1146
 */
1147
	public function testParseAcceptNoQValues() {
1148
		$_SERVER['HTTP_ACCEPT'] = 'application/json, text/plain, */*';
1149
 
1150
		$request = new CakeRequest('/', false);
1151
		$result = $request->parseAccept();
1152
		$expected = array(
1153
			'1.0' => array('application/json', 'text/plain', '*/*'),
1154
		);
1155
		$this->assertEquals($expected, $result);
1156
	}
1157
 
1158
/**
1159
 * Test parsing accept ignores index param
1160
 *
1161
 * @return void
1162
 */
1163
	public function testParseAcceptIgnoreAcceptExtensions() {
1164
		$_SERVER['HTTP_ACCEPT'] = 'application/json;level=1, text/plain, */*';
1165
 
1166
		$request = new CakeRequest('/', false);
1167
		$result = $request->parseAccept();
1168
		$expected = array(
1169
			'1.0' => array('application/json', 'text/plain', '*/*'),
1170
		);
1171
		$this->assertEquals($expected, $result);
1172
	}
1173
 
1174
/**
1175
 * Test that parsing accept headers with invalid syntax works.
1176
 *
1177
 * The header used is missing a q value for application/xml.
1178
 *
1179
 * @return void
1180
 */
1181
	public function testParseAcceptInvalidSyntax() {
1182
		$_SERVER['HTTP_ACCEPT'] = 'text/html,application/xhtml+xml,application/xml;image/png,image/jpeg,image/*;q=0.9,*/*;q=0.8';
1183
		$request = new CakeRequest('/', false);
1184
		$result = $request->parseAccept();
1185
		$expected = array(
1186
			'1.0' => array('text/html', 'application/xhtml+xml', 'application/xml', 'image/jpeg'),
1187
			'0.9' => array('image/*'),
1188
			'0.8' => array('*/*'),
1189
		);
1190
		$this->assertEquals($expected, $result);
1191
	}
1192
 
1193
/**
1194
 * Test baseUrl and webroot with ModRewrite
1195
 *
1196
 * @return void
1197
 */
1198
	public function testBaseUrlAndWebrootWithModRewrite() {
1199
		Configure::write('App.baseUrl', false);
1200
 
1201
		$_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
1202
		$_SERVER['PHP_SELF'] = '/urlencode me/app/webroot/index.php';
1203
		$_SERVER['PATH_INFO'] = '/posts/view/1';
1204
 
1205
		$request = new CakeRequest();
1206
		$this->assertEquals('/urlencode%20me', $request->base);
1207
		$this->assertEquals('/urlencode%20me/', $request->webroot);
1208
		$this->assertEquals('posts/view/1', $request->url);
1209
 
1210
		$_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
1211
		$_SERVER['PHP_SELF'] = '/1.2.x.x/app/webroot/index.php';
1212
		$_SERVER['PATH_INFO'] = '/posts/view/1';
1213
 
1214
		$request = new CakeRequest();
1215
		$this->assertEquals('/1.2.x.x', $request->base);
1216
		$this->assertEquals('/1.2.x.x/', $request->webroot);
1217
		$this->assertEquals('posts/view/1', $request->url);
1218
 
1219
		$_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/app/webroot';
1220
		$_SERVER['PHP_SELF'] = '/index.php';
1221
		$_SERVER['PATH_INFO'] = '/posts/add';
1222
		$request = new CakeRequest();
1223
 
1224
		$this->assertEquals('', $request->base);
1225
		$this->assertEquals('/', $request->webroot);
1226
		$this->assertEquals('posts/add', $request->url);
1227
 
1228
		$_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/test/';
1229
		$_SERVER['PHP_SELF'] = '/webroot/index.php';
1230
		$request = new CakeRequest();
1231
 
1232
		$this->assertEquals('', $request->base);
1233
		$this->assertEquals('/', $request->webroot);
1234
 
1235
		$_SERVER['DOCUMENT_ROOT'] = '/some/apps/where';
1236
		$_SERVER['PHP_SELF'] = '/app/webroot/index.php';
1237
		$request = new CakeRequest();
1238
 
1239
		$this->assertEquals('', $request->base);
1240
		$this->assertEquals('/', $request->webroot);
1241
 
1242
		Configure::write('App.dir', 'auth');
1243
 
1244
		$_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
1245
		$_SERVER['PHP_SELF'] = '/demos/auth/webroot/index.php';
1246
 
1247
		$request = new CakeRequest();
1248
 
1249
		$this->assertEquals('/demos/auth', $request->base);
1250
		$this->assertEquals('/demos/auth/', $request->webroot);
1251
 
1252
		Configure::write('App.dir', 'code');
1253
 
1254
		$_SERVER['DOCUMENT_ROOT'] = '/Library/WebServer/Documents';
1255
		$_SERVER['PHP_SELF'] = '/clients/PewterReport/code/webroot/index.php';
1256
		$request = new CakeRequest();
1257
 
1258
		$this->assertEquals('/clients/PewterReport/code', $request->base);
1259
		$this->assertEquals('/clients/PewterReport/code/', $request->webroot);
1260
	}
1261
 
1262
/**
1263
 * Test baseUrl with ModRewrite alias
1264
 *
1265
 * @return void
1266
 */
1267
	public function testBaseUrlwithModRewriteAlias() {
1268
		$_SERVER['DOCUMENT_ROOT'] = '/home/aplusnur/public_html';
1269
		$_SERVER['PHP_SELF'] = '/control/index.php';
1270
 
1271
		Configure::write('App.base', '/control');
1272
 
1273
		$request = new CakeRequest();
1274
 
1275
		$this->assertEquals('/control', $request->base);
1276
		$this->assertEquals('/control/', $request->webroot);
1277
 
1278
		Configure::write('App.base', false);
1279
		Configure::write('App.dir', 'affiliate');
1280
		Configure::write('App.webroot', 'newaffiliate');
1281
 
1282
		$_SERVER['DOCUMENT_ROOT'] = '/var/www/abtravaff/html';
1283
		$_SERVER['PHP_SELF'] = '/newaffiliate/index.php';
1284
		$request = new CakeRequest();
1285
 
1286
		$this->assertEquals('/newaffiliate', $request->base);
1287
		$this->assertEquals('/newaffiliate/', $request->webroot);
1288
	}
1289
 
1290
/**
1291
 * Test base, webroot, URL and here parsing when there is URL rewriting but
1292
 * CakePHP gets called with index.php in URL nonetheless.
1293
 *
1294
 * Tests uri with
1295
 * - index.php/
1296
 * - index.php/
1297
 * - index.php/apples/
1298
 * - index.php/bananas/eat/tasty_banana
1299
 *
1300
 * @link https://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/3318
1301
 */
1302
	public function testBaseUrlWithModRewriteAndIndexPhp() {
1303
		$_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php';
1304
		$_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php';
1305
		unset($_SERVER['PATH_INFO']);
1306
		$request = new CakeRequest();
1307
 
1308
		$this->assertEquals('/cakephp', $request->base);
1309
		$this->assertEquals('/cakephp/', $request->webroot);
1310
		$this->assertEquals('', $request->url);
1311
		$this->assertEquals('/cakephp/', $request->here);
1312
 
1313
		$_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/';
1314
		$_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/';
1315
		$_SERVER['PATH_INFO'] = '/';
1316
		$request = new CakeRequest();
1317
 
1318
		$this->assertEquals('/cakephp', $request->base);
1319
		$this->assertEquals('/cakephp/', $request->webroot);
1320
		$this->assertEquals('', $request->url);
1321
		$this->assertEquals('/cakephp/', $request->here);
1322
 
1323
		$_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/apples';
1324
		$_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/apples';
1325
		$_SERVER['PATH_INFO'] = '/apples';
1326
		$request = new CakeRequest();
1327
 
1328
		$this->assertEquals('/cakephp', $request->base);
1329
		$this->assertEquals('/cakephp/', $request->webroot);
1330
		$this->assertEquals('apples', $request->url);
1331
		$this->assertEquals('/cakephp/apples', $request->here);
1332
 
1333
		$_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/melons/share/';
1334
		$_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/melons/share/';
1335
		$_SERVER['PATH_INFO'] = '/melons/share/';
1336
		$request = new CakeRequest();
1337
 
1338
		$this->assertEquals('/cakephp', $request->base);
1339
		$this->assertEquals('/cakephp/', $request->webroot);
1340
		$this->assertEquals('melons/share/', $request->url);
1341
		$this->assertEquals('/cakephp/melons/share/', $request->here);
1342
 
1343
		$_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/bananas/eat/tasty_banana';
1344
		$_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/bananas/eat/tasty_banana';
1345
		$_SERVER['PATH_INFO'] = '/bananas/eat/tasty_banana';
1346
		$request = new CakeRequest();
1347
 
1348
		$this->assertEquals('/cakephp', $request->base);
1349
		$this->assertEquals('/cakephp/', $request->webroot);
1350
		$this->assertEquals('bananas/eat/tasty_banana', $request->url);
1351
		$this->assertEquals('/cakephp/bananas/eat/tasty_banana', $request->here);
1352
	}
1353
 
1354
/**
1355
 * Test base, webroot, and URL parsing when there is no URL rewriting
1356
 *
1357
 * @return void
1358
 */
1359
	public function testBaseUrlWithNoModRewrite() {
1360
		$_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites';
1361
		$_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake/index.php';
1362
		$_SERVER['PHP_SELF'] = '/cake/index.php/posts/index';
1363
		$_SERVER['REQUEST_URI'] = '/cake/index.php/posts/index';
1364
 
1365
		Configure::write('App', array(
1366
			'dir' => APP_DIR,
1367
			'webroot' => WEBROOT_DIR,
1368
			'base' => false,
1369
			'baseUrl' => '/cake/index.php'
1370
		));
1371
 
1372
		$request = new CakeRequest();
1373
		$this->assertEquals('/cake/index.php', $request->base);
1374
		$this->assertEquals('/cake/app/webroot/', $request->webroot);
1375
		$this->assertEquals('posts/index', $request->url);
1376
	}
1377
 
1378
/**
1379
 * Test baseUrl and webroot with baseUrl
1380
 *
1381
 * @return void
1382
 */
1383
	public function testBaseUrlAndWebrootWithBaseUrl() {
1384
		Configure::write('App.dir', 'app');
1385
		Configure::write('App.baseUrl', '/app/webroot/index.php');
1386
 
1387
		$request = new CakeRequest();
1388
		$this->assertEquals('/app/webroot/index.php', $request->base);
1389
		$this->assertEquals('/app/webroot/', $request->webroot);
1390
 
1391
		Configure::write('App.baseUrl', '/app/webroot/test.php');
1392
		$request = new CakeRequest();
1393
		$this->assertEquals('/app/webroot/test.php', $request->base);
1394
		$this->assertEquals('/app/webroot/', $request->webroot);
1395
 
1396
		Configure::write('App.baseUrl', '/app/index.php');
1397
		$request = new CakeRequest();
1398
		$this->assertEquals('/app/index.php', $request->base);
1399
		$this->assertEquals('/app/webroot/', $request->webroot);
1400
 
1401
		Configure::write('App.baseUrl', '/CakeBB/app/webroot/index.php');
1402
		$request = new CakeRequest();
1403
		$this->assertEquals('/CakeBB/app/webroot/index.php', $request->base);
1404
		$this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
1405
 
1406
		Configure::write('App.baseUrl', '/CakeBB/app/index.php');
1407
		$request = new CakeRequest();
1408
 
1409
		$this->assertEquals('/CakeBB/app/index.php', $request->base);
1410
		$this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
1411
 
1412
		Configure::write('App.baseUrl', '/CakeBB/index.php');
1413
		$request = new CakeRequest();
1414
 
1415
		$this->assertEquals('/CakeBB/index.php', $request->base);
1416
		$this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
1417
 
1418
		Configure::write('App.baseUrl', '/dbhauser/index.php');
1419
		$_SERVER['DOCUMENT_ROOT'] = '/kunden/homepages/4/d181710652/htdocs/joomla';
1420
		$_SERVER['SCRIPT_FILENAME'] = '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php';
1421
		$request = new CakeRequest();
1422
 
1423
		$this->assertEquals('/dbhauser/index.php', $request->base);
1424
		$this->assertEquals('/dbhauser/app/webroot/', $request->webroot);
1425
	}
1426
 
1427
/**
1428
 * Test baseUrl with no rewrite and using the top level index.php.
1429
 *
1430
 * @return void
1431
 */
1432
	public function testBaseUrlNoRewriteTopLevelIndex() {
1433
		Configure::write('App.baseUrl', '/index.php');
1434
		$_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev';
1435
		$_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/index.php';
1436
 
1437
		$request = new CakeRequest();
1438
		$this->assertEquals('/index.php', $request->base);
1439
		$this->assertEquals('/app/webroot/', $request->webroot);
1440
	}
1441
 
1442
/**
1443
 * Check that a sub-directory containing app|webroot doesn't get mishandled when re-writing is off.
1444
 *
1445
 * @return void
1446
 */
1447
	public function testBaseUrlWithAppAndWebrootInDirname() {
1448
		Configure::write('App.baseUrl', '/approval/index.php');
1449
		$_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
1450
		$_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/approval/index.php';
1451
 
1452
		$request = new CakeRequest();
1453
		$this->assertEquals('/approval/index.php', $request->base);
1454
		$this->assertEquals('/approval/app/webroot/', $request->webroot);
1455
 
1456
		Configure::write('App.baseUrl', '/webrootable/index.php');
1457
		$_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
1458
		$_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/webrootable/index.php';
1459
 
1460
		$request = new CakeRequest();
1461
		$this->assertEquals('/webrootable/index.php', $request->base);
1462
		$this->assertEquals('/webrootable/app/webroot/', $request->webroot);
1463
	}
1464
 
1465
/**
1466
 * Test baseUrl with no rewrite, and using the app/webroot/index.php file as is normal with virtual hosts.
1467
 *
1468
 * @return void
1469
 */
1470
	public function testBaseUrlNoRewriteWebrootIndex() {
1471
		Configure::write('App.baseUrl', '/index.php');
1472
		$_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev/app/webroot';
1473
		$_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/app/webroot/index.php';
1474
 
1475
		$request = new CakeRequest();
1476
		$this->assertEquals('/index.php', $request->base);
1477
		$this->assertEquals('/', $request->webroot);
1478
	}
1479
 
1480
/**
1481
 * Test that a request with a . in the main GET parameter is filtered out.
1482
 * PHP changes GET parameter keys containing dots to _.
1483
 *
1484
 * @return void
1485
 */
1486
	public function testGetParamsWithDot() {
1487
		$_GET = array();
1488
		$_GET['/posts/index/add_add'] = '';
1489
		$_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
1490
		$_SERVER['REQUEST_URI'] = '/cake_dev/posts/index/add.add';
1491
 
1492
		$request = new CakeRequest();
1493
		$this->assertEquals(array(), $request->query);
1494
	}
1495
 
1496
/**
1497
 * Test that a request with urlencoded bits in the main GET parameter are filtered out.
1498
 *
1499
 * @return void
1500
 */
1501
	public function testGetParamWithUrlencodedElement() {
1502
		$_GET = array();
1503
		$_GET['/posts/add/∂∂'] = '';
1504
		$_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
1505
		$_SERVER['REQUEST_URI'] = '/cake_dev/posts/add/%E2%88%82%E2%88%82';
1506
 
1507
		$request = new CakeRequest();
1508
		$this->assertEquals(array(), $request->query);
1509
	}
1510
 
1511
/**
1512
 * Generator for environment configurations
1513
 *
1514
 * @return array Environment array
1515
 */
1516
	public static function environmentGenerator() {
1517
		return array(
1518
			array(
1519
				'IIS - No rewrite base path',
1520
				array(
1521
					'App' => array(
1522
						'base' => false,
1523
						'baseUrl' => '/index.php',
1524
						'dir' => 'app',
1525
						'webroot' => 'webroot'
1526
					),
1527
					'SERVER' => array(
1528
						'SCRIPT_NAME' => '/index.php',
1529
						'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
1530
						'QUERY_STRING' => '',
1531
						'REQUEST_URI' => '/index.php',
1532
						'URL' => '/index.php',
1533
						'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php',
1534
						'ORIG_PATH_INFO' => '/index.php',
1535
						'PATH_INFO' => '',
1536
						'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php',
1537
						'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
1538
						'PHP_SELF' => '/index.php',
1539
					),
1540
				),
1541
				array(
1542
					'base' => '/index.php',
1543
					'webroot' => '/app/webroot/',
1544
					'url' => ''
1545
				),
1546
			),
1547
			array(
1548
				'IIS - No rewrite with path, no PHP_SELF',
1549
				array(
1550
					'App' => array(
1551
						'base' => false,
1552
						'baseUrl' => '/index.php?',
1553
						'dir' => 'app',
1554
						'webroot' => 'webroot'
1555
					),
1556
					'SERVER' => array(
1557
						'QUERY_STRING' => '/posts/add',
1558
						'REQUEST_URI' => '/index.php?/posts/add',
1559
						'PHP_SELF' => '',
1560
						'URL' => '/index.php?/posts/add',
1561
						'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
1562
						'argv' => array('/posts/add'),
1563
						'argc' => 1
1564
					),
1565
				),
1566
				array(
1567
					'url' => 'posts/add',
1568
					'base' => '/index.php?',
1569
					'webroot' => '/app/webroot/'
1570
				)
1571
			),
1572
			array(
1573
				'IIS - No rewrite sub dir 2',
1574
				array(
1575
					'App' => array(
1576
						'base' => false,
1577
						'baseUrl' => '/site/index.php',
1578
						'dir' => 'app',
1579
						'webroot' => 'webroot',
1580
					),
1581
					'SERVER' => array(
1582
						'SCRIPT_NAME' => '/site/index.php',
1583
						'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
1584
						'QUERY_STRING' => '',
1585
						'REQUEST_URI' => '/site/index.php',
1586
						'URL' => '/site/index.php',
1587
						'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
1588
						'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
1589
						'PHP_SELF' => '/site/index.php',
1590
						'argv' => array(),
1591
						'argc' => 0
1592
					),
1593
				),
1594
				array(
1595
					'url' => '',
1596
					'base' => '/site/index.php',
1597
					'webroot' => '/site/app/webroot/'
1598
				),
1599
			),
1600
			array(
1601
				'IIS - No rewrite sub dir 2 with path',
1602
				array(
1603
					'App' => array(
1604
						'base' => false,
1605
						'baseUrl' => '/site/index.php',
1606
						'dir' => 'app',
1607
						'webroot' => 'webroot'
1608
					),
1609
					'GET' => array('/posts/add' => ''),
1610
					'SERVER' => array(
1611
						'SCRIPT_NAME' => '/site/index.php',
1612
						'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
1613
						'QUERY_STRING' => '/posts/add',
1614
						'REQUEST_URI' => '/site/index.php/posts/add',
1615
						'URL' => '/site/index.php/posts/add',
1616
						'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
1617
						'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
1618
						'PHP_SELF' => '/site/index.php/posts/add',
1619
						'argv' => array('/posts/add'),
1620
						'argc' => 1
1621
					),
1622
				),
1623
				array(
1624
					'url' => 'posts/add',
1625
					'base' => '/site/index.php',
1626
					'webroot' => '/site/app/webroot/'
1627
				)
1628
			),
1629
			array(
1630
				'Apache - No rewrite, document root set to webroot, requesting path',
1631
				array(
1632
					'App' => array(
1633
						'base' => false,
1634
						'baseUrl' => '/index.php',
1635
						'dir' => 'app',
1636
						'webroot' => 'webroot'
1637
					),
1638
					'SERVER' => array(
1639
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
1640
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
1641
						'QUERY_STRING' => '',
1642
						'REQUEST_URI' => '/index.php/posts/index',
1643
						'SCRIPT_NAME' => '/index.php',
1644
						'PATH_INFO' => '/posts/index',
1645
						'PHP_SELF' => '/index.php/posts/index',
1646
					),
1647
				),
1648
				array(
1649
					'url' => 'posts/index',
1650
					'base' => '/index.php',
1651
					'webroot' => '/'
1652
				),
1653
			),
1654
			array(
1655
				'Apache - No rewrite, document root set to webroot, requesting root',
1656
				array(
1657
					'App' => array(
1658
						'base' => false,
1659
						'baseUrl' => '/index.php',
1660
						'dir' => 'app',
1661
						'webroot' => 'webroot'
1662
					),
1663
					'SERVER' => array(
1664
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
1665
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
1666
						'QUERY_STRING' => '',
1667
						'REQUEST_URI' => '/index.php',
1668
						'SCRIPT_NAME' => '/index.php',
1669
						'PATH_INFO' => '',
1670
						'PHP_SELF' => '/index.php',
1671
					),
1672
				),
1673
				array(
1674
					'url' => '',
1675
					'base' => '/index.php',
1676
					'webroot' => '/'
1677
				),
1678
			),
1679
			array(
1680
				'Apache - No rewrite, document root set above top level cake dir, requesting path',
1681
				array(
1682
					'App' => array(
1683
						'base' => false,
1684
						'baseUrl' => '/site/index.php',
1685
						'dir' => 'app',
1686
						'webroot' => 'webroot'
1687
					),
1688
					'SERVER' => array(
1689
						'SERVER_NAME' => 'localhost',
1690
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
1691
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
1692
						'REQUEST_URI' => '/site/index.php/posts/index',
1693
						'SCRIPT_NAME' => '/site/index.php',
1694
						'PATH_INFO' => '/posts/index',
1695
						'PHP_SELF' => '/site/index.php/posts/index',
1696
					),
1697
				),
1698
				array(
1699
					'url' => 'posts/index',
1700
					'base' => '/site/index.php',
1701
					'webroot' => '/site/app/webroot/',
1702
				),
1703
			),
1704
			array(
1705
				'Apache - No rewrite, document root set above top level cake dir, request root, no PATH_INFO',
1706
				array(
1707
					'App' => array(
1708
						'base' => false,
1709
						'baseUrl' => '/site/index.php',
1710
						'dir' => 'app',
1711
						'webroot' => 'webroot'
1712
					),
1713
					'SERVER' => array(
1714
						'SERVER_NAME' => 'localhost',
1715
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
1716
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
1717
						'REQUEST_URI' => '/site/index.php/',
1718
						'SCRIPT_NAME' => '/site/index.php',
1719
						'PHP_SELF' => '/site/index.php/',
1720
					),
1721
				),
1722
				array(
1723
					'url' => '',
1724
					'base' => '/site/index.php',
1725
					'webroot' => '/site/app/webroot/',
1726
				),
1727
			),
1728
			array(
1729
				'Apache - No rewrite, document root set above top level cake dir, request path, with GET',
1730
				array(
1731
					'App' => array(
1732
						'base' => false,
1733
						'baseUrl' => '/site/index.php',
1734
						'dir' => 'app',
1735
						'webroot' => 'webroot'
1736
					),
1737
					'GET' => array('a' => 'b', 'c' => 'd'),
1738
					'SERVER' => array(
1739
						'SERVER_NAME' => 'localhost',
1740
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
1741
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
1742
						'REQUEST_URI' => '/site/index.php/posts/index?a=b&c=d',
1743
						'SCRIPT_NAME' => '/site/index.php',
1744
						'PATH_INFO' => '/posts/index',
1745
						'PHP_SELF' => '/site/index.php/posts/index',
1746
						'QUERY_STRING' => 'a=b&c=d'
1747
					),
1748
				),
1749
				array(
1750
					'urlParams' => array('a' => 'b', 'c' => 'd'),
1751
					'url' => 'posts/index',
1752
					'base' => '/site/index.php',
1753
					'webroot' => '/site/app/webroot/',
1754
				),
1755
			),
1756
			array(
1757
				'Apache - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO',
1758
				array(
1759
					'App' => array(
1760
						'base' => false,
1761
						'baseUrl' => false,
1762
						'dir' => 'app',
1763
						'webroot' => 'webroot'
1764
					),
1765
					'SERVER' => array(
1766
						'SERVER_NAME' => 'localhost',
1767
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
1768
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
1769
						'REQUEST_URI' => '/site/',
1770
						'SCRIPT_NAME' => '/site/app/webroot/index.php',
1771
						'PHP_SELF' => '/site/app/webroot/index.php',
1772
					),
1773
				),
1774
				array(
1775
					'url' => '',
1776
					'base' => '/site',
1777
					'webroot' => '/site/',
1778
				),
1779
			),
1780
			array(
1781
				'Apache - w/rewrite, document root above top level cake dir, request root, no PATH_INFO/REQUEST_URI',
1782
				array(
1783
					'App' => array(
1784
						'base' => false,
1785
						'baseUrl' => false,
1786
						'dir' => 'app',
1787
						'webroot' => 'webroot'
1788
					),
1789
					'SERVER' => array(
1790
						'SERVER_NAME' => 'localhost',
1791
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
1792
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
1793
						'SCRIPT_NAME' => '/site/app/webroot/index.php',
1794
						'PHP_SELF' => '/site/app/webroot/index.php',
1795
						'PATH_INFO' => null,
1796
						'REQUEST_URI' => null,
1797
					),
1798
				),
1799
				array(
1800
					'url' => '',
1801
					'base' => '/site',
1802
					'webroot' => '/site/',
1803
				),
1804
			),
1805
			array(
1806
				'Apache - w/rewrite, document root set to webroot, request root, no PATH_INFO/REQUEST_URI',
1807
				array(
1808
					'App' => array(
1809
						'base' => false,
1810
						'baseUrl' => false,
1811
						'dir' => 'app',
1812
						'webroot' => 'webroot'
1813
					),
1814
					'SERVER' => array(
1815
						'SERVER_NAME' => 'localhost',
1816
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
1817
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
1818
						'SCRIPT_NAME' => '/index.php',
1819
						'PHP_SELF' => '/index.php',
1820
						'PATH_INFO' => null,
1821
						'REQUEST_URI' => null,
1822
					),
1823
				),
1824
				array(
1825
					'url' => '',
1826
					'base' => '',
1827
					'webroot' => '/',
1828
				),
1829
			),
1830
			array(
1831
				'Apache - w/rewrite, document root set above top level cake dir, request root, absolute REQUEST_URI',
1832
				array(
1833
					'App' => array(
1834
						'base' => false,
1835
						'baseUrl' => false,
1836
						'dir' => 'app',
1837
						'webroot' => 'webroot'
1838
					),
1839
					'SERVER' => array(
1840
						'SERVER_NAME' => 'localhost',
1841
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
1842
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
1843
						'REQUEST_URI' => '/site/posts/index',
1844
						'SCRIPT_NAME' => '/site/app/webroot/index.php',
1845
						'PHP_SELF' => '/site/app/webroot/index.php',
1846
					),
1847
				),
1848
				array(
1849
					'url' => 'posts/index',
1850
					'base' => '/site',
1851
					'webroot' => '/site/',
1852
				),
1853
			),
1854
			array(
1855
				'Nginx - w/rewrite, document root set to webroot, request root, no PATH_INFO',
1856
				array(
1857
					'App' => array(
1858
						'base' => false,
1859
						'baseUrl' => false,
1860
						'dir' => 'app',
1861
						'webroot' => 'webroot'
1862
					),
1863
					'GET' => array('/posts/add' => ''),
1864
					'SERVER' => array(
1865
						'SERVER_NAME' => 'localhost',
1866
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
1867
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
1868
						'SCRIPT_NAME' => '/index.php',
1869
						'QUERY_STRING' => '/posts/add&',
1870
						'PHP_SELF' => '/index.php',
1871
						'PATH_INFO' => null,
1872
						'REQUEST_URI' => '/posts/add',
1873
					),
1874
				),
1875
				array(
1876
					'url' => 'posts/add',
1877
					'base' => '',
1878
					'webroot' => '/',
1879
					'urlParams' => array()
1880
				),
1881
			),
1882
		);
1883
	}
1884
 
1885
/**
1886
 * Test environment detection
1887
 *
1888
 * @dataProvider environmentGenerator
1889
 * @param $name
1890
 * @param $env
1891
 * @param $expected
1892
 * @return void
1893
 */
1894
	public function testEnvironmentDetection($name, $env, $expected) {
1895
		$_GET = array();
1896
		$this->_loadEnvironment($env);
1897
 
1898
		$request = new CakeRequest();
1899
		$this->assertEquals($expected['url'], $request->url, "url error");
1900
		$this->assertEquals($expected['base'], $request->base, "base error");
1901
		$this->assertEquals($expected['webroot'], $request->webroot, "webroot error");
1902
		if (isset($expected['urlParams'])) {
1903
			$this->assertEquals($expected['urlParams'], $request->query, "GET param mismatch");
1904
		}
1905
	}
1906
 
1907
/**
1908
 * Test the query() method
1909
 *
1910
 * @return void
1911
 */
1912
	public function testQuery() {
1913
		$_GET = array();
1914
		$_GET['foo'] = 'bar';
1915
 
1916
		$request = new CakeRequest();
1917
 
1918
		$result = $request->query('foo');
1919
		$this->assertEquals('bar', $result);
1920
 
1921
		$result = $request->query('imaginary');
1922
		$this->assertNull($result);
1923
	}
1924
 
1925
/**
1926
 * Test the query() method with arrays passed via $_GET
1927
 *
1928
 * @return void
1929
 */
1930
	public function testQueryWithArray() {
1931
		$_GET = array();
1932
		$_GET['test'] = array('foo', 'bar');
1933
 
1934
		$request = new CakeRequest();
1935
 
1936
		$result = $request->query('test');
1937
		$this->assertEquals(array('foo', 'bar'), $result);
1938
 
1939
		$result = $request->query('test.1');
1940
		$this->assertEquals('bar', $result);
1941
 
1942
		$result = $request->query('test.2');
1943
		$this->assertNull($result);
1944
	}
1945
 
1946
/**
1947
 * Test using param()
1948
 *
1949
 * @return void
1950
 */
1951
	public function testReadingParams() {
1952
		$request = new CakeRequest();
1953
		$request->addParams(array(
1954
			'controller' => 'posts',
1955
			'admin' => true,
1956
			'truthy' => 1,
1957
			'zero' => '0',
1958
		));
1959
		$this->assertFalse($request->param('not_set'));
1960
		$this->assertTrue($request->param('admin'));
1961
		$this->assertEquals(1, $request->param('truthy'));
1962
		$this->assertEquals('posts', $request->param('controller'));
1963
		$this->assertEquals('0', $request->param('zero'));
1964
	}
1965
 
1966
/**
1967
 * Test the data() method reading
1968
 *
1969
 * @return void
1970
 */
1971
	public function testDataReading() {
1972
		$_POST['data'] = array(
1973
			'Model' => array(
1974
				'field' => 'value'
1975
			)
1976
		);
1977
		$request = new CakeRequest('posts/index');
1978
		$result = $request->data('Model');
1979
		$this->assertEquals($_POST['data']['Model'], $result);
1980
 
1981
		$result = $request->data('Model.imaginary');
1982
		$this->assertNull($result);
1983
	}
1984
 
1985
/**
1986
 * Test writing with data()
1987
 *
1988
 * @return void
1989
 */
1990
	public function testDataWriting() {
1991
		$_POST['data'] = array(
1992
			'Model' => array(
1993
				'field' => 'value'
1994
			)
1995
		);
1996
		$request = new CakeRequest('posts/index');
1997
		$result = $request->data('Model.new_value', 'new value');
1998
		$this->assertSame($result, $request, 'Return was not $this');
1999
 
2000
		$this->assertEquals('new value', $request->data['Model']['new_value']);
2001
 
2002
		$request->data('Post.title', 'New post')->data('Comment.1.author', 'Mark');
2003
		$this->assertEquals('New post', $request->data['Post']['title']);
2004
		$this->assertEquals('Mark', $request->data['Comment']['1']['author']);
2005
	}
2006
 
2007
/**
2008
 * Test writing falsey values.
2009
 *
2010
 * @return void
2011
 */
2012
	public function testDataWritingFalsey() {
2013
		$request = new CakeRequest('posts/index');
2014
 
2015
		$request->data('Post.null', null);
2016
		$this->assertNull($request->data['Post']['null']);
2017
 
2018
		$request->data('Post.false', false);
2019
		$this->assertFalse($request->data['Post']['false']);
2020
 
2021
		$request->data('Post.zero', 0);
2022
		$this->assertSame(0, $request->data['Post']['zero']);
2023
 
2024
		$request->data('Post.empty', '');
2025
		$this->assertSame('', $request->data['Post']['empty']);
2026
	}
2027
 
2028
/**
2029
 * Test accept language
2030
 *
2031
 * @return void
2032
 */
2033
	public function testAcceptLanguage() {
2034
		// Weird language
2035
		$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
2036
		$result = CakeRequest::acceptLanguage();
2037
		$this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
2038
 
2039
		// No qualifier
2040
		$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
2041
		$result = CakeRequest::acceptLanguage();
2042
		$this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
2043
 
2044
		// With qualifier
2045
		$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4';
2046
		$result = CakeRequest::acceptLanguage();
2047
		$this->assertEquals(array('en-us', 'en', 'pt-br', 'pt'), $result, 'Languages do not match');
2048
 
2049
		// With spaces
2050
		$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'da, en-gb;q=0.8, en;q=0.7';
2051
		$result = CakeRequest::acceptLanguage();
2052
		$this->assertEquals(array('da', 'en-gb', 'en'), $result, 'Languages do not match');
2053
 
2054
		// Checking if requested
2055
		$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
2056
		$result = CakeRequest::acceptLanguage();
2057
 
2058
		$result = CakeRequest::acceptLanguage('en-ca');
2059
		$this->assertTrue($result);
2060
 
2061
		$result = CakeRequest::acceptLanguage('en-CA');
2062
		$this->assertTrue($result);
2063
 
2064
		$result = CakeRequest::acceptLanguage('en-us');
2065
		$this->assertFalse($result);
2066
 
2067
		$result = CakeRequest::acceptLanguage('en-US');
2068
		$this->assertFalse($result);
2069
	}
2070
 
2071
/**
2072
 * Test the here() method
2073
 *
2074
 * @return void
2075
 */
2076
	public function testHere() {
2077
		Configure::write('App.base', '/base_path');
2078
		$_GET = array('test' => 'value');
2079
		$request = new CakeRequest('/posts/add/1/name:value');
2080
 
2081
		$result = $request->here();
2082
		$this->assertEquals('/base_path/posts/add/1/name:value?test=value', $result);
2083
 
2084
		$result = $request->here(false);
2085
		$this->assertEquals('/posts/add/1/name:value?test=value', $result);
2086
 
2087
		$request = new CakeRequest('/posts/base_path/1/name:value');
2088
		$result = $request->here();
2089
		$this->assertEquals('/base_path/posts/base_path/1/name:value?test=value', $result);
2090
 
2091
		$result = $request->here(false);
2092
		$this->assertEquals('/posts/base_path/1/name:value?test=value', $result);
2093
	}
2094
 
2095
/**
2096
 * Test the input() method.
2097
 *
2098
 * @return void
2099
 */
2100
	public function testInput() {
2101
		$request = $this->getMock('CakeRequest', array('_readInput'));
2102
		$request->expects($this->once())->method('_readInput')
2103
			->will($this->returnValue('I came from stdin'));
2104
 
2105
		$result = $request->input();
2106
		$this->assertEquals('I came from stdin', $result);
2107
	}
2108
 
2109
/**
2110
 * Test input() decoding.
2111
 *
2112
 * @return void
2113
 */
2114
	public function testInputDecode() {
2115
		$request = $this->getMock('CakeRequest', array('_readInput'));
2116
		$request->expects($this->once())->method('_readInput')
2117
			->will($this->returnValue('{"name":"value"}'));
2118
 
2119
		$result = $request->input('json_decode');
2120
		$this->assertEquals(array('name' => 'value'), (array)$result);
2121
	}
2122
 
2123
/**
2124
 * Test input() decoding with additional arguments.
2125
 *
2126
 * @return void
2127
 */
2128
	public function testInputDecodeExtraParams() {
2129
		$xml = <<<XML
2130
<?xml version="1.0" encoding="utf-8"?>
2131
<post>
2132
	<title id="title">Test</title>
2133
</post>
2134
XML;
2135
 
2136
		$request = $this->getMock('CakeRequest', array('_readInput'));
2137
		$request->expects($this->once())->method('_readInput')
2138
			->will($this->returnValue($xml));
2139
 
2140
		$result = $request->input('Xml::build', array('return' => 'domdocument'));
2141
		$this->assertInstanceOf('DOMDocument', $result);
2142
		$this->assertEquals(
2143
			'Test',
2144
			$result->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText
2145
		);
2146
	}
2147
 
2148
/**
2149
 * Test is('requested') and isRequested()
2150
 *
2151
 * @return void
2152
 */
2153
	public function testIsRequested() {
2154
		$request = new CakeRequest('/posts/index');
2155
		$request->addParams(array(
2156
			'controller' => 'posts',
2157
			'action' => 'index',
2158
			'plugin' => null,
2159
			'requested' => 1
2160
		));
2161
		$this->assertTrue($request->is('requested'));
2162
		$this->assertTrue($request->isRequested());
2163
 
2164
		$request = new CakeRequest('/posts/index');
2165
		$request->addParams(array(
2166
			'controller' => 'posts',
2167
			'action' => 'index',
2168
			'plugin' => null,
2169
		));
2170
		$this->assertFalse($request->is('requested'));
2171
		$this->assertFalse($request->isRequested());
2172
	}
2173
 
2174
/**
2175
 * Test onlyAllow method
2176
 *
2177
 * @return void
2178
 */
2179
	public function testOnlyAllow() {
2180
		$_SERVER['REQUEST_METHOD'] = 'PUT';
2181
		$request = new CakeRequest('/posts/edit/1');
2182
 
2183
		$this->assertTrue($request->onlyAllow(array('put')));
2184
 
2185
		$_SERVER['REQUEST_METHOD'] = 'DELETE';
2186
		$this->assertTrue($request->onlyAllow('post', 'delete'));
2187
	}
2188
 
2189
/**
2190
 * Test onlyAllow throwing exception
2191
 *
2192
 * @return void
2193
 */
2194
	public function testOnlyAllowException() {
2195
		$_SERVER['REQUEST_METHOD'] = 'PUT';
2196
		$request = new CakeRequest('/posts/edit/1');
2197
 
2198
		try {
2199
			$request->onlyAllow('POST', 'DELETE');
2200
			$this->fail('An expected exception has not been raised.');
2201
		} catch (MethodNotAllowedException $e) {
2202
			$this->assertEquals(array('Allow' => 'POST, DELETE'), $e->responseHeader());
2203
		}
2204
 
2205
		$this->setExpectedException('MethodNotAllowedException');
2206
		$request->onlyAllow('POST');
2207
	}
2208
 
2209
/**
2210
 * loadEnvironment method
2211
 *
2212
 * @param array $env
2213
 * @return void
2214
 */
2215
	protected function _loadEnvironment($env) {
2216
		if (isset($env['App'])) {
2217
			Configure::write('App', $env['App']);
2218
		}
2219
 
2220
		if (isset($env['GET'])) {
2221
			foreach ($env['GET'] as $key => $val) {
2222
				$_GET[$key] = $val;
2223
			}
2224
		}
2225
 
2226
		if (isset($env['POST'])) {
2227
			foreach ($env['POST'] as $key => $val) {
2228
				$_POST[$key] = $val;
2229
			}
2230
		}
2231
 
2232
		if (isset($env['SERVER'])) {
2233
			foreach ($env['SERVER'] as $key => $val) {
2234
				$_SERVER[$key] = $val;
2235
			}
2236
		}
2237
	}
2238
 
2239
}