Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 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 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
 * @return void
617
 */
618
	public function testFilesZeroithIndex() {
619
		$_FILES = array(
620
 
621
				'name' => 'cake_sqlserver_patch.patch',
622
				'type' => 'text/plain',
623
				'tmp_name' => '/private/var/tmp/phpy05Ywj',
624
				'error' => 0,
625
				'size' => 6271,
626
			),
627
		);
628
 
629
		$request = new CakeRequest('some/path');
630
		$this->assertEquals($_FILES, $request->params['form']);
631
	}
632
 
633
/**
634
 * Test method overrides coming in from POST data.
635
 *
636
 * @return void
637
 */
638
	public function testMethodOverrides() {
639
		$_POST = array('_method' => 'POST');
640
		$request = new CakeRequest('some/path');
641
		$this->assertEquals(env('REQUEST_METHOD'), 'POST');
642
 
643
		$_POST = array('_method' => 'DELETE');
644
		$request = new CakeRequest('some/path');
645
		$this->assertEquals(env('REQUEST_METHOD'), 'DELETE');
646
 
647
		$_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
648
		$request = new CakeRequest('some/path');
649
		$this->assertEquals(env('REQUEST_METHOD'), 'PUT');
650
	}
651
 
652
/**
653
 * Test the clientIp method.
654
 *
655
 * @return void
656
 */
657
	public function testclientIp() {
658
		$_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.5, 10.0.1.1, proxy.com';
659
		$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
660
		$_SERVER['REMOTE_ADDR'] = '192.168.1.3';
661
		$request = new CakeRequest('some/path');
662
		$this->assertEquals('192.168.1.5', $request->clientIp(false));
663
		$this->assertEquals('192.168.1.2', $request->clientIp());
664
 
665
		unset($_SERVER['HTTP_X_FORWARDED_FOR']);
666
		$this->assertEquals('192.168.1.2', $request->clientIp());
667
 
668
		unset($_SERVER['HTTP_CLIENT_IP']);
669
		$this->assertEquals('192.168.1.3', $request->clientIp());
670
 
671
		$_SERVER['HTTP_CLIENTADDRESS'] = '10.0.1.2, 10.0.1.1';
672
		$this->assertEquals('10.0.1.2', $request->clientIp());
673
	}
674
 
675
/**
676
 * Test the referrer function.
677
 *
678
 * @return void
679
 */
680
	public function testReferer() {
681
		$request = new CakeRequest('some/path');
682
		$request->webroot = '/';
683
 
684
		$_SERVER['HTTP_REFERER'] = 'http://cakephp.org';
685
		$result = $request->referer();
686
		$this->assertSame($result, 'http://cakephp.org');
687
 
688
		$_SERVER['HTTP_REFERER'] = '';
689
		$result = $request->referer();
690
		$this->assertSame($result, '/');
691
 
692
		$_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '/some/path';
693
		$result = $request->referer(true);
694
		$this->assertSame($result, '/some/path');
695
 
696
		$_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '/some/path';
697
		$result = $request->referer(false);
698
		$this->assertSame($result, Configure::read('App.fullBaseUrl') . '/some/path');
699
 
700
		$_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '/recipes/add';
701
		$result = $request->referer(true);
702
		$this->assertSame($result, '/recipes/add');
703
	}
704
 
705
/**
706
 * Test referer() with a base path that duplicates the
707
 * first segment.
708
 *
709
 * @return void
710
 */
711
	public function testRefererBasePath() {
712
		$request = new CakeRequest('some/path');
713
		$request->url = 'users/login';
714
		$request->webroot = '/waves/';
715
		$request->base = '/waves';
716
		$request->here = '/waves/users/login';
717
 
718
		$_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/waves/waves/add';
719
 
720
		$result = $request->referer(true);
721
		$this->assertSame($result, '/waves/add');
722
	}
723
 
724
/**
725
 * test the simple uses of is()
726
 *
727
 * @return void
728
 */
729
	public function testIsHttpMethods() {
730
		$request = new CakeRequest('some/path');
731
 
732
		$this->assertFalse($request->is('undefined-behavior'));
733
 
734
		$_SERVER['REQUEST_METHOD'] = 'GET';
735
		$this->assertTrue($request->is('get'));
736
 
737
		$_SERVER['REQUEST_METHOD'] = 'POST';
738
		$this->assertTrue($request->is('POST'));
739
 
740
		$_SERVER['REQUEST_METHOD'] = 'PUT';
741
		$this->assertTrue($request->is('put'));
742
		$this->assertFalse($request->is('get'));
743
 
744
		$_SERVER['REQUEST_METHOD'] = 'DELETE';
745
		$this->assertTrue($request->is('delete'));
746
		$this->assertTrue($request->isDelete());
747
 
748
		$_SERVER['REQUEST_METHOD'] = 'delete';
749
		$this->assertFalse($request->is('delete'));
750
	}
751
 
752
/**
753
 * Test is() with multiple types.
754
 *
755
 * @return void
756
 */
757
	public function testIsMultiple() {
758
		$request = new CakeRequest('some/path');
759
 
760
		$_SERVER['REQUEST_METHOD'] = 'GET';
761
		$this->assertTrue($request->is(array('get', 'post')));
762
 
763
		$_SERVER['REQUEST_METHOD'] = 'POST';
764
		$this->assertTrue($request->is(array('get', 'post')));
765
 
766
		$_SERVER['REQUEST_METHOD'] = 'PUT';
767
		$this->assertFalse($request->is(array('get', 'post')));
768
	}
769
 
770
/**
771
 * Test isAll()
772
 *
773
 * @return void
774
 */
775
	public function testIsAll() {
776
		$request = new CakeRequest('some/path');
777
 
778
		$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
779
		$_SERVER['REQUEST_METHOD'] = 'GET';
780
 
781
		$this->assertTrue($request->isAll(array('ajax', 'get')));
782
		$this->assertFalse($request->isAll(array('post', 'get')));
783
		$this->assertFalse($request->isAll(array('ajax', 'post')));
784
	}
785
 
786
/**
787
 * Test the method() method.
788
 *
789
 * @return void
790
 */
791
	public function testMethod() {
792
		$_SERVER['REQUEST_METHOD'] = 'delete';
793
		$request = new CakeRequest('some/path');
794
 
795
		$this->assertEquals('delete', $request->method());
796
	}
797
 
798
/**
799
 * Test host retrieval.
800
 *
801
 * @return void
802
 */
803
	public function testHost() {
804
		$_SERVER['HTTP_HOST'] = 'localhost';
805
		$_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org';
806
		$request = new CakeRequest('some/path');
807
 
808
		$this->assertEquals('localhost', $request->host());
809
		$this->assertEquals('cakephp.org', $request->host(true));
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
		$request->addDetector('extension', array('param' => 'ext', 'options' => array('pdf', 'png', 'txt')));
1049
		$request->params['ext'] = 'pdf';
1050
		$this->assertTrue($request->is('extension'));
1051
 
1052
		$request->params['ext'] = 'exe';
1053
		$this->assertFalse($request->isExtension());
1054
	}
1055
 
1056
/**
1057
 * Helper function for testing callbacks.
1058
 *
1059
 * @param $request
1060
 * @return bool
1061
 */
1062
	public function detectCallback($request) {
1063
		return (bool)$request->return;
1064
	}
1065
 
1066
/**
1067
 * Test getting headers
1068
 *
1069
 * @return void
1070
 */
1071
	public function testHeader() {
1072
		$_SERVER['HTTP_HOST'] = 'localhost';
1073
		$_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';
1074
		$request = new CakeRequest('/', false);
1075
 
1076
		$this->assertEquals($_SERVER['HTTP_HOST'], $request->header('host'));
1077
		$this->assertEquals($_SERVER['HTTP_USER_AGENT'], $request->header('User-Agent'));
1078
	}
1079
 
1080
/**
1081
 * Test accepts() with and without parameters
1082
 *
1083
 * @return void
1084
 */
1085
	public function testAccepts() {
1086
		$_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml;q=0.9,application/xhtml+xml,text/html,text/plain,image/png';
1087
		$request = new CakeRequest('/', false);
1088
 
1089
		$result = $request->accepts();
1090
		$expected = array(
1091
			'text/xml', 'application/xhtml+xml', 'text/html', 'text/plain', 'image/png', 'application/xml'
1092
		);
1093
		$this->assertEquals($expected, $result, 'Content types differ.');
1094
 
1095
		$result = $request->accepts('text/html');
1096
		$this->assertTrue($result);
1097
 
1098
		$result = $request->accepts('image/gif');
1099
		$this->assertFalse($result);
1100
	}
1101
 
1102
/**
1103
 * Test that accept header types are trimmed for comparisons.
1104
 *
1105
 * @return void
1106
 */
1107
	public function testAcceptWithWhitespace() {
1108
		$_SERVER['HTTP_ACCEPT'] = 'text/xml  ,  text/html ,  text/plain,image/png';
1109
		$request = new CakeRequest('/', false);
1110
		$result = $request->accepts();
1111
		$expected = array(
1112
			'text/xml', 'text/html', 'text/plain', 'image/png'
1113
		);
1114
		$this->assertEquals($expected, $result, 'Content types differ.');
1115
 
1116
		$this->assertTrue($request->accepts('text/html'));
1117
	}
1118
 
1119
/**
1120
 * Content types from accepts() should respect the client's q preference values.
1121
 *
1122
 * @return void
1123
 */
1124
	public function testAcceptWithQvalueSorting() {
1125
		$_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0';
1126
		$request = new CakeRequest('/', false);
1127
		$result = $request->accepts();
1128
		$expected = array('application/xml', 'text/html', 'application/json');
1129
		$this->assertEquals($expected, $result);
1130
	}
1131
 
1132
/**
1133
 * Test the raw parsing of accept headers into the q value formatting.
1134
 *
1135
 * @return void
1136
 */
1137
	public function testParseAcceptWithQValue() {
1138
		$_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0,image/png';
1139
		$request = new CakeRequest('/', false);
1140
		$result = $request->parseAccept();
1141
		$expected = array(
1142
			'1.0' => array('application/xml', 'image/png'),
1143
			'0.8' => array('text/html'),
1144
			'0.7' => array('application/json'),
1145
		);
1146
		$this->assertEquals($expected, $result);
1147
	}
1148
 
1149
/**
1150
 * Test parsing accept with a confusing accept value.
1151
 *
1152
 * @return void
1153
 */
1154
	public function testParseAcceptNoQValues() {
1155
		$_SERVER['HTTP_ACCEPT'] = 'application/json, text/plain, */*';
1156
 
1157
		$request = new CakeRequest('/', false);
1158
		$result = $request->parseAccept();
1159
		$expected = array(
1160
			'1.0' => array('application/json', 'text/plain', '*/*'),
1161
		);
1162
		$this->assertEquals($expected, $result);
1163
	}
1164
 
1165
/**
1166
 * Test parsing accept ignores index param
1167
 *
1168
 * @return void
1169
 */
1170
	public function testParseAcceptIgnoreAcceptExtensions() {
1171
		$_SERVER['HTTP_ACCEPT'] = 'application/json;level=1, text/plain, */*';
1172
 
1173
		$request = new CakeRequest('/', false);
1174
		$result = $request->parseAccept();
1175
		$expected = array(
1176
			'1.0' => array('application/json', 'text/plain', '*/*'),
1177
		);
1178
		$this->assertEquals($expected, $result);
1179
	}
1180
 
1181
/**
1182
 * Test that parsing accept headers with invalid syntax works.
1183
 *
1184
 * The header used is missing a q value for application/xml.
1185
 *
1186
 * @return void
1187
 */
1188
	public function testParseAcceptInvalidSyntax() {
1189
		$_SERVER['HTTP_ACCEPT'] = 'text/html,application/xhtml+xml,application/xml;image/png,image/jpeg,image/*;q=0.9,*/*;q=0.8';
1190
		$request = new CakeRequest('/', false);
1191
		$result = $request->parseAccept();
1192
		$expected = array(
1193
			'1.0' => array('text/html', 'application/xhtml+xml', 'application/xml', 'image/jpeg'),
1194
			'0.9' => array('image/*'),
1195
			'0.8' => array('*/*'),
1196
		);
1197
		$this->assertEquals($expected, $result);
1198
	}
1199
 
1200
/**
1201
 * Test baseUrl and webroot with ModRewrite
1202
 *
1203
 * @return void
1204
 */
1205
	public function testBaseUrlAndWebrootWithModRewrite() {
1206
		Configure::write('App.baseUrl', false);
1207
 
1208
		$_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
1209
		$_SERVER['PHP_SELF'] = '/urlencode me/app/webroot/index.php';
1210
		$_SERVER['PATH_INFO'] = '/posts/view/1';
1211
 
1212
		$request = new CakeRequest();
1213
		$this->assertEquals('/urlencode%20me', $request->base);
1214
		$this->assertEquals('/urlencode%20me/', $request->webroot);
1215
		$this->assertEquals('posts/view/1', $request->url);
1216
 
1217
		$_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
1218
		$_SERVER['PHP_SELF'] = '/1.2.x.x/app/webroot/index.php';
1219
		$_SERVER['PATH_INFO'] = '/posts/view/1';
1220
 
1221
		$request = new CakeRequest();
1222
		$this->assertEquals('/1.2.x.x', $request->base);
1223
		$this->assertEquals('/1.2.x.x/', $request->webroot);
1224
		$this->assertEquals('posts/view/1', $request->url);
1225
 
1226
		$_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/app/webroot';
1227
		$_SERVER['PHP_SELF'] = '/index.php';
1228
		$_SERVER['PATH_INFO'] = '/posts/add';
1229
		$request = new CakeRequest();
1230
 
1231
		$this->assertEquals('', $request->base);
1232
		$this->assertEquals('/', $request->webroot);
1233
		$this->assertEquals('posts/add', $request->url);
1234
 
1235
		$_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/test/';
1236
		$_SERVER['PHP_SELF'] = '/webroot/index.php';
1237
		$request = new CakeRequest();
1238
 
1239
		$this->assertEquals('', $request->base);
1240
		$this->assertEquals('/', $request->webroot);
1241
 
1242
		$_SERVER['DOCUMENT_ROOT'] = '/some/apps/where';
1243
		$_SERVER['PHP_SELF'] = '/app/webroot/index.php';
1244
		$request = new CakeRequest();
1245
 
1246
		$this->assertEquals('', $request->base);
1247
		$this->assertEquals('/', $request->webroot);
1248
 
1249
		Configure::write('App.dir', 'auth');
1250
 
1251
		$_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
1252
		$_SERVER['PHP_SELF'] = '/demos/auth/webroot/index.php';
1253
 
1254
		$request = new CakeRequest();
1255
 
1256
		$this->assertEquals('/demos/auth', $request->base);
1257
		$this->assertEquals('/demos/auth/', $request->webroot);
1258
 
1259
		Configure::write('App.dir', 'code');
1260
 
1261
		$_SERVER['DOCUMENT_ROOT'] = '/Library/WebServer/Documents';
1262
		$_SERVER['PHP_SELF'] = '/clients/PewterReport/code/webroot/index.php';
1263
		$request = new CakeRequest();
1264
 
1265
		$this->assertEquals('/clients/PewterReport/code', $request->base);
1266
		$this->assertEquals('/clients/PewterReport/code/', $request->webroot);
1267
	}
1268
 
1269
/**
1270
 * Test baseUrl with ModRewrite alias
1271
 *
1272
 * @return void
1273
 */
1274
	public function testBaseUrlwithModRewriteAlias() {
1275
		$_SERVER['DOCUMENT_ROOT'] = '/home/aplusnur/public_html';
1276
		$_SERVER['PHP_SELF'] = '/control/index.php';
1277
 
1278
		Configure::write('App.base', '/control');
1279
 
1280
		$request = new CakeRequest();
1281
 
1282
		$this->assertEquals('/control', $request->base);
1283
		$this->assertEquals('/control/', $request->webroot);
1284
 
1285
		Configure::write('App.base', false);
1286
		Configure::write('App.dir', 'affiliate');
1287
		Configure::write('App.webroot', 'newaffiliate');
1288
 
1289
		$_SERVER['DOCUMENT_ROOT'] = '/var/www/abtravaff/html';
1290
		$_SERVER['PHP_SELF'] = '/newaffiliate/index.php';
1291
		$request = new CakeRequest();
1292
 
1293
		$this->assertEquals('/newaffiliate', $request->base);
1294
		$this->assertEquals('/newaffiliate/', $request->webroot);
1295
	}
1296
 
1297
/**
1298
 * Test base, webroot, URL and here parsing when there is URL rewriting but
1299
 * CakePHP gets called with index.php in URL nonetheless.
1300
 *
1301
 * Tests uri with
1302
 * - index.php/
1303
 * - index.php/
1304
 * - index.php/apples/
1305
 * - index.php/bananas/eat/tasty_banana
1306
 *
1307
 * @link https://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/3318
1308
 * @return void
1309
 */
1310
	public function testBaseUrlWithModRewriteAndIndexPhp() {
1311
		$_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php';
1312
		$_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php';
1313
		unset($_SERVER['PATH_INFO']);
1314
		$request = new CakeRequest();
1315
 
1316
		$this->assertEquals('/cakephp', $request->base);
1317
		$this->assertEquals('/cakephp/', $request->webroot);
1318
		$this->assertEquals('', $request->url);
1319
		$this->assertEquals('/cakephp/', $request->here);
1320
 
1321
		$_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/';
1322
		$_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/';
1323
		$_SERVER['PATH_INFO'] = '/';
1324
		$request = new CakeRequest();
1325
 
1326
		$this->assertEquals('/cakephp', $request->base);
1327
		$this->assertEquals('/cakephp/', $request->webroot);
1328
		$this->assertEquals('', $request->url);
1329
		$this->assertEquals('/cakephp/', $request->here);
1330
 
1331
		$_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/apples';
1332
		$_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/apples';
1333
		$_SERVER['PATH_INFO'] = '/apples';
1334
		$request = new CakeRequest();
1335
 
1336
		$this->assertEquals('/cakephp', $request->base);
1337
		$this->assertEquals('/cakephp/', $request->webroot);
1338
		$this->assertEquals('apples', $request->url);
1339
		$this->assertEquals('/cakephp/apples', $request->here);
1340
 
1341
		$_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/melons/share/';
1342
		$_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/melons/share/';
1343
		$_SERVER['PATH_INFO'] = '/melons/share/';
1344
		$request = new CakeRequest();
1345
 
1346
		$this->assertEquals('/cakephp', $request->base);
1347
		$this->assertEquals('/cakephp/', $request->webroot);
1348
		$this->assertEquals('melons/share/', $request->url);
1349
		$this->assertEquals('/cakephp/melons/share/', $request->here);
1350
 
1351
		$_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/bananas/eat/tasty_banana';
1352
		$_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/bananas/eat/tasty_banana';
1353
		$_SERVER['PATH_INFO'] = '/bananas/eat/tasty_banana';
1354
		$request = new CakeRequest();
1355
 
1356
		$this->assertEquals('/cakephp', $request->base);
1357
		$this->assertEquals('/cakephp/', $request->webroot);
1358
		$this->assertEquals('bananas/eat/tasty_banana', $request->url);
1359
		$this->assertEquals('/cakephp/bananas/eat/tasty_banana', $request->here);
1360
	}
1361
 
1362
/**
1363
 * Test base, webroot, and URL parsing when there is no URL rewriting
1364
 *
1365
 * @return void
1366
 */
1367
	public function testBaseUrlWithNoModRewrite() {
1368
		$_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites';
1369
		$_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake/index.php';
1370
		$_SERVER['PHP_SELF'] = '/cake/index.php/posts/index';
1371
		$_SERVER['REQUEST_URI'] = '/cake/index.php/posts/index';
1372
 
1373
		Configure::write('App', array(
1374
			'dir' => APP_DIR,
1375
			'webroot' => WEBROOT_DIR,
1376
			'base' => false,
1377
			'baseUrl' => '/cake/index.php'
1378
		));
1379
 
1380
		$request = new CakeRequest();
1381
		$this->assertEquals('/cake/index.php', $request->base);
1382
		$this->assertEquals('/cake/app/webroot/', $request->webroot);
1383
		$this->assertEquals('posts/index', $request->url);
1384
	}
1385
 
1386
/**
1387
 * Test baseUrl and webroot with baseUrl
1388
 *
1389
 * @return void
1390
 */
1391
	public function testBaseUrlAndWebrootWithBaseUrl() {
1392
		Configure::write('App.dir', 'app');
1393
		Configure::write('App.baseUrl', '/app/webroot/index.php');
1394
 
1395
		$request = new CakeRequest();
1396
		$this->assertEquals('/app/webroot/index.php', $request->base);
1397
		$this->assertEquals('/app/webroot/', $request->webroot);
1398
 
1399
		Configure::write('App.baseUrl', '/app/webroot/test.php');
1400
		$request = new CakeRequest();
1401
		$this->assertEquals('/app/webroot/test.php', $request->base);
1402
		$this->assertEquals('/app/webroot/', $request->webroot);
1403
 
1404
		Configure::write('App.baseUrl', '/app/index.php');
1405
		$request = new CakeRequest();
1406
		$this->assertEquals('/app/index.php', $request->base);
1407
		$this->assertEquals('/app/webroot/', $request->webroot);
1408
 
1409
		Configure::write('App.baseUrl', '/CakeBB/app/webroot/index.php');
1410
		$request = new CakeRequest();
1411
		$this->assertEquals('/CakeBB/app/webroot/index.php', $request->base);
1412
		$this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
1413
 
1414
		Configure::write('App.baseUrl', '/CakeBB/app/index.php');
1415
		$request = new CakeRequest();
1416
 
1417
		$this->assertEquals('/CakeBB/app/index.php', $request->base);
1418
		$this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
1419
 
1420
		Configure::write('App.baseUrl', '/CakeBB/index.php');
1421
		$request = new CakeRequest();
1422
 
1423
		$this->assertEquals('/CakeBB/index.php', $request->base);
1424
		$this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
1425
 
1426
		Configure::write('App.baseUrl', '/dbhauser/index.php');
1427
		$_SERVER['DOCUMENT_ROOT'] = '/kunden/homepages/4/d181710652/htdocs/joomla';
1428
		$_SERVER['SCRIPT_FILENAME'] = '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php';
1429
		$request = new CakeRequest();
1430
 
1431
		$this->assertEquals('/dbhauser/index.php', $request->base);
1432
		$this->assertEquals('/dbhauser/app/webroot/', $request->webroot);
1433
	}
1434
 
1435
/**
1436
 * Test baseUrl with no rewrite and using the top level index.php.
1437
 *
1438
 * @return void
1439
 */
1440
	public function testBaseUrlNoRewriteTopLevelIndex() {
1441
		Configure::write('App.baseUrl', '/index.php');
1442
		$_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev';
1443
		$_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/index.php';
1444
 
1445
		$request = new CakeRequest();
1446
		$this->assertEquals('/index.php', $request->base);
1447
		$this->assertEquals('/app/webroot/', $request->webroot);
1448
	}
1449
 
1450
/**
1451
 * Check that a sub-directory containing app|webroot doesn't get mishandled when re-writing is off.
1452
 *
1453
 * @return void
1454
 */
1455
	public function testBaseUrlWithAppAndWebrootInDirname() {
1456
		Configure::write('App.baseUrl', '/approval/index.php');
1457
		$_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
1458
		$_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/approval/index.php';
1459
 
1460
		$request = new CakeRequest();
1461
		$this->assertEquals('/approval/index.php', $request->base);
1462
		$this->assertEquals('/approval/app/webroot/', $request->webroot);
1463
 
1464
		Configure::write('App.baseUrl', '/webrootable/index.php');
1465
		$_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
1466
		$_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/webrootable/index.php';
1467
 
1468
		$request = new CakeRequest();
1469
		$this->assertEquals('/webrootable/index.php', $request->base);
1470
		$this->assertEquals('/webrootable/app/webroot/', $request->webroot);
1471
	}
1472
 
1473
/**
1474
 * Test baseUrl with no rewrite, and using the app/webroot/index.php file as is normal with virtual hosts.
1475
 *
1476
 * @return void
1477
 */
1478
	public function testBaseUrlNoRewriteWebrootIndex() {
1479
		Configure::write('App.baseUrl', '/index.php');
1480
		$_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev/app/webroot';
1481
		$_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/app/webroot/index.php';
1482
 
1483
		$request = new CakeRequest();
1484
		$this->assertEquals('/index.php', $request->base);
1485
		$this->assertEquals('/', $request->webroot);
1486
	}
1487
 
1488
/**
1489
 * Test that a request with a . in the main GET parameter is filtered out.
1490
 * PHP changes GET parameter keys containing dots to _.
1491
 *
1492
 * @return void
1493
 */
1494
	public function testGetParamsWithDot() {
1495
		$_GET = array();
1496
		$_GET['/posts/index/add_add'] = '';
1497
		$_SERVER['PHP_SELF'] = '/app/webroot/index.php';
1498
		$_SERVER['REQUEST_URI'] = '/posts/index/add.add';
1499
		$request = new CakeRequest();
1500
		$this->assertEquals('', $request->base);
1501
		$this->assertEquals(array(), $request->query);
1502
 
1503
		$_GET = array();
1504
		$_GET['/cake_dev/posts/index/add_add'] = '';
1505
		$_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
1506
		$_SERVER['REQUEST_URI'] = '/cake_dev/posts/index/add.add';
1507
		$request = new CakeRequest();
1508
		$this->assertEquals('/cake_dev', $request->base);
1509
		$this->assertEquals(array(), $request->query);
1510
	}
1511
 
1512
/**
1513
 * Test that a request with urlencoded bits in the main GET parameter are filtered out.
1514
 *
1515
 * @return void
1516
 */
1517
	public function testGetParamWithUrlencodedElement() {
1518
		$_GET = array();
1519
		$_GET['/posts/add/∂∂'] = '';
1520
		$_SERVER['PHP_SELF'] = '/app/webroot/index.php';
1521
		$_SERVER['REQUEST_URI'] = '/posts/add/%E2%88%82%E2%88%82';
1522
		$request = new CakeRequest();
1523
		$this->assertEquals('', $request->base);
1524
		$this->assertEquals(array(), $request->query);
1525
 
1526
		$_GET = array();
1527
		$_GET['/cake_dev/posts/add/∂∂'] = '';
1528
		$_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
1529
		$_SERVER['REQUEST_URI'] = '/cake_dev/posts/add/%E2%88%82%E2%88%82';
1530
		$request = new CakeRequest();
1531
		$this->assertEquals('/cake_dev', $request->base);
1532
		$this->assertEquals(array(), $request->query);
1533
	}
1534
 
1535
/**
1536
 * Generator for environment configurations
1537
 *
1538
 * @return array Environment array
1539
 */
1540
	public static function environmentGenerator() {
1541
		return array(
1542
			array(
1543
				'IIS - No rewrite base path',
1544
				array(
1545
					'App' => array(
1546
						'base' => false,
1547
						'baseUrl' => '/index.php',
1548
						'dir' => 'app',
1549
						'webroot' => 'webroot'
1550
					),
1551
					'SERVER' => array(
1552
						'SCRIPT_NAME' => '/index.php',
1553
						'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
1554
						'QUERY_STRING' => '',
1555
						'REQUEST_URI' => '/index.php',
1556
						'URL' => '/index.php',
1557
						'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php',
1558
						'ORIG_PATH_INFO' => '/index.php',
1559
						'PATH_INFO' => '',
1560
						'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php',
1561
						'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
1562
						'PHP_SELF' => '/index.php',
1563
					),
1564
				),
1565
				array(
1566
					'base' => '/index.php',
1567
					'webroot' => '/app/webroot/',
1568
					'url' => ''
1569
				),
1570
			),
1571
			array(
1572
				'IIS - No rewrite with path, no PHP_SELF',
1573
				array(
1574
					'App' => array(
1575
						'base' => false,
1576
						'baseUrl' => '/index.php?',
1577
						'dir' => 'app',
1578
						'webroot' => 'webroot'
1579
					),
1580
					'SERVER' => array(
1581
						'QUERY_STRING' => '/posts/add',
1582
						'REQUEST_URI' => '/index.php?/posts/add',
1583
						'PHP_SELF' => '',
1584
						'URL' => '/index.php?/posts/add',
1585
						'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
1586
						'argv' => array('/posts/add'),
1587
						'argc' => 1
1588
					),
1589
				),
1590
				array(
1591
					'url' => 'posts/add',
1592
					'base' => '/index.php?',
1593
					'webroot' => '/app/webroot/'
1594
				)
1595
			),
1596
			array(
1597
				'IIS - No rewrite sub dir 2',
1598
				array(
1599
					'App' => array(
1600
						'base' => false,
1601
						'baseUrl' => '/site/index.php',
1602
						'dir' => 'app',
1603
						'webroot' => 'webroot',
1604
					),
1605
					'SERVER' => array(
1606
						'SCRIPT_NAME' => '/site/index.php',
1607
						'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
1608
						'QUERY_STRING' => '',
1609
						'REQUEST_URI' => '/site/index.php',
1610
						'URL' => '/site/index.php',
1611
						'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
1612
						'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
1613
						'PHP_SELF' => '/site/index.php',
1614
						'argv' => array(),
1615
						'argc' => 0
1616
					),
1617
				),
1618
				array(
1619
					'url' => '',
1620
					'base' => '/site/index.php',
1621
					'webroot' => '/site/app/webroot/'
1622
				),
1623
			),
1624
			array(
1625
				'IIS - No rewrite sub dir 2 with path',
1626
				array(
1627
					'App' => array(
1628
						'base' => false,
1629
						'baseUrl' => '/site/index.php',
1630
						'dir' => 'app',
1631
						'webroot' => 'webroot'
1632
					),
1633
					'GET' => array('/posts/add' => ''),
1634
					'SERVER' => array(
1635
						'SCRIPT_NAME' => '/site/index.php',
1636
						'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
1637
						'QUERY_STRING' => '/posts/add',
1638
						'REQUEST_URI' => '/site/index.php/posts/add',
1639
						'URL' => '/site/index.php/posts/add',
1640
						'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
1641
						'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
1642
						'PHP_SELF' => '/site/index.php/posts/add',
1643
						'argv' => array('/posts/add'),
1644
						'argc' => 1
1645
					),
1646
				),
1647
				array(
1648
					'url' => 'posts/add',
1649
					'base' => '/site/index.php',
1650
					'webroot' => '/site/app/webroot/'
1651
				)
1652
			),
1653
			array(
1654
				'Apache - No rewrite, document root set to webroot, requesting path',
1655
				array(
1656
					'App' => array(
1657
						'base' => false,
1658
						'baseUrl' => '/index.php',
1659
						'dir' => 'app',
1660
						'webroot' => 'webroot'
1661
					),
1662
					'SERVER' => array(
1663
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
1664
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
1665
						'QUERY_STRING' => '',
1666
						'REQUEST_URI' => '/index.php/posts/index',
1667
						'SCRIPT_NAME' => '/index.php',
1668
						'PATH_INFO' => '/posts/index',
1669
						'PHP_SELF' => '/index.php/posts/index',
1670
					),
1671
				),
1672
				array(
1673
					'url' => 'posts/index',
1674
					'base' => '/index.php',
1675
					'webroot' => '/'
1676
				),
1677
			),
1678
			array(
1679
				'Apache - No rewrite, document root set to webroot, requesting root',
1680
				array(
1681
					'App' => array(
1682
						'base' => false,
1683
						'baseUrl' => '/index.php',
1684
						'dir' => 'app',
1685
						'webroot' => 'webroot'
1686
					),
1687
					'SERVER' => array(
1688
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
1689
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
1690
						'QUERY_STRING' => '',
1691
						'REQUEST_URI' => '/index.php',
1692
						'SCRIPT_NAME' => '/index.php',
1693
						'PATH_INFO' => '',
1694
						'PHP_SELF' => '/index.php',
1695
					),
1696
				),
1697
				array(
1698
					'url' => '',
1699
					'base' => '/index.php',
1700
					'webroot' => '/'
1701
				),
1702
			),
1703
			array(
1704
				'Apache - No rewrite, document root set above top level cake dir, requesting path',
1705
				array(
1706
					'App' => array(
1707
						'base' => false,
1708
						'baseUrl' => '/site/index.php',
1709
						'dir' => 'app',
1710
						'webroot' => 'webroot'
1711
					),
1712
					'SERVER' => array(
1713
						'SERVER_NAME' => 'localhost',
1714
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
1715
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
1716
						'REQUEST_URI' => '/site/index.php/posts/index',
1717
						'SCRIPT_NAME' => '/site/index.php',
1718
						'PATH_INFO' => '/posts/index',
1719
						'PHP_SELF' => '/site/index.php/posts/index',
1720
					),
1721
				),
1722
				array(
1723
					'url' => 'posts/index',
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 root, no PATH_INFO',
1730
				array(
1731
					'App' => array(
1732
						'base' => false,
1733
						'baseUrl' => '/site/index.php',
1734
						'dir' => 'app',
1735
						'webroot' => 'webroot'
1736
					),
1737
					'SERVER' => array(
1738
						'SERVER_NAME' => 'localhost',
1739
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
1740
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
1741
						'REQUEST_URI' => '/site/index.php/',
1742
						'SCRIPT_NAME' => '/site/index.php',
1743
						'PHP_SELF' => '/site/index.php/',
1744
					),
1745
				),
1746
				array(
1747
					'url' => '',
1748
					'base' => '/site/index.php',
1749
					'webroot' => '/site/app/webroot/',
1750
				),
1751
			),
1752
			array(
1753
				'Apache - No rewrite, document root set above top level cake dir, request path, with GET',
1754
				array(
1755
					'App' => array(
1756
						'base' => false,
1757
						'baseUrl' => '/site/index.php',
1758
						'dir' => 'app',
1759
						'webroot' => 'webroot'
1760
					),
1761
					'GET' => array('a' => 'b', 'c' => 'd'),
1762
					'SERVER' => array(
1763
						'SERVER_NAME' => 'localhost',
1764
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
1765
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
1766
						'REQUEST_URI' => '/site/index.php/posts/index?a=b&c=d',
1767
						'SCRIPT_NAME' => '/site/index.php',
1768
						'PATH_INFO' => '/posts/index',
1769
						'PHP_SELF' => '/site/index.php/posts/index',
1770
						'QUERY_STRING' => 'a=b&c=d'
1771
					),
1772
				),
1773
				array(
1774
					'urlParams' => array('a' => 'b', 'c' => 'd'),
1775
					'url' => 'posts/index',
1776
					'base' => '/site/index.php',
1777
					'webroot' => '/site/app/webroot/',
1778
				),
1779
			),
1780
			array(
1781
				'Apache - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO',
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
						'REQUEST_URI' => '/site/',
1794
						'SCRIPT_NAME' => '/site/app/webroot/index.php',
1795
						'PHP_SELF' => '/site/app/webroot/index.php',
1796
					),
1797
				),
1798
				array(
1799
					'url' => '',
1800
					'base' => '/site',
1801
					'webroot' => '/site/',
1802
				),
1803
			),
1804
			array(
1805
				'Apache - w/rewrite, document root above top level cake dir, request root, no PATH_INFO/REQUEST_URI',
1806
				array(
1807
					'App' => array(
1808
						'base' => false,
1809
						'baseUrl' => false,
1810
						'dir' => 'app',
1811
						'webroot' => 'webroot'
1812
					),
1813
					'SERVER' => array(
1814
						'SERVER_NAME' => 'localhost',
1815
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
1816
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
1817
						'SCRIPT_NAME' => '/site/app/webroot/index.php',
1818
						'PHP_SELF' => '/site/app/webroot/index.php',
1819
						'PATH_INFO' => null,
1820
						'REQUEST_URI' => null,
1821
					),
1822
				),
1823
				array(
1824
					'url' => '',
1825
					'base' => '/site',
1826
					'webroot' => '/site/',
1827
				),
1828
			),
1829
			array(
1830
				'Apache - w/rewrite, document root set to webroot, request root, no PATH_INFO/REQUEST_URI',
1831
				array(
1832
					'App' => array(
1833
						'base' => false,
1834
						'baseUrl' => false,
1835
						'dir' => 'app',
1836
						'webroot' => 'webroot'
1837
					),
1838
					'SERVER' => array(
1839
						'SERVER_NAME' => 'localhost',
1840
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
1841
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
1842
						'SCRIPT_NAME' => '/index.php',
1843
						'PHP_SELF' => '/index.php',
1844
						'PATH_INFO' => null,
1845
						'REQUEST_URI' => null,
1846
					),
1847
				),
1848
				array(
1849
					'url' => '',
1850
					'base' => '',
1851
					'webroot' => '/',
1852
				),
1853
			),
1854
			array(
1855
				'Apache - w/rewrite, document root set above top level cake dir, request root, absolute REQUEST_URI',
1856
				array(
1857
					'App' => array(
1858
						'base' => false,
1859
						'baseUrl' => false,
1860
						'dir' => 'app',
1861
						'webroot' => 'webroot'
1862
					),
1863
					'SERVER' => array(
1864
						'SERVER_NAME' => 'localhost',
1865
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
1866
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
1867
						'REQUEST_URI' => '/site/posts/index',
1868
						'SCRIPT_NAME' => '/site/app/webroot/index.php',
1869
						'PHP_SELF' => '/site/app/webroot/index.php',
1870
					),
1871
				),
1872
				array(
1873
					'url' => 'posts/index',
1874
					'base' => '/site',
1875
					'webroot' => '/site/',
1876
				),
1877
			),
1878
			array(
1879
				'Nginx - w/rewrite, document root set to webroot, request root, no PATH_INFO',
1880
				array(
1881
					'App' => array(
1882
						'base' => false,
1883
						'baseUrl' => false,
1884
						'dir' => 'app',
1885
						'webroot' => 'webroot'
1886
					),
1887
					'GET' => array('/posts/add' => ''),
1888
					'SERVER' => array(
1889
						'SERVER_NAME' => 'localhost',
1890
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
1891
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
1892
						'SCRIPT_NAME' => '/index.php',
1893
						'QUERY_STRING' => '/posts/add&',
1894
						'PHP_SELF' => '/index.php',
1895
						'PATH_INFO' => null,
1896
						'REQUEST_URI' => '/posts/add',
1897
					),
1898
				),
1899
				array(
1900
					'url' => 'posts/add',
1901
					'base' => '',
1902
					'webroot' => '/',
1903
					'urlParams' => array()
1904
				),
1905
			),
1906
			array(
1907
				'Nginx - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO, base parameter set',
1908
				array(
1909
					'App' => array(
1910
						'base' => false,
1911
						'baseUrl' => false,
1912
						'dir' => 'app',
1913
						'webroot' => 'webroot'
1914
					),
1915
					'GET' => array('/site/posts/add' => ''),
1916
					'SERVER' => array(
1917
						'SERVER_NAME' => 'localhost',
1918
						'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
1919
						'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
1920
						'SCRIPT_NAME' => '/site/app/webroot/index.php',
1921
						'QUERY_STRING' => '/site/posts/add&',
1922
						'PHP_SELF' => '/site/app/webroot/index.php',
1923
						'PATH_INFO' => null,
1924
						'REQUEST_URI' => '/site/posts/add',
1925
					),
1926
				),
1927
				array(
1928
					'url' => 'posts/add',
1929
					'base' => '/site',
1930
					'webroot' => '/site/',
1931
					'urlParams' => array()
1932
				),
1933
			),
1934
		);
1935
	}
1936
 
1937
/**
1938
 * Test environment detection
1939
 *
1940
 * @dataProvider environmentGenerator
1941
 * @param $name
1942
 * @param $env
1943
 * @param $expected
1944
 * @return void
1945
 */
1946
	public function testEnvironmentDetection($name, $env, $expected) {
1947
		$_GET = array();
1948
		$this->_loadEnvironment($env);
1949
 
1950
		$request = new CakeRequest();
1951
		$this->assertEquals($expected['url'], $request->url, "url error");
1952
		$this->assertEquals($expected['base'], $request->base, "base error");
1953
		$this->assertEquals($expected['webroot'], $request->webroot, "webroot error");
1954
		if (isset($expected['urlParams'])) {
1955
			$this->assertEquals($expected['urlParams'], $request->query, "GET param mismatch");
1956
		}
1957
	}
1958
 
1959
/**
1960
 * Test the query() method
1961
 *
1962
 * @return void
1963
 */
1964
	public function testQuery() {
1965
		$_GET = array();
1966
		$_GET['foo'] = 'bar';
1967
 
1968
		$request = new CakeRequest();
1969
 
1970
		$result = $request->query('foo');
1971
		$this->assertEquals('bar', $result);
1972
 
1973
		$result = $request->query('imaginary');
1974
		$this->assertNull($result);
1975
	}
1976
 
1977
/**
1978
 * Test the query() method with arrays passed via $_GET
1979
 *
1980
 * @return void
1981
 */
1982
	public function testQueryWithArray() {
1983
		$_GET = array();
1984
		$_GET['test'] = array('foo', 'bar');
1985
 
1986
		$request = new CakeRequest();
1987
 
1988
		$result = $request->query('test');
1989
		$this->assertEquals(array('foo', 'bar'), $result);
1990
 
1991
		$result = $request->query('test.1');
1992
		$this->assertEquals('bar', $result);
1993
 
1994
		$result = $request->query('test.2');
1995
		$this->assertNull($result);
1996
	}
1997
 
1998
/**
1999
 * Test using param()
2000
 *
2001
 * @return void
2002
 */
2003
	public function testReadingParams() {
2004
		$request = new CakeRequest();
2005
		$request->addParams(array(
2006
			'controller' => 'posts',
2007
			'admin' => true,
2008
			'truthy' => 1,
2009
			'zero' => '0',
2010
		));
2011
		$this->assertFalse($request->param('not_set'));
2012
		$this->assertTrue($request->param('admin'));
2013
		$this->assertEquals(1, $request->param('truthy'));
2014
		$this->assertEquals('posts', $request->param('controller'));
2015
		$this->assertEquals('0', $request->param('zero'));
2016
	}
2017
 
2018
/**
2019
 * Test the data() method reading
2020
 *
2021
 * @return void
2022
 */
2023
	public function testDataReading() {
2024
		$_POST['data'] = array(
2025
			'Model' => array(
2026
				'field' => 'value'
2027
			)
2028
		);
2029
		$request = new CakeRequest('posts/index');
2030
		$result = $request->data('Model');
2031
		$this->assertEquals($_POST['data']['Model'], $result);
2032
 
2033
		$result = $request->data('Model.imaginary');
2034
		$this->assertNull($result);
2035
	}
2036
 
2037
/**
2038
 * Test writing with data()
2039
 *
2040
 * @return void
2041
 */
2042
	public function testDataWriting() {
2043
		$_POST['data'] = array(
2044
			'Model' => array(
2045
				'field' => 'value'
2046
			)
2047
		);
2048
		$request = new CakeRequest('posts/index');
2049
		$result = $request->data('Model.new_value', 'new value');
2050
		$this->assertSame($result, $request, 'Return was not $this');
2051
 
2052
		$this->assertEquals('new value', $request->data['Model']['new_value']);
2053
 
2054
		$request->data('Post.title', 'New post')->data('Comment.1.author', 'Mark');
2055
		$this->assertEquals('New post', $request->data['Post']['title']);
2056
		$this->assertEquals('Mark', $request->data['Comment']['1']['author']);
2057
	}
2058
 
2059
/**
2060
 * Test writing falsey values.
2061
 *
2062
 * @return void
2063
 */
2064
	public function testDataWritingFalsey() {
2065
		$request = new CakeRequest('posts/index');
2066
 
2067
		$request->data('Post.null', null);
2068
		$this->assertNull($request->data['Post']['null']);
2069
 
2070
		$request->data('Post.false', false);
2071
		$this->assertFalse($request->data['Post']['false']);
2072
 
2073
		$request->data('Post.zero', 0);
2074
		$this->assertSame(0, $request->data['Post']['zero']);
2075
 
2076
		$request->data('Post.empty', '');
2077
		$this->assertSame('', $request->data['Post']['empty']);
2078
	}
2079
 
2080
/**
2081
 * Test accept language
2082
 *
2083
 * @return void
2084
 */
2085
	public function testAcceptLanguage() {
2086
		// Weird language
2087
		$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
2088
		$result = CakeRequest::acceptLanguage();
2089
		$this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
2090
 
2091
		// No qualifier
2092
		$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
2093
		$result = CakeRequest::acceptLanguage();
2094
		$this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
2095
 
2096
		// With qualifier
2097
		$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4';
2098
		$result = CakeRequest::acceptLanguage();
2099
		$this->assertEquals(array('en-us', 'en', 'pt-br', 'pt'), $result, 'Languages do not match');
2100
 
2101
		// With spaces
2102
		$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'da, en-gb;q=0.8, en;q=0.7';
2103
		$result = CakeRequest::acceptLanguage();
2104
		$this->assertEquals(array('da', 'en-gb', 'en'), $result, 'Languages do not match');
2105
 
2106
		// Checking if requested
2107
		$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
2108
		$result = CakeRequest::acceptLanguage();
2109
 
2110
		$result = CakeRequest::acceptLanguage('en-ca');
2111
		$this->assertTrue($result);
2112
 
2113
		$result = CakeRequest::acceptLanguage('en-CA');
2114
		$this->assertTrue($result);
2115
 
2116
		$result = CakeRequest::acceptLanguage('en-us');
2117
		$this->assertFalse($result);
2118
 
2119
		$result = CakeRequest::acceptLanguage('en-US');
2120
		$this->assertFalse($result);
2121
	}
2122
 
2123
/**
2124
 * Test the here() method
2125
 *
2126
 * @return void
2127
 */
2128
	public function testHere() {
2129
		Configure::write('App.base', '/base_path');
2130
		$_GET = array('test' => 'value');
2131
		$request = new CakeRequest('/posts/add/1/name:value');
2132
 
2133
		$result = $request->here();
2134
		$this->assertEquals('/base_path/posts/add/1/name:value?test=value', $result);
2135
 
2136
		$result = $request->here(false);
2137
		$this->assertEquals('/posts/add/1/name:value?test=value', $result);
2138
 
2139
		$request = new CakeRequest('/posts/base_path/1/name:value');
2140
		$result = $request->here();
2141
		$this->assertEquals('/base_path/posts/base_path/1/name:value?test=value', $result);
2142
 
2143
		$result = $request->here(false);
2144
		$this->assertEquals('/posts/base_path/1/name:value?test=value', $result);
2145
	}
2146
 
2147
/**
2148
 * Test the input() method.
2149
 *
2150
 * @return void
2151
 */
2152
	public function testInput() {
2153
		$request = $this->getMock('CakeRequest', array('_readInput'));
2154
		$request->expects($this->once())->method('_readInput')
2155
			->will($this->returnValue('I came from stdin'));
2156
 
2157
		$result = $request->input();
2158
		$this->assertEquals('I came from stdin', $result);
2159
	}
2160
 
2161
/**
2162
 * Test input() decoding.
2163
 *
2164
 * @return void
2165
 */
2166
	public function testInputDecode() {
2167
		$request = $this->getMock('CakeRequest', array('_readInput'));
2168
		$request->expects($this->once())->method('_readInput')
2169
			->will($this->returnValue('{"name":"value"}'));
2170
 
2171
		$result = $request->input('json_decode');
2172
		$this->assertEquals(array('name' => 'value'), (array)$result);
2173
	}
2174
 
2175
/**
2176
 * Test input() decoding with additional arguments.
2177
 *
2178
 * @return void
2179
 */
2180
	public function testInputDecodeExtraParams() {
2181
		$xml = <<<XML
2182
<?xml version="1.0" encoding="utf-8"?>
2183
<post>
2184
	<title id="title">Test</title>
2185
</post>
2186
XML;
2187
 
2188
		$request = $this->getMock('CakeRequest', array('_readInput'));
2189
		$request->expects($this->once())->method('_readInput')
2190
			->will($this->returnValue($xml));
2191
 
2192
		$result = $request->input('Xml::build', array('return' => 'domdocument'));
2193
		$this->assertInstanceOf('DOMDocument', $result);
2194
		$this->assertEquals(
2195
			'Test',
2196
			$result->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText
2197
		);
2198
	}
2199
 
2200
/**
2201
 * Test is('requested') and isRequested()
2202
 *
2203
 * @return void
2204
 */
2205
	public function testIsRequested() {
2206
		$request = new CakeRequest('/posts/index');
2207
		$request->addParams(array(
2208
			'controller' => 'posts',
2209
			'action' => 'index',
2210
			'plugin' => null,
2211
			'requested' => 1
2212
		));
2213
		$this->assertTrue($request->is('requested'));
2214
		$this->assertTrue($request->isRequested());
2215
 
2216
		$request = new CakeRequest('/posts/index');
2217
		$request->addParams(array(
2218
			'controller' => 'posts',
2219
			'action' => 'index',
2220
			'plugin' => null,
2221
		));
2222
		$this->assertFalse($request->is('requested'));
2223
		$this->assertFalse($request->isRequested());
2224
	}
2225
 
2226
/**
2227
 * Test allowMethod method
2228
 *
2229
 * @return void
2230
 */
2231
	public function testAllowMethod() {
2232
		$_SERVER['REQUEST_METHOD'] = 'PUT';
2233
		$request = new CakeRequest('/posts/edit/1');
2234
 
2235
		$this->assertTrue($request->allowMethod(array('put')));
2236
 
2237
		// BC check
2238
		$this->assertTrue($request->onlyAllow(array('put')));
2239
 
2240
		$_SERVER['REQUEST_METHOD'] = 'DELETE';
2241
		$this->assertTrue($request->allowMethod('post', 'delete'));
2242
 
2243
		// BC check
2244
		$this->assertTrue($request->onlyAllow('post', 'delete'));
2245
	}
2246
 
2247
/**
2248
 * Test allowMethod throwing exception
2249
 *
2250
 * @return void
2251
 */
2252
	public function testAllowMethodException() {
2253
		$_SERVER['REQUEST_METHOD'] = 'PUT';
2254
		$request = new CakeRequest('/posts/edit/1');
2255
 
2256
		try {
2257
			$request->allowMethod('POST', 'DELETE');
2258
			$this->fail('An expected exception has not been raised.');
2259
		} catch (MethodNotAllowedException $e) {
2260
			$this->assertEquals(array('Allow' => 'POST, DELETE'), $e->responseHeader());
2261
		}
2262
 
2263
		$this->setExpectedException('MethodNotAllowedException');
2264
		$request->allowMethod('POST');
2265
	}
2266
 
2267
/**
2268
 * loadEnvironment method
2269
 *
2270
 * @param array $env
2271
 * @return void
2272
 */
2273
	protected function _loadEnvironment($env) {
2274
		if (isset($env['App'])) {
2275
			Configure::write('App', $env['App']);
2276
		}
2277
 
2278
		if (isset($env['GET'])) {
2279
			foreach ($env['GET'] as $key => $val) {
2280
				$_GET[$key] = $val;
2281
			}
2282
		}
2283
 
2284
		if (isset($env['POST'])) {
2285
			foreach ($env['POST'] as $key => $val) {
2286
				$_POST[$key] = $val;
2287
			}
2288
		}
2289
 
2290
		if (isset($env['SERVER'])) {
2291
			foreach ($env['SERVER'] as $key => $val) {
2292
				$_SERVER[$key] = $val;
2293
			}
2294
		}
2295
	}
2296
 
2297
}