RequestTest.php 63 KB

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