RequestTest.php 62 KB

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