CakeRequestTest.php 51 KB

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