RequestTest.php 64 KB

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