RequestTest.php 61 KB

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