Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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