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