RequestTest.php 62 KB

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