CakeRequestTest.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879
  1. <?php
  2. /**
  3. * CakeRequest Test case file.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Test.Case.Network
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Dispatcher', 'Routing');
  20. App::uses('Xml', 'Utility');
  21. App::uses('CakeRequest', 'Network');
  22. class TestCakeRequest extends CakeRequest {
  23. public $input;
  24. public function reConstruct($url = 'some/path', $parseEnvironment = true) {
  25. $this->_base();
  26. if (empty($url)) {
  27. $url = $this->_url();
  28. }
  29. if ($url[0] == '/') {
  30. $url = substr($url, 1);
  31. }
  32. $this->url = $url;
  33. if ($parseEnvironment) {
  34. $this->_processPost();
  35. $this->_processGet();
  36. $this->_processFiles();
  37. }
  38. $this->here = $this->base . '/' . $this->url;
  39. }
  40. protected function _readInput() {
  41. return $this->input;
  42. }
  43. }
  44. class CakeRequestTest extends CakeTestCase {
  45. /**
  46. * setup callback
  47. *
  48. * @return void
  49. */
  50. public function setUp() {
  51. parent::setUp();
  52. $this->_app = Configure::read('App');
  53. $this->_case = null;
  54. if (isset($_GET['case'])) {
  55. $this->_case = $_GET['case'];
  56. unset($_GET['case']);
  57. }
  58. Configure::write('App.baseUrl', false);
  59. }
  60. /**
  61. * tearDown
  62. *
  63. * @return void
  64. */
  65. public function tearDown() {
  66. parent::tearDown();
  67. if (!empty($this->_case)) {
  68. $_GET['case'] = $this->_case;
  69. }
  70. Configure::write('App', $this->_app);
  71. }
  72. /**
  73. * test that the autoparse = false constructor works.
  74. *
  75. * @return void
  76. */
  77. public function testNoAutoParseConstruction() {
  78. $_GET = array(
  79. 'one' => 'param'
  80. );
  81. $request = new CakeRequest(null, false);
  82. $this->assertFalse(isset($request->query['one']));
  83. }
  84. /**
  85. * test construction
  86. *
  87. * @return void
  88. */
  89. public function testConstructionGetParsing() {
  90. $_GET = array(
  91. 'one' => 'param',
  92. 'two' => 'banana'
  93. );
  94. $request = new CakeRequest('some/path');
  95. $this->assertEquals($request->query, $_GET);
  96. $_GET = array(
  97. 'one' => 'param',
  98. 'two' => 'banana',
  99. );
  100. $request = new CakeRequest('some/path');
  101. $this->assertEquals($request->query, $_GET);
  102. $this->assertEquals('some/path', $request->url);
  103. }
  104. /**
  105. * Test that querystring args provided in the url string are parsed.
  106. *
  107. * @return void
  108. */
  109. public function testQueryStringParsingFromInputUrl() {
  110. $_GET = array();
  111. $request = new CakeRequest('some/path?one=something&two=else');
  112. $expected = array('one' => 'something', 'two' => 'else');
  113. $this->assertEquals($expected, $request->query);
  114. $this->assertEquals('some/path?one=something&two=else', $request->url);
  115. }
  116. /**
  117. * Test that named arguments + querystrings are handled correctly.
  118. *
  119. * @return void
  120. */
  121. public function testQueryStringAndNamedParams() {
  122. $_SERVER['REQUEST_URI'] = '/tasks/index/page:1?ts=123456';
  123. $request = new CakeRequest();
  124. $this->assertEquals('tasks/index/page:1', $request->url);
  125. $_SERVER['REQUEST_URI'] = '/tasks/index/page:1/?ts=123456';
  126. $request = new CakeRequest();
  127. $this->assertEquals('tasks/index/page:1/', $request->url);
  128. }
  129. /**
  130. * test addParams() method
  131. *
  132. * @return void
  133. */
  134. public function testAddParams() {
  135. $request = new CakeRequest('some/path');
  136. $request->params = array('controller' => 'posts', 'action' => 'view');
  137. $result = $request->addParams(array('plugin' => null, 'action' => 'index'));
  138. $this->assertSame($result, $request, 'Method did not return itself. %s');
  139. $this->assertEquals('posts', $request->controller);
  140. $this->assertEquals('index', $request->action);
  141. $this->assertEquals(null, $request->plugin);
  142. }
  143. /**
  144. * test splicing in paths.
  145. *
  146. * @return void
  147. */
  148. public function testAddPaths() {
  149. $request = new CakeRequest('some/path');
  150. $request->webroot = '/some/path/going/here/';
  151. $result = $request->addPaths(array(
  152. 'random' => '/something', 'webroot' => '/', 'here' => '/', 'base' => '/base_dir'
  153. ));
  154. $this->assertSame($result, $request, 'Method did not return itself. %s');
  155. $this->assertEquals('/', $request->webroot);
  156. $this->assertEquals('/base_dir', $request->base);
  157. $this->assertEquals('/', $request->here);
  158. $this->assertFalse(isset($request->random));
  159. }
  160. /**
  161. * test parsing POST data into the object.
  162. *
  163. * @return void
  164. */
  165. public function testPostParsing() {
  166. $_POST = array('data' => array(
  167. 'Article' => array('title')
  168. ));
  169. $request = new CakeRequest('some/path');
  170. $this->assertEquals($_POST['data'], $request->data);
  171. $_POST = array('one' => 1, 'two' => 'three');
  172. $request = new CakeRequest('some/path');
  173. $this->assertEquals($_POST, $request->data);
  174. $_POST = array(
  175. 'data' => array(
  176. 'Article' => array('title' => 'Testing'),
  177. ),
  178. 'action' => 'update'
  179. );
  180. $request = new CakeRequest('some/path');
  181. $expected = array(
  182. 'Article' => array('title' => 'Testing'),
  183. 'action' => 'update'
  184. );
  185. $this->assertEquals($expected, $request->data);
  186. $_POST = array('data' => array(
  187. 'Article' => array('title'),
  188. 'Tag' => array('Tag' => array(1, 2))
  189. ));
  190. $request = new CakeRequest('some/path');
  191. $this->assertEquals($_POST['data'], $request->data);
  192. $_POST = array('data' => array(
  193. 'Article' => array('title' => 'some title'),
  194. 'Tag' => array('Tag' => array(1, 2))
  195. ));
  196. $request = new CakeRequest('some/path');
  197. $this->assertEquals($_POST['data'], $request->data);
  198. $_POST = array(
  199. 'a' => array(1, 2),
  200. 'b' => array(1, 2)
  201. );
  202. $request = new CakeRequest('some/path');
  203. $this->assertEquals($_POST, $request->data);
  204. }
  205. /**
  206. * test parsing PUT data into the object.
  207. *
  208. * @return void
  209. */
  210. public function testPutParsing() {
  211. $_SERVER['REQUEST_METHOD'] = 'PUT';
  212. $_SERVER['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
  213. $data = array('data' => array(
  214. 'Article' => array('title')
  215. ));
  216. $request = $this->getMock('TestCakeRequest', array('_readInput'));
  217. $request->expects($this->at(0))->method('_readInput')
  218. ->will($this->returnValue('data[Article][]=title'));
  219. $request->reConstruct();
  220. $this->assertEquals($data['data'], $request->data);
  221. $data = array('one' => 1, 'two' => 'three');
  222. $request = $this->getMock('TestCakeRequest', array('_readInput'));
  223. $request->expects($this->at(0))->method('_readInput')
  224. ->will($this->returnValue('one=1&two=three'));
  225. $request->reConstruct();
  226. $this->assertEquals($data, $request->data);
  227. $data = array(
  228. 'data' => array(
  229. 'Article' => array('title' => 'Testing'),
  230. ),
  231. 'action' => 'update'
  232. );
  233. $request = $this->getMock('TestCakeRequest', array('_readInput'));
  234. $request->expects($this->at(0))->method('_readInput')
  235. ->will($this->returnValue('data[Article][title]=Testing&action=update'));
  236. $request->reConstruct();
  237. $expected = array(
  238. 'Article' => array('title' => 'Testing'),
  239. 'action' => 'update'
  240. );
  241. $this->assertEquals($expected, $request->data);
  242. $data = array('data' => array(
  243. 'Article' => array('title'),
  244. 'Tag' => array('Tag' => array(1, 2))
  245. ));
  246. $request = $this->getMock('TestCakeRequest', array('_readInput'));
  247. $request->expects($this->at(0))->method('_readInput')
  248. ->will($this->returnValue('data[Article][]=title&Tag[Tag][]=1&Tag[Tag][]=2'));
  249. $request->reConstruct();
  250. $this->assertEquals($data['data'], $request->data);
  251. $data = array('data' => array(
  252. 'Article' => array('title' => 'some title'),
  253. 'Tag' => array('Tag' => array(1, 2))
  254. ));
  255. $request = $this->getMock('TestCakeRequest', array('_readInput'));
  256. $request->expects($this->at(0))->method('_readInput')
  257. ->will($this->returnValue('data[Article][title]=some%20title&Tag[Tag][]=1&Tag[Tag][]=2'));
  258. $request->reConstruct();
  259. $this->assertEquals($data['data'], $request->data);
  260. $data = array(
  261. 'a' => array(1, 2),
  262. 'b' => array(1, 2)
  263. );
  264. $request = $this->getMock('TestCakeRequest', array('_readInput'));
  265. $request->expects($this->at(0))->method('_readInput')
  266. ->will($this->returnValue('a[]=1&a[]=2&b[]=1&b[]=2'));
  267. $request->reConstruct();
  268. $this->assertEquals($data, $request->data);
  269. }
  270. /**
  271. * test parsing of FILES array
  272. *
  273. * @return void
  274. */
  275. public function testFilesParsing() {
  276. $_FILES = array(
  277. 'data' => array(
  278. 'name' => array(
  279. 'File' => array(
  280. array('data' => 'cake_sqlserver_patch.patch'),
  281. array('data' => 'controller.diff'),
  282. array('data' => ''),
  283. array('data' => ''),
  284. ),
  285. 'Post' => array('attachment' => 'jquery-1.2.1.js'),
  286. ),
  287. 'type' => array(
  288. 'File' => array(
  289. array('data' => ''),
  290. array('data' => ''),
  291. array('data' => ''),
  292. array('data' => ''),
  293. ),
  294. 'Post' => array('attachment' => 'application/x-javascript'),
  295. ),
  296. 'tmp_name' => array(
  297. 'File' => array(
  298. array('data' => '/private/var/tmp/phpy05Ywj'),
  299. array('data' => '/private/var/tmp/php7MBztY'),
  300. array('data' => ''),
  301. array('data' => ''),
  302. ),
  303. 'Post' => array('attachment' => '/private/var/tmp/phpEwlrIo'),
  304. ),
  305. 'error' => array(
  306. 'File' => array(
  307. array('data' => 0),
  308. array('data' => 0),
  309. array('data' => 4),
  310. array('data' => 4)
  311. ),
  312. 'Post' => array('attachment' => 0)
  313. ),
  314. 'size' => array(
  315. 'File' => array(
  316. array('data' => 6271),
  317. array('data' => 350),
  318. array('data' => 0),
  319. array('data' => 0),
  320. ),
  321. 'Post' => array('attachment' => 80469)
  322. ),
  323. )
  324. );
  325. $request = new CakeRequest('some/path');
  326. $expected = array(
  327. 'File' => array(
  328. array(
  329. 'data' => array(
  330. 'name' => 'cake_sqlserver_patch.patch',
  331. 'type' => '',
  332. 'tmp_name' => '/private/var/tmp/phpy05Ywj',
  333. 'error' => 0,
  334. 'size' => 6271,
  335. )
  336. ),
  337. array(
  338. 'data' => array(
  339. 'name' => 'controller.diff',
  340. 'type' => '',
  341. 'tmp_name' => '/private/var/tmp/php7MBztY',
  342. 'error' => 0,
  343. 'size' => 350,
  344. )
  345. ),
  346. array(
  347. 'data' => array(
  348. 'name' => '',
  349. 'type' => '',
  350. 'tmp_name' => '',
  351. 'error' => 4,
  352. 'size' => 0,
  353. )
  354. ),
  355. array(
  356. 'data' => array(
  357. 'name' => '',
  358. 'type' => '',
  359. 'tmp_name' => '',
  360. 'error' => 4,
  361. 'size' => 0,
  362. )
  363. ),
  364. ),
  365. 'Post' => array(
  366. 'attachment' => array(
  367. 'name' => 'jquery-1.2.1.js',
  368. 'type' => 'application/x-javascript',
  369. 'tmp_name' => '/private/var/tmp/phpEwlrIo',
  370. 'error' => 0,
  371. 'size' => 80469,
  372. )
  373. )
  374. );
  375. $this->assertEquals($expected, $request->data);
  376. $_FILES = array(
  377. 'data' => array(
  378. 'name' => array(
  379. 'Document' => array(
  380. 1 => array(
  381. 'birth_cert' => 'born on.txt',
  382. 'passport' => 'passport.txt',
  383. 'drivers_license' => 'ugly pic.jpg'
  384. ),
  385. 2 => array(
  386. 'birth_cert' => 'aunt betty.txt',
  387. 'passport' => 'betty-passport.txt',
  388. 'drivers_license' => 'betty-photo.jpg'
  389. ),
  390. ),
  391. ),
  392. 'type' => array(
  393. 'Document' => array(
  394. 1 => array(
  395. 'birth_cert' => 'application/octet-stream',
  396. 'passport' => 'application/octet-stream',
  397. 'drivers_license' => 'application/octet-stream',
  398. ),
  399. 2 => array(
  400. 'birth_cert' => 'application/octet-stream',
  401. 'passport' => 'application/octet-stream',
  402. 'drivers_license' => 'application/octet-stream',
  403. )
  404. )
  405. ),
  406. 'tmp_name' => array(
  407. 'Document' => array(
  408. 1 => array(
  409. 'birth_cert' => '/private/var/tmp/phpbsUWfH',
  410. 'passport' => '/private/var/tmp/php7f5zLt',
  411. 'drivers_license' => '/private/var/tmp/phpMXpZgT',
  412. ),
  413. 2 => array(
  414. 'birth_cert' => '/private/var/tmp/php5kHZt0',
  415. 'passport' => '/private/var/tmp/phpnYkOuM',
  416. 'drivers_license' => '/private/var/tmp/php9Rq0P3',
  417. )
  418. )
  419. ),
  420. 'error' => array(
  421. 'Document' => array(
  422. 1 => array(
  423. 'birth_cert' => 0,
  424. 'passport' => 0,
  425. 'drivers_license' => 0,
  426. ),
  427. 2 => array(
  428. 'birth_cert' => 0,
  429. 'passport' => 0,
  430. 'drivers_license' => 0,
  431. )
  432. )
  433. ),
  434. 'size' => array(
  435. 'Document' => array(
  436. 1 => array(
  437. 'birth_cert' => 123,
  438. 'passport' => 458,
  439. 'drivers_license' => 875,
  440. ),
  441. 2 => array(
  442. 'birth_cert' => 876,
  443. 'passport' => 976,
  444. 'drivers_license' => 9783,
  445. )
  446. )
  447. )
  448. )
  449. );
  450. $request = new CakeRequest('some/path');
  451. $expected = array(
  452. 'Document' => array(
  453. 1 => array(
  454. 'birth_cert' => array(
  455. 'name' => 'born on.txt',
  456. 'tmp_name' => '/private/var/tmp/phpbsUWfH',
  457. 'error' => 0,
  458. 'size' => 123,
  459. 'type' => 'application/octet-stream',
  460. ),
  461. 'passport' => array(
  462. 'name' => 'passport.txt',
  463. 'tmp_name' => '/private/var/tmp/php7f5zLt',
  464. 'error' => 0,
  465. 'size' => 458,
  466. 'type' => 'application/octet-stream',
  467. ),
  468. 'drivers_license' => array(
  469. 'name' => 'ugly pic.jpg',
  470. 'tmp_name' => '/private/var/tmp/phpMXpZgT',
  471. 'error' => 0,
  472. 'size' => 875,
  473. 'type' => 'application/octet-stream',
  474. ),
  475. ),
  476. 2 => array(
  477. 'birth_cert' => array(
  478. 'name' => 'aunt betty.txt',
  479. 'tmp_name' => '/private/var/tmp/php5kHZt0',
  480. 'error' => 0,
  481. 'size' => 876,
  482. 'type' => 'application/octet-stream',
  483. ),
  484. 'passport' => array(
  485. 'name' => 'betty-passport.txt',
  486. 'tmp_name' => '/private/var/tmp/phpnYkOuM',
  487. 'error' => 0,
  488. 'size' => 976,
  489. 'type' => 'application/octet-stream',
  490. ),
  491. 'drivers_license' => array(
  492. 'name' => 'betty-photo.jpg',
  493. 'tmp_name' => '/private/var/tmp/php9Rq0P3',
  494. 'error' => 0,
  495. 'size' => 9783,
  496. 'type' => 'application/octet-stream',
  497. ),
  498. ),
  499. )
  500. );
  501. $this->assertEquals($expected, $request->data);
  502. $_FILES = array(
  503. 'data' => array(
  504. 'name' => array('birth_cert' => 'born on.txt'),
  505. 'type' => array('birth_cert' => 'application/octet-stream'),
  506. 'tmp_name' => array('birth_cert' => '/private/var/tmp/phpbsUWfH'),
  507. 'error' => array('birth_cert' => 0),
  508. 'size' => array('birth_cert' => 123)
  509. )
  510. );
  511. $request = new CakeRequest('some/path');
  512. $expected = array(
  513. 'birth_cert' => array(
  514. 'name' => 'born on.txt',
  515. 'type' => 'application/octet-stream',
  516. 'tmp_name' => '/private/var/tmp/phpbsUWfH',
  517. 'error' => 0,
  518. 'size' => 123
  519. )
  520. );
  521. $this->assertEquals($expected, $request->data);
  522. $_FILES = array(
  523. 'something' => array(
  524. 'name' => 'something.txt',
  525. 'type' => 'text/plain',
  526. 'tmp_name' => '/some/file',
  527. 'error' => 0,
  528. 'size' => 123
  529. )
  530. );
  531. $request = new CakeRequest('some/path');
  532. $this->assertEquals($request->params['form'], $_FILES);
  533. }
  534. /**
  535. * test method overrides coming in from POST data.
  536. *
  537. * @return void
  538. */
  539. public function testMethodOverrides() {
  540. $_POST = array('_method' => 'POST');
  541. $request = new CakeRequest('some/path');
  542. $this->assertEquals(env('REQUEST_METHOD'), 'POST');
  543. $_POST = array('_method' => 'DELETE');
  544. $request = new CakeRequest('some/path');
  545. $this->assertEquals(env('REQUEST_METHOD'), 'DELETE');
  546. $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
  547. $request = new CakeRequest('some/path');
  548. $this->assertEquals(env('REQUEST_METHOD'), 'PUT');
  549. }
  550. /**
  551. * test the clientIp method.
  552. *
  553. * @return void
  554. */
  555. public function testclientIp() {
  556. $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.5, 10.0.1.1, proxy.com';
  557. $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
  558. $_SERVER['REMOTE_ADDR'] = '192.168.1.3';
  559. $request = new CakeRequest('some/path');
  560. $this->assertEquals('192.168.1.5', $request->clientIp(false));
  561. $this->assertEquals('192.168.1.2', $request->clientIp());
  562. unset($_SERVER['HTTP_X_FORWARDED_FOR']);
  563. $this->assertEquals('192.168.1.2', $request->clientIp());
  564. unset($_SERVER['HTTP_CLIENT_IP']);
  565. $this->assertEquals('192.168.1.3', $request->clientIp());
  566. $_SERVER['HTTP_CLIENTADDRESS'] = '10.0.1.2, 10.0.1.1';
  567. $this->assertEquals('10.0.1.2', $request->clientIp());
  568. }
  569. /**
  570. * test the referer function.
  571. *
  572. * @return void
  573. */
  574. public function testReferer() {
  575. $request = new CakeRequest('some/path');
  576. $request->webroot = '/';
  577. $_SERVER['HTTP_REFERER'] = 'http://cakephp.org';
  578. $result = $request->referer();
  579. $this->assertSame($result, 'http://cakephp.org');
  580. $_SERVER['HTTP_REFERER'] = '';
  581. $result = $request->referer();
  582. $this->assertSame($result, '/');
  583. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
  584. $result = $request->referer(true);
  585. $this->assertSame($result, '/some/path');
  586. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
  587. $result = $request->referer(false);
  588. $this->assertSame($result, FULL_BASE_URL . '/some/path');
  589. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
  590. $result = $request->referer(true);
  591. $this->assertSame($result, '/some/path');
  592. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/recipes/add';
  593. $result = $request->referer(true);
  594. $this->assertSame($result, '/recipes/add');
  595. $_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org';
  596. $result = $request->referer();
  597. $this->assertSame($result, 'cakephp.org');
  598. }
  599. /**
  600. * test the simple uses of is()
  601. *
  602. * @return void
  603. */
  604. public function testIsHttpMethods() {
  605. $request = new CakeRequest('some/path');
  606. $this->assertFalse($request->is('undefined-behavior'));
  607. $_SERVER['REQUEST_METHOD'] = 'GET';
  608. $this->assertTrue($request->is('get'));
  609. $_SERVER['REQUEST_METHOD'] = 'POST';
  610. $this->assertTrue($request->is('POST'));
  611. $_SERVER['REQUEST_METHOD'] = 'PUT';
  612. $this->assertTrue($request->is('put'));
  613. $this->assertFalse($request->is('get'));
  614. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  615. $this->assertTrue($request->is('delete'));
  616. $this->assertTrue($request->isDelete());
  617. $_SERVER['REQUEST_METHOD'] = 'delete';
  618. $this->assertFalse($request->is('delete'));
  619. }
  620. /**
  621. * test the method() method.
  622. *
  623. * @return void
  624. */
  625. public function testMethod() {
  626. $_SERVER['REQUEST_METHOD'] = 'delete';
  627. $request = new CakeRequest('some/path');
  628. $this->assertEquals('delete', $request->method());
  629. }
  630. /**
  631. * test host retrieval.
  632. *
  633. * @return void
  634. */
  635. public function testHost() {
  636. $_SERVER['HTTP_HOST'] = 'localhost';
  637. $request = new CakeRequest('some/path');
  638. $this->assertEquals('localhost', $request->host());
  639. }
  640. /**
  641. * test domain retrieval.
  642. *
  643. * @return void
  644. */
  645. public function testDomain() {
  646. $_SERVER['HTTP_HOST'] = 'something.example.com';
  647. $request = new CakeRequest('some/path');
  648. $this->assertEquals('example.com', $request->domain());
  649. $_SERVER['HTTP_HOST'] = 'something.example.co.uk';
  650. $this->assertEquals('example.co.uk', $request->domain(2));
  651. }
  652. /**
  653. * test getting subdomains for a host.
  654. *
  655. * @return void
  656. */
  657. public function testSubdomain() {
  658. $_SERVER['HTTP_HOST'] = 'something.example.com';
  659. $request = new CakeRequest('some/path');
  660. $this->assertEquals(array('something'), $request->subdomains());
  661. $_SERVER['HTTP_HOST'] = 'www.something.example.com';
  662. $this->assertEquals(array('www', 'something'), $request->subdomains());
  663. $_SERVER['HTTP_HOST'] = 'www.something.example.co.uk';
  664. $this->assertEquals(array('www', 'something'), $request->subdomains(2));
  665. $_SERVER['HTTP_HOST'] = 'example.co.uk';
  666. $this->assertEquals(array(), $request->subdomains(2));
  667. }
  668. /**
  669. * test ajax, flash and friends
  670. *
  671. * @return void
  672. */
  673. public function testisAjaxFlashAndFriends() {
  674. $request = new CakeRequest('some/path');
  675. $_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
  676. $this->assertTrue($request->is('flash'));
  677. $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash';
  678. $this->assertTrue($request->is('flash'));
  679. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  680. $this->assertTrue($request->is('ajax'));
  681. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHTTPREQUEST';
  682. $this->assertFalse($request->is('ajax'));
  683. $this->assertFalse($request->isAjax());
  684. $_SERVER['HTTP_USER_AGENT'] = 'Android 2.0';
  685. $this->assertTrue($request->is('mobile'));
  686. $this->assertTrue($request->isMobile());
  687. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 5.1; rv:2.0b6pre) Gecko/20100902 Firefox/4.0b6pre Fennec/2.0b1pre';
  688. $this->assertTrue($request->is('mobile'));
  689. $this->assertTrue($request->isMobile());
  690. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; SAMSUNG; OMNIA7)';
  691. $this->assertTrue($request->is('mobile'));
  692. $this->assertTrue($request->isMobile());
  693. }
  694. /**
  695. * test __call expcetions
  696. *
  697. * @expectedException CakeException
  698. * @return void
  699. */
  700. public function testMagicCallExceptionOnUnknownMethod() {
  701. $request = new CakeRequest('some/path');
  702. $request->IamABanana();
  703. }
  704. /**
  705. * test is(ssl)
  706. *
  707. * @return void
  708. */
  709. public function testIsSsl() {
  710. $request = new CakeRequest('some/path');
  711. $_SERVER['HTTPS'] = 1;
  712. $this->assertTrue($request->is('ssl'));
  713. $_SERVER['HTTPS'] = 'on';
  714. $this->assertTrue($request->is('ssl'));
  715. $_SERVER['HTTPS'] = '1';
  716. $this->assertTrue($request->is('ssl'));
  717. $_SERVER['HTTPS'] = 'I am not empty';
  718. $this->assertTrue($request->is('ssl'));
  719. $_SERVER['HTTPS'] = 1;
  720. $this->assertTrue($request->is('ssl'));
  721. $_SERVER['HTTPS'] = 'off';
  722. $this->assertFalse($request->is('ssl'));
  723. $_SERVER['HTTPS'] = false;
  724. $this->assertFalse($request->is('ssl'));
  725. $_SERVER['HTTPS'] = '';
  726. $this->assertFalse($request->is('ssl'));
  727. }
  728. /**
  729. * test getting request params with object properties.
  730. *
  731. * @return void
  732. */
  733. public function testMagicget() {
  734. $request = new CakeRequest('some/path');
  735. $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
  736. $this->assertEquals('posts', $request->controller);
  737. $this->assertEquals('view', $request->action);
  738. $this->assertEquals('blogs', $request->plugin);
  739. $this->assertSame($request->banana, null);
  740. }
  741. /**
  742. * Test isset()/empty() with overloaded properties.
  743. *
  744. * @return void
  745. */
  746. public function testMagicisset() {
  747. $request = new CakeRequest('some/path');
  748. $request->params = array(
  749. 'controller' => 'posts',
  750. 'action' => 'view',
  751. 'plugin' => 'blogs',
  752. 'named' => array()
  753. );
  754. $this->assertTrue(isset($request->controller));
  755. $this->assertFalse(isset($request->notthere));
  756. $this->assertFalse(empty($request->controller));
  757. $this->assertTrue(empty($request->named));
  758. }
  759. /**
  760. * test the array access implementation
  761. *
  762. * @return void
  763. */
  764. public function testArrayAccess() {
  765. $request = new CakeRequest('some/path');
  766. $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
  767. $this->assertEquals('posts', $request['controller']);
  768. $request['slug'] = 'speedy-slug';
  769. $this->assertEquals('speedy-slug', $request->slug);
  770. $this->assertEquals('speedy-slug', $request['slug']);
  771. $this->assertTrue(isset($request['action']));
  772. $this->assertFalse(isset($request['wrong-param']));
  773. $this->assertTrue(isset($request['plugin']));
  774. unset($request['plugin']);
  775. $this->assertFalse(isset($request['plugin']));
  776. $this->assertNull($request['plugin']);
  777. $this->assertNull($request->plugin);
  778. $request = new CakeRequest('some/path?one=something&two=else');
  779. $this->assertTrue(isset($request['url']['one']));
  780. $request->data = array('Post' => array('title' => 'something'));
  781. $this->assertEquals('something', $request['data']['Post']['title']);
  782. }
  783. /**
  784. * test adding detectors and having them work.
  785. *
  786. * @return void
  787. */
  788. public function testAddDetector() {
  789. $request = new CakeRequest('some/path');
  790. $request->addDetector('compare', array('env' => 'TEST_VAR', 'value' => 'something'));
  791. $_SERVER['TEST_VAR'] = 'something';
  792. $this->assertTrue($request->is('compare'), 'Value match failed.');
  793. $_SERVER['TEST_VAR'] = 'wrong';
  794. $this->assertFalse($request->is('compare'), 'Value mis-match failed.');
  795. $request->addDetector('compareCamelCase', array('env' => 'TEST_VAR', 'value' => 'foo'));
  796. $_SERVER['TEST_VAR'] = 'foo';
  797. $this->assertTrue($request->is('compareCamelCase'), 'Value match failed.');
  798. $request->addDetector('banana', array('env' => 'TEST_VAR', 'pattern' => '/^ban.*$/'));
  799. $_SERVER['TEST_VAR'] = 'banana';
  800. $this->assertTrue($request->isBanana());
  801. $_SERVER['TEST_VAR'] = 'wrong value';
  802. $this->assertFalse($request->isBanana());
  803. $request->addDetector('mobile', array('options' => array('Imagination')));
  804. $_SERVER['HTTP_USER_AGENT'] = 'Imagination land';
  805. $this->assertTrue($request->isMobile());
  806. $_SERVER['HTTP_USER_AGENT'] = 'iPhone 3.0';
  807. $this->assertTrue($request->isMobile());
  808. $request->addDetector('callme', array('env' => 'TEST_VAR', 'callback' => array($this, 'detectCallback')));
  809. $request->addDetector('index', array('param' => 'action', 'value' => 'index'));
  810. $request->params['action'] = 'index';
  811. $this->assertTrue($request->isIndex());
  812. $request->params['action'] = 'add';
  813. $this->assertFalse($request->isIndex());
  814. $request->return = true;
  815. $this->assertTrue($request->isCallMe());
  816. $request->return = false;
  817. $this->assertFalse($request->isCallMe());
  818. }
  819. /**
  820. * helper function for testing callbacks.
  821. *
  822. * @return void
  823. */
  824. public function detectCallback($request) {
  825. return $request->return == true;
  826. }
  827. /**
  828. * test getting headers
  829. *
  830. * @return void
  831. */
  832. public function testHeader() {
  833. $_SERVER['HTTP_HOST'] = 'localhost';
  834. $_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';
  835. $request = new CakeRequest('/', false);
  836. $this->assertEquals($_SERVER['HTTP_HOST'], $request->header('host'));
  837. $this->assertEquals($_SERVER['HTTP_USER_AGENT'], $request->header('User-Agent'));
  838. }
  839. /**
  840. * test accepts() with and without parameters
  841. *
  842. * @return void
  843. */
  844. public function testAccepts() {
  845. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml;q=0.9,application/xhtml+xml,text/html,text/plain,image/png';
  846. $request = new CakeRequest('/', false);
  847. $result = $request->accepts();
  848. $expected = array(
  849. 'text/xml', 'application/xhtml+xml', 'text/html', 'text/plain', 'image/png', 'application/xml'
  850. );
  851. $this->assertEquals($expected, $result, 'Content types differ.');
  852. $result = $request->accepts('text/html');
  853. $this->assertTrue($result);
  854. $result = $request->accepts('image/gif');
  855. $this->assertFalse($result);
  856. }
  857. /**
  858. * Test that accept header types are trimmed for comparisons.
  859. *
  860. * @return void
  861. */
  862. public function testAcceptWithWhitespace() {
  863. $_SERVER['HTTP_ACCEPT'] = 'text/xml , text/html , text/plain,image/png';
  864. $request = new CakeRequest('/', false);
  865. $result = $request->accepts();
  866. $expected = array(
  867. 'text/xml', 'text/html', 'text/plain', 'image/png'
  868. );
  869. $this->assertEquals($expected, $result, 'Content types differ.');
  870. $this->assertTrue($request->accepts('text/html'));
  871. }
  872. /**
  873. * Content types from accepts() should respect the client's q preference values.
  874. *
  875. * @return void
  876. */
  877. public function testAcceptWithQvalueSorting() {
  878. $_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0';
  879. $request = new CakeRequest('/', false);
  880. $result = $request->accepts();
  881. $expected = array('application/xml', 'text/html', 'application/json');
  882. $this->assertEquals($expected, $result);
  883. }
  884. /**
  885. * Test the raw parsing of accept headers into the q value formatting.
  886. *
  887. * @return void
  888. */
  889. public function testParseAcceptWithQValue() {
  890. $_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0,image/png';
  891. $request = new CakeRequest('/', false);
  892. $result = $request->parseAccept();
  893. $expected = array(
  894. '1.0' => array('application/xml', 'image/png'),
  895. '0.8' => array('text/html'),
  896. '0.7' => array('application/json'),
  897. );
  898. $this->assertEquals($expected, $result);
  899. }
  900. /**
  901. * Test parsing accept with a confusing accept value.
  902. *
  903. * @return void
  904. */
  905. public function testParseAcceptNoQValues() {
  906. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/plain, */*';
  907. $request = new CakeRequest('/', false);
  908. $result = $request->parseAccept();
  909. $expected = array(
  910. '1.0' => array('application/json', 'text/plain', '*/*'),
  911. );
  912. $this->assertEquals($expected, $result);
  913. }
  914. /**
  915. * testBaseUrlAndWebrootWithModRewrite method
  916. *
  917. * @return void
  918. */
  919. public function testBaseUrlAndWebrootWithModRewrite() {
  920. Configure::write('App.baseUrl', false);
  921. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  922. $_SERVER['PHP_SELF'] = '/1.2.x.x/app/webroot/index.php';
  923. $_SERVER['PATH_INFO'] = '/posts/view/1';
  924. $request = new CakeRequest();
  925. $this->assertEquals('/1.2.x.x', $request->base);
  926. $this->assertEquals('/1.2.x.x/', $request->webroot);
  927. $this->assertEquals('posts/view/1', $request->url);
  928. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/app/webroot';
  929. $_SERVER['PHP_SELF'] = '/index.php';
  930. $_SERVER['PATH_INFO'] = '/posts/add';
  931. $request = new CakeRequest();
  932. $this->assertEquals('', $request->base);
  933. $this->assertEquals('/', $request->webroot);
  934. $this->assertEquals('posts/add', $request->url);
  935. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/test/';
  936. $_SERVER['PHP_SELF'] = '/webroot/index.php';
  937. $request = new CakeRequest();
  938. $this->assertEquals('', $request->base);
  939. $this->assertEquals('/', $request->webroot);
  940. $_SERVER['DOCUMENT_ROOT'] = '/some/apps/where';
  941. $_SERVER['PHP_SELF'] = '/app/webroot/index.php';
  942. $request = new CakeRequest();
  943. $this->assertEquals('', $request->base);
  944. $this->assertEquals('/', $request->webroot);
  945. Configure::write('App.dir', 'auth');
  946. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  947. $_SERVER['PHP_SELF'] = '/demos/auth/webroot/index.php';
  948. $request = new CakeRequest();
  949. $this->assertEquals('/demos/auth', $request->base);
  950. $this->assertEquals('/demos/auth/', $request->webroot);
  951. Configure::write('App.dir', 'code');
  952. $_SERVER['DOCUMENT_ROOT'] = '/Library/WebServer/Documents';
  953. $_SERVER['PHP_SELF'] = '/clients/PewterReport/code/webroot/index.php';
  954. $request = new CakeRequest();
  955. $this->assertEquals('/clients/PewterReport/code', $request->base);
  956. $this->assertEquals('/clients/PewterReport/code/', $request->webroot);
  957. }
  958. /**
  959. * testBaseUrlwithModRewriteAlias method
  960. *
  961. * @return void
  962. */
  963. public function testBaseUrlwithModRewriteAlias() {
  964. $_SERVER['DOCUMENT_ROOT'] = '/home/aplusnur/public_html';
  965. $_SERVER['PHP_SELF'] = '/control/index.php';
  966. Configure::write('App.base', '/control');
  967. $request = new CakeRequest();
  968. $this->assertEquals('/control', $request->base);
  969. $this->assertEquals('/control/', $request->webroot);
  970. Configure::write('App.base', false);
  971. Configure::write('App.dir', 'affiliate');
  972. Configure::write('App.webroot', 'newaffiliate');
  973. $_SERVER['DOCUMENT_ROOT'] = '/var/www/abtravaff/html';
  974. $_SERVER['PHP_SELF'] = '/newaffiliate/index.php';
  975. $request = new CakeRequest();
  976. $this->assertEquals('/newaffiliate', $request->base);
  977. $this->assertEquals('/newaffiliate/', $request->webroot);
  978. }
  979. /**
  980. * test base, webroot, and url parsing when there is no url rewriting
  981. *
  982. * @return void
  983. */
  984. public function testBaseUrlWithNoModRewrite() {
  985. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites';
  986. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake/index.php';
  987. $_SERVER['PHP_SELF'] = '/cake/index.php/posts/index';
  988. $_SERVER['REQUEST_URI'] = '/cake/index.php/posts/index';
  989. Configure::write('App', array(
  990. 'dir' => APP_DIR,
  991. 'webroot' => WEBROOT_DIR,
  992. 'base' => false,
  993. 'baseUrl' => '/cake/index.php'
  994. ));
  995. $request = new CakeRequest();
  996. $this->assertEquals('/cake/index.php', $request->base);
  997. $this->assertEquals('/cake/app/webroot/', $request->webroot);
  998. $this->assertEquals('posts/index', $request->url);
  999. }
  1000. /**
  1001. * testBaseUrlAndWebrootWithBaseUrl method
  1002. *
  1003. * @return void
  1004. */
  1005. public function testBaseUrlAndWebrootWithBaseUrl() {
  1006. Configure::write('App.dir', 'app');
  1007. Configure::write('App.baseUrl', '/app/webroot/index.php');
  1008. $request = new CakeRequest();
  1009. $this->assertEquals('/app/webroot/index.php', $request->base);
  1010. $this->assertEquals('/app/webroot/', $request->webroot);
  1011. Configure::write('App.baseUrl', '/app/webroot/test.php');
  1012. $request = new CakeRequest();
  1013. $this->assertEquals('/app/webroot/test.php', $request->base);
  1014. $this->assertEquals('/app/webroot/', $request->webroot);
  1015. Configure::write('App.baseUrl', '/app/index.php');
  1016. $request = new CakeRequest();
  1017. $this->assertEquals('/app/index.php', $request->base);
  1018. $this->assertEquals('/app/webroot/', $request->webroot);
  1019. Configure::write('App.baseUrl', '/CakeBB/app/webroot/index.php');
  1020. $request = new CakeRequest();
  1021. $this->assertEquals('/CakeBB/app/webroot/index.php', $request->base);
  1022. $this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
  1023. Configure::write('App.baseUrl', '/CakeBB/app/index.php');
  1024. $request = new CakeRequest();
  1025. $this->assertEquals('/CakeBB/app/index.php', $request->base);
  1026. $this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
  1027. Configure::write('App.baseUrl', '/CakeBB/index.php');
  1028. $request = new CakeRequest();
  1029. $this->assertEquals('/CakeBB/index.php', $request->base);
  1030. $this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
  1031. Configure::write('App.baseUrl', '/dbhauser/index.php');
  1032. $_SERVER['DOCUMENT_ROOT'] = '/kunden/homepages/4/d181710652/htdocs/joomla';
  1033. $_SERVER['SCRIPT_FILENAME'] = '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php';
  1034. $request = new CakeRequest();
  1035. $this->assertEquals('/dbhauser/index.php', $request->base);
  1036. $this->assertEquals('/dbhauser/app/webroot/', $request->webroot);
  1037. }
  1038. /**
  1039. * test baseUrl with no rewrite and using the top level index.php.
  1040. *
  1041. * @return void
  1042. */
  1043. public function testBaseUrlNoRewriteTopLevelIndex() {
  1044. Configure::write('App.baseUrl', '/index.php');
  1045. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev';
  1046. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/index.php';
  1047. $request = new CakeRequest();
  1048. $this->assertEquals('/index.php', $request->base);
  1049. $this->assertEquals('/app/webroot/', $request->webroot);
  1050. }
  1051. /**
  1052. * Check that a sub-directory containing app|webroot doesn't get mishandled when re-writing is off.
  1053. *
  1054. * @return void
  1055. */
  1056. public function testBaseUrlWithAppAndWebrootInDirname() {
  1057. Configure::write('App.baseUrl', '/approval/index.php');
  1058. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
  1059. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/approval/index.php';
  1060. $request = new CakeRequest();
  1061. $this->assertEquals('/approval/index.php', $request->base);
  1062. $this->assertEquals('/approval/app/webroot/', $request->webroot);
  1063. Configure::write('App.baseUrl', '/webrootable/index.php');
  1064. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
  1065. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/webrootable/index.php';
  1066. $request = new CakeRequest();
  1067. $this->assertEquals('/webrootable/index.php', $request->base);
  1068. $this->assertEquals('/webrootable/app/webroot/', $request->webroot);
  1069. }
  1070. /**
  1071. * test baseUrl with no rewrite, and using the app/webroot/index.php file as is normal with virtual hosts.
  1072. *
  1073. * @return void
  1074. */
  1075. public function testBaseUrlNoRewriteWebrootIndex() {
  1076. Configure::write('App.baseUrl', '/index.php');
  1077. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev/app/webroot';
  1078. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/app/webroot/index.php';
  1079. $request = new CakeRequest();
  1080. $this->assertEquals('/index.php', $request->base);
  1081. $this->assertEquals('/', $request->webroot);
  1082. }
  1083. /**
  1084. * Test that a request with a . in the main GET parameter is filtered out.
  1085. * PHP changes GET parameter keys containing dots to _.
  1086. *
  1087. * @return void
  1088. */
  1089. public function testGetParamsWithDot() {
  1090. $_GET = array();
  1091. $_GET['/posts/index/add_add'] = '';
  1092. $_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
  1093. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/index/add.add';
  1094. $request = new CakeRequest();
  1095. $this->assertEquals(array(), $request->query);
  1096. }
  1097. /**
  1098. * Test that a request with urlencoded bits in the main GET parameter are filtered out.
  1099. *
  1100. * @return void
  1101. */
  1102. public function testGetParamWithUrlencodedElement() {
  1103. $_GET = array();
  1104. $_GET['/posts/add/∂∂'] = '';
  1105. $_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
  1106. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/add/%E2%88%82%E2%88%82';
  1107. $request = new CakeRequest();
  1108. $this->assertEquals(array(), $request->query);
  1109. }
  1110. /**
  1111. * generator for environment configurations
  1112. *
  1113. * @return void
  1114. */
  1115. public static function environmentGenerator() {
  1116. return array(
  1117. array(
  1118. 'IIS - No rewrite base path',
  1119. array(
  1120. 'App' => array(
  1121. 'base' => false,
  1122. 'baseUrl' => '/index.php',
  1123. 'dir' => 'app',
  1124. 'webroot' => 'webroot'
  1125. ),
  1126. 'SERVER' => array(
  1127. 'SCRIPT_NAME' => '/index.php',
  1128. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1129. 'QUERY_STRING' => '',
  1130. 'REQUEST_URI' => '/index.php',
  1131. 'URL' => '/index.php',
  1132. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php',
  1133. 'ORIG_PATH_INFO' => '/index.php',
  1134. 'PATH_INFO' => '',
  1135. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php',
  1136. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1137. 'PHP_SELF' => '/index.php',
  1138. ),
  1139. ),
  1140. array(
  1141. 'base' => '/index.php',
  1142. 'webroot' => '/app/webroot/',
  1143. 'url' => ''
  1144. ),
  1145. ),
  1146. array(
  1147. 'IIS - No rewrite with path, no PHP_SELF',
  1148. array(
  1149. 'App' => array(
  1150. 'base' => false,
  1151. 'baseUrl' => '/index.php?',
  1152. 'dir' => 'app',
  1153. 'webroot' => 'webroot'
  1154. ),
  1155. 'SERVER' => array(
  1156. 'QUERY_STRING' => '/posts/add',
  1157. 'REQUEST_URI' => '/index.php?/posts/add',
  1158. 'PHP_SELF' => '',
  1159. 'URL' => '/index.php?/posts/add',
  1160. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1161. 'argv' => array('/posts/add'),
  1162. 'argc' => 1
  1163. ),
  1164. ),
  1165. array(
  1166. 'url' => 'posts/add',
  1167. 'base' => '/index.php?',
  1168. 'webroot' => '/app/webroot/'
  1169. )
  1170. ),
  1171. array(
  1172. 'IIS - No rewrite sub dir 2',
  1173. array(
  1174. 'App' => array(
  1175. 'base' => false,
  1176. 'baseUrl' => '/site/index.php',
  1177. 'dir' => 'app',
  1178. 'webroot' => 'webroot',
  1179. ),
  1180. 'SERVER' => array(
  1181. 'SCRIPT_NAME' => '/site/index.php',
  1182. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1183. 'QUERY_STRING' => '',
  1184. 'REQUEST_URI' => '/site/index.php',
  1185. 'URL' => '/site/index.php',
  1186. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1187. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1188. 'PHP_SELF' => '/site/index.php',
  1189. 'argv' => array(),
  1190. 'argc' => 0
  1191. ),
  1192. ),
  1193. array(
  1194. 'url' => '',
  1195. 'base' => '/site/index.php',
  1196. 'webroot' => '/site/app/webroot/'
  1197. ),
  1198. ),
  1199. array(
  1200. 'IIS - No rewrite sub dir 2 with path',
  1201. array(
  1202. 'App' => array(
  1203. 'base' => false,
  1204. 'baseUrl' => '/site/index.php',
  1205. 'dir' => 'app',
  1206. 'webroot' => 'webroot'
  1207. ),
  1208. 'GET' => array('/posts/add' => ''),
  1209. 'SERVER' => array(
  1210. 'SCRIPT_NAME' => '/site/index.php',
  1211. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1212. 'QUERY_STRING' => '/posts/add',
  1213. 'REQUEST_URI' => '/site/index.php/posts/add',
  1214. 'URL' => '/site/index.php/posts/add',
  1215. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1216. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1217. 'PHP_SELF' => '/site/index.php/posts/add',
  1218. 'argv' => array('/posts/add'),
  1219. 'argc' => 1
  1220. ),
  1221. ),
  1222. array(
  1223. 'url' => 'posts/add',
  1224. 'base' => '/site/index.php',
  1225. 'webroot' => '/site/app/webroot/'
  1226. )
  1227. ),
  1228. array(
  1229. 'Apache - No rewrite, document root set to webroot, requesting path',
  1230. array(
  1231. 'App' => array(
  1232. 'base' => false,
  1233. 'baseUrl' => '/index.php',
  1234. 'dir' => 'app',
  1235. 'webroot' => 'webroot'
  1236. ),
  1237. 'SERVER' => array(
  1238. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1239. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1240. 'QUERY_STRING' => '',
  1241. 'REQUEST_URI' => '/index.php/posts/index',
  1242. 'SCRIPT_NAME' => '/index.php',
  1243. 'PATH_INFO' => '/posts/index',
  1244. 'PHP_SELF' => '/index.php/posts/index',
  1245. ),
  1246. ),
  1247. array(
  1248. 'url' => 'posts/index',
  1249. 'base' => '/index.php',
  1250. 'webroot' => '/'
  1251. ),
  1252. ),
  1253. array(
  1254. 'Apache - No rewrite, document root set to webroot, requesting root',
  1255. array(
  1256. 'App' => array(
  1257. 'base' => false,
  1258. 'baseUrl' => '/index.php',
  1259. 'dir' => 'app',
  1260. 'webroot' => 'webroot'
  1261. ),
  1262. 'SERVER' => array(
  1263. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1264. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1265. 'QUERY_STRING' => '',
  1266. 'REQUEST_URI' => '/index.php',
  1267. 'SCRIPT_NAME' => '/index.php',
  1268. 'PATH_INFO' => '',
  1269. 'PHP_SELF' => '/index.php',
  1270. ),
  1271. ),
  1272. array(
  1273. 'url' => '',
  1274. 'base' => '/index.php',
  1275. 'webroot' => '/'
  1276. ),
  1277. ),
  1278. array(
  1279. 'Apache - No rewrite, document root set above top level cake dir, requesting path',
  1280. array(
  1281. 'App' => array(
  1282. 'base' => false,
  1283. 'baseUrl' => '/site/index.php',
  1284. 'dir' => 'app',
  1285. 'webroot' => 'webroot'
  1286. ),
  1287. 'SERVER' => array(
  1288. 'SERVER_NAME' => 'localhost',
  1289. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1290. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1291. 'REQUEST_URI' => '/site/index.php/posts/index',
  1292. 'SCRIPT_NAME' => '/site/index.php',
  1293. 'PATH_INFO' => '/posts/index',
  1294. 'PHP_SELF' => '/site/index.php/posts/index',
  1295. ),
  1296. ),
  1297. array(
  1298. 'url' => 'posts/index',
  1299. 'base' => '/site/index.php',
  1300. 'webroot' => '/site/app/webroot/',
  1301. ),
  1302. ),
  1303. array(
  1304. 'Apache - No rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1305. array(
  1306. 'App' => array(
  1307. 'base' => false,
  1308. 'baseUrl' => '/site/index.php',
  1309. 'dir' => 'app',
  1310. 'webroot' => 'webroot'
  1311. ),
  1312. 'SERVER' => array(
  1313. 'SERVER_NAME' => 'localhost',
  1314. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1315. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1316. 'REQUEST_URI' => '/site/index.php/',
  1317. 'SCRIPT_NAME' => '/site/index.php',
  1318. 'PHP_SELF' => '/site/index.php/',
  1319. ),
  1320. ),
  1321. array(
  1322. 'url' => '',
  1323. 'base' => '/site/index.php',
  1324. 'webroot' => '/site/app/webroot/',
  1325. ),
  1326. ),
  1327. array(
  1328. 'Apache - No rewrite, document root set above top level cake dir, request path, with GET',
  1329. array(
  1330. 'App' => array(
  1331. 'base' => false,
  1332. 'baseUrl' => '/site/index.php',
  1333. 'dir' => 'app',
  1334. 'webroot' => 'webroot'
  1335. ),
  1336. 'GET' => array('a' => 'b', 'c' => 'd'),
  1337. 'SERVER' => array(
  1338. 'SERVER_NAME' => 'localhost',
  1339. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1340. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1341. 'REQUEST_URI' => '/site/index.php/posts/index?a=b&c=d',
  1342. 'SCRIPT_NAME' => '/site/index.php',
  1343. 'PATH_INFO' => '/posts/index',
  1344. 'PHP_SELF' => '/site/index.php/posts/index',
  1345. 'QUERY_STRING' => 'a=b&c=d'
  1346. ),
  1347. ),
  1348. array(
  1349. 'urlParams' => array('a' => 'b', 'c' => 'd'),
  1350. 'url' => 'posts/index',
  1351. 'base' => '/site/index.php',
  1352. 'webroot' => '/site/app/webroot/',
  1353. ),
  1354. ),
  1355. array(
  1356. 'Apache - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1357. array(
  1358. 'App' => array(
  1359. 'base' => false,
  1360. 'baseUrl' => false,
  1361. 'dir' => 'app',
  1362. 'webroot' => 'webroot'
  1363. ),
  1364. 'SERVER' => array(
  1365. 'SERVER_NAME' => 'localhost',
  1366. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1367. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1368. 'REQUEST_URI' => '/site/',
  1369. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1370. 'PHP_SELF' => '/site/app/webroot/index.php',
  1371. ),
  1372. ),
  1373. array(
  1374. 'url' => '',
  1375. 'base' => '/site',
  1376. 'webroot' => '/site/',
  1377. ),
  1378. ),
  1379. array(
  1380. 'Apache - w/rewrite, document root above top level cake dir, request root, no PATH_INFO/REQUEST_URI',
  1381. array(
  1382. 'App' => array(
  1383. 'base' => false,
  1384. 'baseUrl' => false,
  1385. 'dir' => 'app',
  1386. 'webroot' => 'webroot'
  1387. ),
  1388. 'SERVER' => array(
  1389. 'SERVER_NAME' => 'localhost',
  1390. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1391. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1392. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1393. 'PHP_SELF' => '/site/app/webroot/index.php',
  1394. 'PATH_INFO' => null,
  1395. 'REQUEST_URI' => null,
  1396. ),
  1397. ),
  1398. array(
  1399. 'url' => '',
  1400. 'base' => '/site',
  1401. 'webroot' => '/site/',
  1402. ),
  1403. ),
  1404. array(
  1405. 'Apache - w/rewrite, document root set to webroot, request root, no PATH_INFO/REQUEST_URI',
  1406. array(
  1407. 'App' => array(
  1408. 'base' => false,
  1409. 'baseUrl' => false,
  1410. 'dir' => 'app',
  1411. 'webroot' => 'webroot'
  1412. ),
  1413. 'SERVER' => array(
  1414. 'SERVER_NAME' => 'localhost',
  1415. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1416. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1417. 'SCRIPT_NAME' => '/index.php',
  1418. 'PHP_SELF' => '/index.php',
  1419. 'PATH_INFO' => null,
  1420. 'REQUEST_URI' => null,
  1421. ),
  1422. ),
  1423. array(
  1424. 'url' => '',
  1425. 'base' => '',
  1426. 'webroot' => '/',
  1427. ),
  1428. ),
  1429. array(
  1430. 'Nginx - w/rewrite, document root set to webroot, request root, no PATH_INFO',
  1431. array(
  1432. 'App' => array(
  1433. 'base' => false,
  1434. 'baseUrl' => false,
  1435. 'dir' => 'app',
  1436. 'webroot' => 'webroot'
  1437. ),
  1438. 'GET' => array('/posts/add' => ''),
  1439. 'SERVER' => array(
  1440. 'SERVER_NAME' => 'localhost',
  1441. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1442. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1443. 'SCRIPT_NAME' => '/index.php',
  1444. 'QUERY_STRING' => '/posts/add&',
  1445. 'PHP_SELF' => '/index.php',
  1446. 'PATH_INFO' => null,
  1447. 'REQUEST_URI' => '/posts/add',
  1448. ),
  1449. ),
  1450. array(
  1451. 'url' => 'posts/add',
  1452. 'base' => '',
  1453. 'webroot' => '/',
  1454. 'urlParams' => array()
  1455. ),
  1456. ),
  1457. );
  1458. }
  1459. /**
  1460. * testEnvironmentDetection method
  1461. *
  1462. * @dataProvider environmentGenerator
  1463. * @return void
  1464. */
  1465. public function testEnvironmentDetection($name, $env, $expected) {
  1466. $_GET = array();
  1467. $this->__loadEnvironment($env);
  1468. $request = new CakeRequest();
  1469. $this->assertEquals($expected['url'], $request->url, "url error");
  1470. $this->assertEquals($expected['base'], $request->base, "base error");
  1471. $this->assertEquals($expected['webroot'], $request->webroot, "webroot error");
  1472. if (isset($expected['urlParams'])) {
  1473. $this->assertEquals($expected['urlParams'], $request->query, "GET param mismatch");
  1474. }
  1475. }
  1476. /**
  1477. * test the data() method reading
  1478. *
  1479. * @return void
  1480. */
  1481. public function testDataReading() {
  1482. $_POST['data'] = array(
  1483. 'Model' => array(
  1484. 'field' => 'value'
  1485. )
  1486. );
  1487. $request = new CakeRequest('posts/index');
  1488. $result = $request->data('Model');
  1489. $this->assertEquals($_POST['data']['Model'], $result);
  1490. $result = $request->data('Model.imaginary');
  1491. $this->assertNull($result);
  1492. }
  1493. /**
  1494. * test writing with data()
  1495. *
  1496. * @return void
  1497. */
  1498. public function testDataWriting() {
  1499. $_POST['data'] = array(
  1500. 'Model' => array(
  1501. 'field' => 'value'
  1502. )
  1503. );
  1504. $request = new CakeRequest('posts/index');
  1505. $result = $request->data('Model.new_value', 'new value');
  1506. $this->assertSame($result, $request, 'Return was not $this');
  1507. $this->assertEquals('new value', $request->data['Model']['new_value']);
  1508. $request->data('Post.title', 'New post')->data('Comment.1.author', 'Mark');
  1509. $this->assertEquals('New post', $request->data['Post']['title']);
  1510. $this->assertEquals('Mark', $request->data['Comment']['1']['author']);
  1511. }
  1512. /**
  1513. * test writing falsey values.
  1514. *
  1515. * @return void
  1516. */
  1517. public function testDataWritingFalsey() {
  1518. $request = new CakeRequest('posts/index');
  1519. $request->data('Post.null', null);
  1520. $this->assertNull($request->data['Post']['null']);
  1521. $request->data('Post.false', false);
  1522. $this->assertFalse($request->data['Post']['false']);
  1523. $request->data('Post.zero', 0);
  1524. $this->assertSame(0, $request->data['Post']['zero']);
  1525. $request->data('Post.empty', '');
  1526. $this->assertSame('', $request->data['Post']['empty']);
  1527. }
  1528. /**
  1529. * test accept language
  1530. *
  1531. * @return void
  1532. */
  1533. public function testAcceptLanguage() {
  1534. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
  1535. $result = CakeRequest::acceptLanguage();
  1536. $this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
  1537. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx;en_ca';
  1538. $result = CakeRequest::acceptLanguage();
  1539. $this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
  1540. $result = CakeRequest::acceptLanguage('en-ca');
  1541. $this->assertTrue($result);
  1542. $result = CakeRequest::acceptLanguage('en-us');
  1543. $this->assertFalse($result);
  1544. }
  1545. /**
  1546. * test the here() method
  1547. *
  1548. * @return void
  1549. */
  1550. public function testHere() {
  1551. Configure::write('App.base', '/base_path');
  1552. $_GET = array('test' => 'value');
  1553. $request = new CakeRequest('/posts/add/1/name:value');
  1554. $result = $request->here();
  1555. $this->assertEquals('/base_path/posts/add/1/name:value?test=value', $result);
  1556. $result = $request->here(false);
  1557. $this->assertEquals('/posts/add/1/name:value?test=value', $result);
  1558. $request = new CakeRequest('/posts/base_path/1/name:value');
  1559. $result = $request->here();
  1560. $this->assertEquals('/base_path/posts/base_path/1/name:value?test=value', $result);
  1561. $result = $request->here(false);
  1562. $this->assertEquals('/posts/base_path/1/name:value?test=value', $result);
  1563. }
  1564. /**
  1565. * Test the input() method.
  1566. *
  1567. * @return void
  1568. */
  1569. public function testInput() {
  1570. $request = $this->getMock('CakeRequest', array('_readInput'));
  1571. $request->expects($this->once())->method('_readInput')
  1572. ->will($this->returnValue('I came from stdin'));
  1573. $result = $request->input();
  1574. $this->assertEquals('I came from stdin', $result);
  1575. }
  1576. /**
  1577. * Test input() decoding.
  1578. *
  1579. * @return void
  1580. */
  1581. public function testInputDecode() {
  1582. $request = $this->getMock('CakeRequest', array('_readInput'));
  1583. $request->expects($this->once())->method('_readInput')
  1584. ->will($this->returnValue('{"name":"value"}'));
  1585. $result = $request->input('json_decode');
  1586. $this->assertEquals(array('name' => 'value'), (array)$result);
  1587. }
  1588. /**
  1589. * Test input() decoding with additional arguments.
  1590. *
  1591. * @return void
  1592. */
  1593. public function testInputDecodeExtraParams() {
  1594. $xml = <<<XML
  1595. <?xml version="1.0" encoding="utf-8"?>
  1596. <post>
  1597. <title id="title">Test</title>
  1598. </post>
  1599. XML;
  1600. $request = $this->getMock('CakeRequest', array('_readInput'));
  1601. $request->expects($this->once())->method('_readInput')
  1602. ->will($this->returnValue($xml));
  1603. $result = $request->input('Xml::build', array('return' => 'domdocument'));
  1604. $this->assertInstanceOf('DOMDocument', $result);
  1605. $this->assertEquals(
  1606. 'Test',
  1607. $result->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText
  1608. );
  1609. }
  1610. /**
  1611. * Test is('requested') and isRequested()
  1612. *
  1613. * @return void
  1614. */
  1615. public function testIsRequested() {
  1616. $request = new CakeRequest('/posts/index');
  1617. $request->addParams(array(
  1618. 'controller' => 'posts',
  1619. 'action' => 'index',
  1620. 'plugin' => null,
  1621. 'requested' => 1
  1622. ));
  1623. $this->assertTrue($request->is('requested'));
  1624. $this->assertTrue($request->isRequested());
  1625. $request = new CakeRequest('/posts/index');
  1626. $request->addParams(array(
  1627. 'controller' => 'posts',
  1628. 'action' => 'index',
  1629. 'plugin' => null,
  1630. ));
  1631. $this->assertFalse($request->is('requested'));
  1632. $this->assertFalse($request->isRequested());
  1633. }
  1634. /**
  1635. * loadEnvironment method
  1636. *
  1637. * @param array $env
  1638. * @return void
  1639. */
  1640. protected function __loadEnvironment($env) {
  1641. if (isset($env['App'])) {
  1642. Configure::write('App', $env['App']);
  1643. }
  1644. if (isset($env['GET'])) {
  1645. foreach ($env['GET'] as $key => $val) {
  1646. $_GET[$key] = $val;
  1647. }
  1648. }
  1649. if (isset($env['POST'])) {
  1650. foreach ($env['POST'] as $key => $val) {
  1651. $_POST[$key] = $val;
  1652. }
  1653. }
  1654. if (isset($env['SERVER'])) {
  1655. foreach ($env['SERVER'] as $key => $val) {
  1656. $_SERVER[$key] = $val;
  1657. }
  1658. }
  1659. }
  1660. }