CakeRequestTest.php 51 KB

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