CakeRequestTest.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  1. <?php
  2. /**
  3. * CakeRequest Test case file.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Test.Case.Network
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Dispatcher', 'Routing');
  20. App::uses('Xml', 'Utility');
  21. App::uses('CakeRequest', 'Network');
  22. class CakeRequestTest extends CakeTestCase {
  23. /**
  24. * setup callback
  25. *
  26. * @return void
  27. */
  28. public function setUp() {
  29. parent::setUp();
  30. $this->_server = $_SERVER;
  31. $this->_get = $_GET;
  32. $this->_post = $_POST;
  33. $this->_files = $_FILES;
  34. $this->_app = Configure::read('App');
  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. $_SERVER = $this->_server;
  50. $_GET = $this->_get;
  51. $_POST = $this->_post;
  52. $_FILES = $this->_files;
  53. if (!empty($this->_case)) {
  54. $_GET['case'] = $this->_case;
  55. }
  56. Configure::write('App', $this->_app);
  57. }
  58. /**
  59. * test that the autoparse = false constructor works.
  60. *
  61. * @return void
  62. */
  63. public function testNoAutoParseConstruction() {
  64. $_GET = array(
  65. 'one' => 'param'
  66. );
  67. $request = new CakeRequest(null, false);
  68. $this->assertFalse(isset($request->query['one']));
  69. }
  70. /**
  71. * test construction
  72. *
  73. * @return void
  74. */
  75. public function testConstructionGetParsing() {
  76. $_GET = array(
  77. 'one' => 'param',
  78. 'two' => 'banana'
  79. );
  80. $request = new CakeRequest('some/path');
  81. $this->assertEquals($request->query, $_GET);
  82. $_GET = array(
  83. 'one' => 'param',
  84. 'two' => 'banana',
  85. );
  86. $request = new CakeRequest('some/path');
  87. $this->assertEquals($request->query, $_GET);
  88. $this->assertEquals($request->url, 'some/path');
  89. }
  90. /**
  91. * Test that querystring args provided in the url string are parsed.
  92. *
  93. * @return void
  94. */
  95. public function testQueryStringParsingFromInputUrl() {
  96. $_GET = array();
  97. $request = new CakeRequest('some/path?one=something&two=else');
  98. $expected = array('one' => 'something', 'two' => 'else');
  99. $this->assertEquals($expected, $request->query);
  100. $this->assertEquals('some/path?one=something&two=else', $request->url);
  101. }
  102. /**
  103. * Test that named arguments + querystrings are handled correctly.
  104. *
  105. * @return void
  106. */
  107. public function testQueryStringAndNamedParams() {
  108. $_SERVER['REQUEST_URI'] = '/tasks/index/page:1?ts=123456';
  109. $request = new CakeRequest();
  110. $this->assertEquals('tasks/index/page:1', $request->url);
  111. $_SERVER['REQUEST_URI'] = '/tasks/index/page:1/?ts=123456';
  112. $request = new CakeRequest();
  113. $this->assertEquals('tasks/index/page:1/', $request->url);
  114. }
  115. /**
  116. * test addParams() method
  117. *
  118. * @return void
  119. */
  120. public function testAddParams() {
  121. $request = new CakeRequest('some/path');
  122. $request->params = array('controller' => 'posts', 'action' => 'view');
  123. $result = $request->addParams(array('plugin' => null, 'action' => 'index'));
  124. $this->assertSame($result, $request, 'Method did not return itself. %s');
  125. $this->assertEquals($request->controller, 'posts');
  126. $this->assertEquals($request->action, 'index');
  127. $this->assertEquals($request->plugin, null);
  128. }
  129. /**
  130. * test splicing in paths.
  131. *
  132. * @return void
  133. */
  134. public function testAddPaths() {
  135. $request = new CakeRequest('some/path');
  136. $request->webroot = '/some/path/going/here/';
  137. $result = $request->addPaths(array(
  138. 'random' => '/something', 'webroot' => '/', 'here' => '/', 'base' => '/base_dir'
  139. ));
  140. $this->assertSame($result, $request, 'Method did not return itself. %s');
  141. $this->assertEquals($request->webroot, '/');
  142. $this->assertEquals($request->base, '/base_dir');
  143. $this->assertEquals($request->here, '/');
  144. $this->assertFalse(isset($request->random));
  145. }
  146. /**
  147. * test parsing POST data into the object.
  148. *
  149. * @return void
  150. */
  151. public function testPostParsing() {
  152. $_POST = array('data' => array(
  153. 'Article' => array('title')
  154. ));
  155. $request = new CakeRequest('some/path');
  156. $this->assertEquals($_POST['data'], $request->data);
  157. $_POST = array('one' => 1, 'two' => 'three');
  158. $request = new CakeRequest('some/path');
  159. $this->assertEquals($_POST, $request->data);
  160. $_POST = array(
  161. 'data' => array(
  162. 'Article' => array('title' => 'Testing'),
  163. ),
  164. 'action' => 'update'
  165. );
  166. $request = new CakeRequest('some/path');
  167. $expected = array(
  168. 'Article' => array('title' => 'Testing'),
  169. 'action' => 'update'
  170. );
  171. $this->assertEquals($expected, $request->data);
  172. $_POST = array('data' => array(
  173. 'Article' => array('title'),
  174. 'Tag' => array('Tag' => array(1, 2))
  175. ));
  176. $request = new CakeRequest('some/path');
  177. $this->assertEquals($_POST['data'], $request->data);
  178. $_POST = array('data' => array(
  179. 'Article' => array('title' => 'some title'),
  180. 'Tag' => array('Tag' => array(1, 2))
  181. ));
  182. $request = new CakeRequest('some/path');
  183. $this->assertEquals($_POST['data'], $request->data);
  184. }
  185. /**
  186. * test parsing of FILES array
  187. *
  188. * @return void
  189. */
  190. public function testFILESParsing() {
  191. $_FILES = array('data' => array('name' => array(
  192. 'File' => array(
  193. array('data' => 'cake_sqlserver_patch.patch'),
  194. array('data' => 'controller.diff'),
  195. array('data' => ''),
  196. array('data' => ''),
  197. ),
  198. 'Post' => array('attachment' => 'jquery-1.2.1.js'),
  199. ),
  200. 'type' => array(
  201. 'File' => array(
  202. array('data' => ''),
  203. array('data' => ''),
  204. array('data' => ''),
  205. array('data' => ''),
  206. ),
  207. 'Post' => array('attachment' => 'application/x-javascript'),
  208. ),
  209. 'tmp_name' => array(
  210. 'File' => array(
  211. array('data' => '/private/var/tmp/phpy05Ywj'),
  212. array('data' => '/private/var/tmp/php7MBztY'),
  213. array('data' => ''),
  214. array('data' => ''),
  215. ),
  216. 'Post' => array('attachment' => '/private/var/tmp/phpEwlrIo'),
  217. ),
  218. 'error' => array(
  219. 'File' => array(
  220. array('data' => 0),
  221. array('data' => 0),
  222. array('data' => 4),
  223. array('data' => 4)
  224. ),
  225. 'Post' => array('attachment' => 0)
  226. ),
  227. 'size' => array(
  228. 'File' => array(
  229. array('data' => 6271),
  230. array('data' => 350),
  231. array('data' => 0),
  232. array('data' => 0),
  233. ),
  234. 'Post' => array('attachment' => 80469)
  235. ),
  236. ));
  237. $request = new CakeRequest('some/path');
  238. $expected = array(
  239. 'File' => array(
  240. array('data' => array(
  241. 'name' => 'cake_sqlserver_patch.patch',
  242. 'type' => '',
  243. 'tmp_name' => '/private/var/tmp/phpy05Ywj',
  244. 'error' => 0,
  245. 'size' => 6271,
  246. )),
  247. array(
  248. 'data' => array(
  249. 'name' => 'controller.diff',
  250. 'type' => '',
  251. 'tmp_name' => '/private/var/tmp/php7MBztY',
  252. 'error' => 0,
  253. 'size' => 350,
  254. )),
  255. array('data' => array(
  256. 'name' => '',
  257. 'type' => '',
  258. 'tmp_name' => '',
  259. 'error' => 4,
  260. 'size' => 0,
  261. )),
  262. array('data' => array(
  263. 'name' => '',
  264. 'type' => '',
  265. 'tmp_name' => '',
  266. 'error' => 4,
  267. 'size' => 0,
  268. )),
  269. ),
  270. 'Post' => array('attachment' => array(
  271. 'name' => 'jquery-1.2.1.js',
  272. 'type' => 'application/x-javascript',
  273. 'tmp_name' => '/private/var/tmp/phpEwlrIo',
  274. 'error' => 0,
  275. 'size' => 80469,
  276. ))
  277. );
  278. $this->assertEquals($request->data, $expected);
  279. $_FILES = array(
  280. 'data' => array(
  281. 'name' => array(
  282. 'Document' => array(
  283. 1 => array(
  284. 'birth_cert' => 'born on.txt',
  285. 'passport' => 'passport.txt',
  286. 'drivers_license' => 'ugly pic.jpg'
  287. ),
  288. 2 => array(
  289. 'birth_cert' => 'aunt betty.txt',
  290. 'passport' => 'betty-passport.txt',
  291. 'drivers_license' => 'betty-photo.jpg'
  292. ),
  293. ),
  294. ),
  295. 'type' => array(
  296. 'Document' => array(
  297. 1 => array(
  298. 'birth_cert' => 'application/octet-stream',
  299. 'passport' => 'application/octet-stream',
  300. 'drivers_license' => 'application/octet-stream',
  301. ),
  302. 2 => array(
  303. 'birth_cert' => 'application/octet-stream',
  304. 'passport' => 'application/octet-stream',
  305. 'drivers_license' => 'application/octet-stream',
  306. )
  307. )
  308. ),
  309. 'tmp_name' => array(
  310. 'Document' => array(
  311. 1 => array(
  312. 'birth_cert' => '/private/var/tmp/phpbsUWfH',
  313. 'passport' => '/private/var/tmp/php7f5zLt',
  314. 'drivers_license' => '/private/var/tmp/phpMXpZgT',
  315. ),
  316. 2 => array(
  317. 'birth_cert' => '/private/var/tmp/php5kHZt0',
  318. 'passport' => '/private/var/tmp/phpnYkOuM',
  319. 'drivers_license' => '/private/var/tmp/php9Rq0P3',
  320. )
  321. )
  322. ),
  323. 'error' => array(
  324. 'Document' => array(
  325. 1 => array(
  326. 'birth_cert' => 0,
  327. 'passport' => 0,
  328. 'drivers_license' => 0,
  329. ),
  330. 2 => array(
  331. 'birth_cert' => 0,
  332. 'passport' => 0,
  333. 'drivers_license' => 0,
  334. )
  335. )
  336. ),
  337. 'size' => array(
  338. 'Document' => array(
  339. 1 => array(
  340. 'birth_cert' => 123,
  341. 'passport' => 458,
  342. 'drivers_license' => 875,
  343. ),
  344. 2 => array(
  345. 'birth_cert' => 876,
  346. 'passport' => 976,
  347. 'drivers_license' => 9783,
  348. )
  349. )
  350. )
  351. )
  352. );
  353. $request = new CakeRequest('some/path');
  354. $expected = array(
  355. 'Document' => array(
  356. 1 => array(
  357. 'birth_cert' => array(
  358. 'name' => 'born on.txt',
  359. 'tmp_name' => '/private/var/tmp/phpbsUWfH',
  360. 'error' => 0,
  361. 'size' => 123,
  362. 'type' => 'application/octet-stream',
  363. ),
  364. 'passport' => array(
  365. 'name' => 'passport.txt',
  366. 'tmp_name' => '/private/var/tmp/php7f5zLt',
  367. 'error' => 0,
  368. 'size' => 458,
  369. 'type' => 'application/octet-stream',
  370. ),
  371. 'drivers_license' => array(
  372. 'name' => 'ugly pic.jpg',
  373. 'tmp_name' => '/private/var/tmp/phpMXpZgT',
  374. 'error' => 0,
  375. 'size' => 875,
  376. 'type' => 'application/octet-stream',
  377. ),
  378. ),
  379. 2 => array(
  380. 'birth_cert' => array(
  381. 'name' => 'aunt betty.txt',
  382. 'tmp_name' => '/private/var/tmp/php5kHZt0',
  383. 'error' => 0,
  384. 'size' => 876,
  385. 'type' => 'application/octet-stream',
  386. ),
  387. 'passport' => array(
  388. 'name' => 'betty-passport.txt',
  389. 'tmp_name' => '/private/var/tmp/phpnYkOuM',
  390. 'error' => 0,
  391. 'size' => 976,
  392. 'type' => 'application/octet-stream',
  393. ),
  394. 'drivers_license' => array(
  395. 'name' => 'betty-photo.jpg',
  396. 'tmp_name' => '/private/var/tmp/php9Rq0P3',
  397. 'error' => 0,
  398. 'size' => 9783,
  399. 'type' => 'application/octet-stream',
  400. ),
  401. ),
  402. )
  403. );
  404. $this->assertEquals($request->data, $expected);
  405. $_FILES = array(
  406. 'data' => array(
  407. 'name' => array('birth_cert' => 'born on.txt'),
  408. 'type' => array('birth_cert' => 'application/octet-stream'),
  409. 'tmp_name' => array('birth_cert' => '/private/var/tmp/phpbsUWfH'),
  410. 'error' => array('birth_cert' => 0),
  411. 'size' => array('birth_cert' => 123)
  412. )
  413. );
  414. $request = new CakeRequest('some/path');
  415. $expected = array(
  416. 'birth_cert' => array(
  417. 'name' => 'born on.txt',
  418. 'type' => 'application/octet-stream',
  419. 'tmp_name' => '/private/var/tmp/phpbsUWfH',
  420. 'error' => 0,
  421. 'size' => 123
  422. )
  423. );
  424. $this->assertEquals($request->data, $expected);
  425. $_FILES = array(
  426. 'something' => array(
  427. 'name' => 'something.txt',
  428. 'type' => 'text/plain',
  429. 'tmp_name' => '/some/file',
  430. 'error' => 0,
  431. 'size' => 123
  432. )
  433. );
  434. $request = new CakeRequest('some/path');
  435. $this->assertEquals($request->params['form'], $_FILES);
  436. }
  437. /**
  438. * test method overrides coming in from POST data.
  439. *
  440. * @return void
  441. */
  442. public function testMethodOverrides() {
  443. $_POST = array('_method' => 'POST');
  444. $request = new CakeRequest('some/path');
  445. $this->assertEquals(env('REQUEST_METHOD'), 'POST');
  446. $_POST = array('_method' => 'DELETE');
  447. $request = new CakeRequest('some/path');
  448. $this->assertEquals(env('REQUEST_METHOD'), 'DELETE');
  449. $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
  450. $request = new CakeRequest('some/path');
  451. $this->assertEquals(env('REQUEST_METHOD'), 'PUT');
  452. }
  453. /**
  454. * test the clientIp method.
  455. *
  456. * @return void
  457. */
  458. public function testclientIp() {
  459. $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.5, 10.0.1.1, proxy.com';
  460. $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
  461. $_SERVER['REMOTE_ADDR'] = '192.168.1.3';
  462. $request = new CakeRequest('some/path');
  463. $this->assertEquals($request->clientIp(false), '192.168.1.5');
  464. $this->assertEquals($request->clientIp(), '192.168.1.2');
  465. unset($_SERVER['HTTP_X_FORWARDED_FOR']);
  466. $this->assertEquals($request->clientIp(), '192.168.1.2');
  467. unset($_SERVER['HTTP_CLIENT_IP']);
  468. $this->assertEquals($request->clientIp(), '192.168.1.3');
  469. $_SERVER['HTTP_CLIENTADDRESS'] = '10.0.1.2, 10.0.1.1';
  470. $this->assertEquals($request->clientIp(), '10.0.1.2');
  471. }
  472. /**
  473. * test the referer function.
  474. *
  475. * @return void
  476. */
  477. public function testReferer() {
  478. $request = new CakeRequest('some/path');
  479. $request->webroot = '/';
  480. $_SERVER['HTTP_REFERER'] = 'http://cakephp.org';
  481. $result = $request->referer();
  482. $this->assertSame($result, 'http://cakephp.org');
  483. $_SERVER['HTTP_REFERER'] = '';
  484. $result = $request->referer();
  485. $this->assertSame($result, '/');
  486. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
  487. $result = $request->referer(true);
  488. $this->assertSame($result, '/some/path');
  489. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
  490. $result = $request->referer(false);
  491. $this->assertSame($result, FULL_BASE_URL . '/some/path');
  492. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
  493. $result = $request->referer(true);
  494. $this->assertSame($result, '/some/path');
  495. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/recipes/add';
  496. $result = $request->referer(true);
  497. $this->assertSame($result, '/recipes/add');
  498. $_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org';
  499. $result = $request->referer();
  500. $this->assertSame($result, 'cakephp.org');
  501. }
  502. /**
  503. * test the simple uses of is()
  504. *
  505. * @return void
  506. */
  507. public function testIsHttpMethods() {
  508. $request = new CakeRequest('some/path');
  509. $this->assertFalse($request->is('undefined-behavior'));
  510. $_SERVER['REQUEST_METHOD'] = 'GET';
  511. $this->assertTrue($request->is('get'));
  512. $_SERVER['REQUEST_METHOD'] = 'POST';
  513. $this->assertTrue($request->is('POST'));
  514. $_SERVER['REQUEST_METHOD'] = 'PUT';
  515. $this->assertTrue($request->is('put'));
  516. $this->assertFalse($request->is('get'));
  517. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  518. $this->assertTrue($request->is('delete'));
  519. $this->assertTrue($request->isDelete());
  520. $_SERVER['REQUEST_METHOD'] = 'delete';
  521. $this->assertFalse($request->is('delete'));
  522. }
  523. /**
  524. * test the method() method.
  525. *
  526. * @return void
  527. */
  528. public function testMethod() {
  529. $_SERVER['REQUEST_METHOD'] = 'delete';
  530. $request = new CakeRequest('some/path');
  531. $this->assertEquals('delete', $request->method());
  532. }
  533. /**
  534. * test host retrieval.
  535. *
  536. * @return void
  537. */
  538. public function testHost() {
  539. $_SERVER['HTTP_HOST'] = 'localhost';
  540. $request = new CakeRequest('some/path');
  541. $this->assertEquals('localhost', $request->host());
  542. }
  543. /**
  544. * test domain retrieval.
  545. *
  546. * @return void
  547. */
  548. public function testDomain() {
  549. $_SERVER['HTTP_HOST'] = 'something.example.com';
  550. $request = new CakeRequest('some/path');
  551. $this->assertEquals('example.com', $request->domain());
  552. $_SERVER['HTTP_HOST'] = 'something.example.co.uk';
  553. $this->assertEquals('example.co.uk', $request->domain(2));
  554. }
  555. /**
  556. * test getting subdomains for a host.
  557. *
  558. * @return void
  559. */
  560. public function testSubdomain() {
  561. $_SERVER['HTTP_HOST'] = 'something.example.com';
  562. $request = new CakeRequest('some/path');
  563. $this->assertEquals(array('something'), $request->subdomains());
  564. $_SERVER['HTTP_HOST'] = 'www.something.example.com';
  565. $this->assertEquals(array('www', 'something'), $request->subdomains());
  566. $_SERVER['HTTP_HOST'] = 'www.something.example.co.uk';
  567. $this->assertEquals(array('www', 'something'), $request->subdomains(2));
  568. $_SERVER['HTTP_HOST'] = 'example.co.uk';
  569. $this->assertEquals(array(), $request->subdomains(2));
  570. }
  571. /**
  572. * test ajax, flash and friends
  573. *
  574. * @return void
  575. */
  576. public function testisAjaxFlashAndFriends() {
  577. $request = new CakeRequest('some/path');
  578. $_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
  579. $this->assertTrue($request->is('flash'));
  580. $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash';
  581. $this->assertTrue($request->is('flash'));
  582. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  583. $this->assertTrue($request->is('ajax'));
  584. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHTTPREQUEST';
  585. $this->assertFalse($request->is('ajax'));
  586. $this->assertFalse($request->isAjax());
  587. $_SERVER['HTTP_USER_AGENT'] = 'Android 2.0';
  588. $this->assertTrue($request->is('mobile'));
  589. $this->assertTrue($request->isMobile());
  590. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 5.1; rv:2.0b6pre) Gecko/20100902 Firefox/4.0b6pre Fennec/2.0b1pre';
  591. $this->assertTrue($request->is('mobile'));
  592. $this->assertTrue($request->isMobile());
  593. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; SAMSUNG; OMNIA7)';
  594. $this->assertTrue($request->is('mobile'));
  595. $this->assertTrue($request->isMobile());
  596. }
  597. /**
  598. * test __call expcetions
  599. *
  600. * @expectedException CakeException
  601. * @return void
  602. */
  603. public function test__callExceptionOnUnknownMethod() {
  604. $request = new CakeRequest('some/path');
  605. $request->IamABanana();
  606. }
  607. /**
  608. * test is(ssl)
  609. *
  610. * @return void
  611. */
  612. public function testIsSsl() {
  613. $request = new CakeRequest('some/path');
  614. $_SERVER['HTTPS'] = 1;
  615. $this->assertTrue($request->is('ssl'));
  616. $_SERVER['HTTPS'] = 'on';
  617. $this->assertTrue($request->is('ssl'));
  618. $_SERVER['HTTPS'] = '1';
  619. $this->assertTrue($request->is('ssl'));
  620. $_SERVER['HTTPS'] = 'I am not empty';
  621. $this->assertTrue($request->is('ssl'));
  622. $_SERVER['HTTPS'] = 1;
  623. $this->assertTrue($request->is('ssl'));
  624. $_SERVER['HTTPS'] = 'off';
  625. $this->assertFalse($request->is('ssl'));
  626. $_SERVER['HTTPS'] = false;
  627. $this->assertFalse($request->is('ssl'));
  628. $_SERVER['HTTPS'] = '';
  629. $this->assertFalse($request->is('ssl'));
  630. }
  631. /**
  632. * test getting request params with object properties.
  633. *
  634. * @return void
  635. */
  636. public function test__get() {
  637. $request = new CakeRequest('some/path');
  638. $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
  639. $this->assertEquals($request->controller, 'posts');
  640. $this->assertEquals($request->action, 'view');
  641. $this->assertEquals($request->plugin, 'blogs');
  642. $this->assertSame($request->banana, null);
  643. }
  644. /**
  645. * Test isset()/empty() with overloaded properties.
  646. *
  647. * @return void
  648. */
  649. public function test__isset() {
  650. $request = new CakeRequest('some/path');
  651. $request->params = array(
  652. 'controller' => 'posts',
  653. 'action' => 'view',
  654. 'plugin' => 'blogs',
  655. 'named' => array()
  656. );
  657. $this->assertTrue(isset($request->controller));
  658. $this->assertFalse(isset($request->notthere));
  659. $this->assertFalse(empty($request->controller));
  660. $this->assertTrue(empty($request->named));
  661. }
  662. /**
  663. * test the array access implementation
  664. *
  665. * @return void
  666. */
  667. public function testArrayAccess() {
  668. $request = new CakeRequest('some/path');
  669. $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
  670. $this->assertEquals($request['controller'], 'posts');
  671. $request['slug'] = 'speedy-slug';
  672. $this->assertEquals($request->slug, 'speedy-slug');
  673. $this->assertEquals($request['slug'], 'speedy-slug');
  674. $this->assertTrue(isset($request['action']));
  675. $this->assertFalse(isset($request['wrong-param']));
  676. $this->assertTrue(isset($request['plugin']));
  677. unset($request['plugin']);
  678. $this->assertFalse(isset($request['plugin']));
  679. $this->assertNull($request['plugin']);
  680. $this->assertNull($request->plugin);
  681. $request = new CakeRequest('some/path?one=something&two=else');
  682. $this->assertTrue(isset($request['url']['one']));
  683. $request->data = array('Post' => array('title' => 'something'));
  684. $this->assertEquals($request['data']['Post']['title'], 'something');
  685. }
  686. /**
  687. * test adding detectors and having them work.
  688. *
  689. * @return void
  690. */
  691. public function testAddDetector() {
  692. $request = new CakeRequest('some/path');
  693. $request->addDetector('compare', array('env' => 'TEST_VAR', 'value' => 'something'));
  694. $_SERVER['TEST_VAR'] = 'something';
  695. $this->assertTrue($request->is('compare'), 'Value match failed.');
  696. $_SERVER['TEST_VAR'] = 'wrong';
  697. $this->assertFalse($request->is('compare'), 'Value mis-match failed.');
  698. $request->addDetector('banana', array('env' => 'TEST_VAR', 'pattern' => '/^ban.*$/'));
  699. $_SERVER['TEST_VAR'] = 'banana';
  700. $this->assertTrue($request->isBanana());
  701. $_SERVER['TEST_VAR'] = 'wrong value';
  702. $this->assertFalse($request->isBanana());
  703. $request->addDetector('mobile', array('options' => array('Imagination')));
  704. $_SERVER['HTTP_USER_AGENT'] = 'Imagination land';
  705. $this->assertTrue($request->isMobile());
  706. $_SERVER['HTTP_USER_AGENT'] = 'iPhone 3.0';
  707. $this->assertTrue($request->isMobile());
  708. $request->addDetector('callme', array('env' => 'TEST_VAR', 'callback' => array($this, '_detectCallback')));
  709. $request->addDetector('index', array('param' => 'action', 'value' => 'index'));
  710. $request->params['action'] = 'index';
  711. $this->assertTrue($request->isIndex());
  712. $request->params['action'] = 'add';
  713. $this->assertFalse($request->isIndex());
  714. $request->return = true;
  715. $this->assertTrue($request->isCallMe());
  716. $request->return = false;
  717. $this->assertFalse($request->isCallMe());
  718. }
  719. /**
  720. * helper function for testing callbacks.
  721. *
  722. * @return void
  723. */
  724. public function _detectCallback($request) {
  725. return $request->return == true;
  726. }
  727. /**
  728. * test getting headers
  729. *
  730. * @return void
  731. */
  732. public function testHeader() {
  733. $_SERVER['HTTP_HOST'] = 'localhost';
  734. $_SERVER['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';
  735. $request = new CakeRequest('/', false);
  736. $this->assertEquals($_SERVER['HTTP_HOST'], $request->header('host'));
  737. $this->assertEquals($_SERVER['HTTP_USER_AGENT'], $request->header('User-Agent'));
  738. }
  739. /**
  740. * test accepts() with and without parameters
  741. *
  742. * @return void
  743. */
  744. public function testAccepts() {
  745. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml;q=0.9,application/xhtml+xml,text/html,text/plain,image/png';
  746. $request = new CakeRequest('/', false);
  747. $result = $request->accepts();
  748. $expected = array(
  749. 'text/xml', 'application/xhtml+xml', 'text/html', 'text/plain', 'image/png', 'application/xml'
  750. );
  751. $this->assertEquals($expected, $result, 'Content types differ.');
  752. $result = $request->accepts('text/html');
  753. $this->assertTrue($result);
  754. $result = $request->accepts('image/gif');
  755. $this->assertFalse($result);
  756. }
  757. /**
  758. * Test that accept header types are trimmed for comparisons.
  759. *
  760. * @return void
  761. */
  762. public function testAcceptWithWhitespace() {
  763. $_SERVER['HTTP_ACCEPT'] = 'text/xml , text/html , text/plain,image/png';
  764. $request = new CakeRequest('/', false);
  765. $result = $request->accepts();
  766. $expected = array(
  767. 'text/xml', 'text/html', 'text/plain', 'image/png'
  768. );
  769. $this->assertEquals($expected, $result, 'Content types differ.');
  770. $this->assertTrue($request->accepts('text/html'));
  771. }
  772. /**
  773. * Content types from accepts() should respect the client's q preference values.
  774. *
  775. * @return void
  776. */
  777. public function testAcceptWithQvalueSorting() {
  778. $_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0';
  779. $request = new CakeRequest('/', false);
  780. $result = $request->accepts();
  781. $expected = array('application/xml', 'text/html', 'application/json');
  782. $this->assertEquals($expected, $result);
  783. }
  784. /**
  785. * Test the raw parsing of accept headers into the q value formatting.
  786. *
  787. * @return void
  788. */
  789. public function testParseAcceptWithQValue() {
  790. $_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0,image/png';
  791. $request = new CakeRequest('/', false);
  792. $result = $request->parseAccept();
  793. $expected = array(
  794. '1.0' => array('application/xml', 'image/png'),
  795. '0.8' => array('text/html'),
  796. '0.7' => array('application/json'),
  797. );
  798. $this->assertEquals($expected, $result);
  799. }
  800. /**
  801. * testBaseUrlAndWebrootWithModRewrite method
  802. *
  803. * @return void
  804. */
  805. public function testBaseUrlAndWebrootWithModRewrite() {
  806. Configure::write('App.baseUrl', false);
  807. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  808. $_SERVER['PHP_SELF'] = '/1.2.x.x/app/webroot/index.php';
  809. $_SERVER['PATH_INFO'] = '/posts/view/1';
  810. $request = new CakeRequest();
  811. $this->assertEquals($request->base, '/1.2.x.x');
  812. $this->assertEquals($request->webroot, '/1.2.x.x/');
  813. $this->assertEquals($request->url, 'posts/view/1');
  814. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/app/webroot';
  815. $_SERVER['PHP_SELF'] = '/index.php';
  816. $_SERVER['PATH_INFO'] = '/posts/add';
  817. $request = new CakeRequest();
  818. $this->assertEquals($request->base, '');
  819. $this->assertEquals($request->webroot, '/');
  820. $this->assertEquals($request->url, 'posts/add');
  821. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/test/';
  822. $_SERVER['PHP_SELF'] = '/webroot/index.php';
  823. $request = new CakeRequest();
  824. $this->assertEquals('', $request->base);
  825. $this->assertEquals('/', $request->webroot);
  826. $_SERVER['DOCUMENT_ROOT'] = '/some/apps/where';
  827. $_SERVER['PHP_SELF'] = '/app/webroot/index.php';
  828. $request = new CakeRequest();
  829. $this->assertEquals($request->base, '');
  830. $this->assertEquals($request->webroot, '/');
  831. Configure::write('App.dir', 'auth');
  832. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  833. $_SERVER['PHP_SELF'] = '/demos/auth/webroot/index.php';
  834. $request = new CakeRequest();
  835. $this->assertEquals($request->base, '/demos/auth');
  836. $this->assertEquals($request->webroot, '/demos/auth/');
  837. Configure::write('App.dir', 'code');
  838. $_SERVER['DOCUMENT_ROOT'] = '/Library/WebServer/Documents';
  839. $_SERVER['PHP_SELF'] = '/clients/PewterReport/code/webroot/index.php';
  840. $request = new CakeRequest();
  841. $this->assertEquals($request->base, '/clients/PewterReport/code');
  842. $this->assertEquals($request->webroot, '/clients/PewterReport/code/');
  843. }
  844. /**
  845. * testBaseUrlwithModRewriteAlias method
  846. *
  847. * @return void
  848. */
  849. public function testBaseUrlwithModRewriteAlias() {
  850. $_SERVER['DOCUMENT_ROOT'] = '/home/aplusnur/public_html';
  851. $_SERVER['PHP_SELF'] = '/control/index.php';
  852. Configure::write('App.base', '/control');
  853. $request = new CakeRequest();
  854. $this->assertEquals($request->base, '/control');
  855. $this->assertEquals($request->webroot, '/control/');
  856. Configure::write('App.base', false);
  857. Configure::write('App.dir', 'affiliate');
  858. Configure::write('App.webroot', 'newaffiliate');
  859. $_SERVER['DOCUMENT_ROOT'] = '/var/www/abtravaff/html';
  860. $_SERVER['PHP_SELF'] = '/newaffiliate/index.php';
  861. $request = new CakeRequest();
  862. $this->assertEquals($request->base, '/newaffiliate');
  863. $this->assertEquals($request->webroot, '/newaffiliate/');
  864. }
  865. /**
  866. * test base, webroot, and url parsing when there is no url rewriting
  867. *
  868. * @return void
  869. */
  870. public function testBaseUrlWithNoModRewrite() {
  871. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites';
  872. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake/index.php';
  873. $_SERVER['PHP_SELF'] = '/cake/index.php/posts/index';
  874. $_SERVER['REQUEST_URI'] = '/cake/index.php/posts/index';
  875. Configure::write('App', array(
  876. 'dir' => APP_DIR,
  877. 'webroot' => WEBROOT_DIR,
  878. 'base' => false,
  879. 'baseUrl' => '/cake/index.php'
  880. ));
  881. $request = new CakeRequest();
  882. $this->assertEquals($request->base, '/cake/index.php');
  883. $this->assertEquals($request->webroot, '/cake/app/webroot/');
  884. $this->assertEquals($request->url, 'posts/index');
  885. }
  886. /**
  887. * testBaseUrlAndWebrootWithBaseUrl method
  888. *
  889. * @return void
  890. */
  891. public function testBaseUrlAndWebrootWithBaseUrl() {
  892. Configure::write('App.dir', 'app');
  893. Configure::write('App.baseUrl', '/app/webroot/index.php');
  894. $request = new CakeRequest();
  895. $this->assertEquals($request->base, '/app/webroot/index.php');
  896. $this->assertEquals($request->webroot, '/app/webroot/');
  897. Configure::write('App.baseUrl', '/app/webroot/test.php');
  898. $request = new CakeRequest();
  899. $this->assertEquals($request->base, '/app/webroot/test.php');
  900. $this->assertEquals($request->webroot, '/app/webroot/');
  901. Configure::write('App.baseUrl', '/app/index.php');
  902. $request = new CakeRequest();
  903. $this->assertEquals($request->base, '/app/index.php');
  904. $this->assertEquals($request->webroot, '/app/webroot/');
  905. Configure::write('App.baseUrl', '/CakeBB/app/webroot/index.php');
  906. $request = new CakeRequest();
  907. $this->assertEquals($request->base, '/CakeBB/app/webroot/index.php');
  908. $this->assertEquals($request->webroot, '/CakeBB/app/webroot/');
  909. Configure::write('App.baseUrl', '/CakeBB/app/index.php');
  910. $request = new CakeRequest();
  911. $this->assertEquals($request->base, '/CakeBB/app/index.php');
  912. $this->assertEquals($request->webroot, '/CakeBB/app/webroot/');
  913. Configure::write('App.baseUrl', '/CakeBB/index.php');
  914. $request = new CakeRequest();
  915. $this->assertEquals($request->base, '/CakeBB/index.php');
  916. $this->assertEquals($request->webroot, '/CakeBB/app/webroot/');
  917. Configure::write('App.baseUrl', '/dbhauser/index.php');
  918. $_SERVER['DOCUMENT_ROOT'] = '/kunden/homepages/4/d181710652/htdocs/joomla';
  919. $_SERVER['SCRIPT_FILENAME'] = '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php';
  920. $request = new CakeRequest();
  921. $this->assertEquals($request->base, '/dbhauser/index.php');
  922. $this->assertEquals($request->webroot, '/dbhauser/app/webroot/');
  923. }
  924. /**
  925. * test baseUrl with no rewrite and using the top level index.php.
  926. *
  927. * @return void
  928. */
  929. public function testBaseUrlNoRewriteTopLevelIndex() {
  930. Configure::write('App.baseUrl', '/index.php');
  931. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev';
  932. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/index.php';
  933. $request = new CakeRequest();
  934. $this->assertEquals('/index.php', $request->base);
  935. $this->assertEquals('/app/webroot/', $request->webroot);
  936. }
  937. /**
  938. * Check that a sub-directory containing app|webroot doesn't get mishandled when re-writing is off.
  939. *
  940. * @return void
  941. */
  942. public function testBaseUrlWithAppAndWebrootInDirname() {
  943. Configure::write('App.baseUrl', '/approval/index.php');
  944. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
  945. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/approval/index.php';
  946. $request = new CakeRequest();
  947. $this->assertEquals('/approval/index.php', $request->base);
  948. $this->assertEquals('/approval/app/webroot/', $request->webroot);
  949. Configure::write('App.baseUrl', '/webrootable/index.php');
  950. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
  951. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/webrootable/index.php';
  952. $request = new CakeRequest();
  953. $this->assertEquals('/webrootable/index.php', $request->base);
  954. $this->assertEquals('/webrootable/app/webroot/', $request->webroot);
  955. }
  956. /**
  957. * test baseUrl with no rewrite, and using the app/webroot/index.php file as is normal with virtual hosts.
  958. *
  959. * @return void
  960. */
  961. public function testBaseUrlNoRewriteWebrootIndex() {
  962. Configure::write('App.baseUrl', '/index.php');
  963. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev/app/webroot';
  964. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/app/webroot/index.php';
  965. $request = new CakeRequest();
  966. $this->assertEquals('/index.php', $request->base);
  967. $this->assertEquals('/', $request->webroot);
  968. }
  969. /**
  970. * Test that a request with a . in the main GET parameter is filtered out.
  971. * PHP changes GET parameter keys containing dots to _.
  972. *
  973. * @return void
  974. */
  975. public function testGetParamsWithDot() {
  976. $_GET['/posts/index/add_add'] = '';
  977. $_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
  978. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/index/add.add';
  979. $request = new CakeRequest();
  980. $this->assertEquals(array(), $request->query);
  981. }
  982. /**
  983. * Test that a request with urlencoded bits in the main GET parameter are filtered out.
  984. *
  985. * @return void
  986. */
  987. public function testGetParamWithUrlencodedElement() {
  988. $_GET['/posts/add/∂∂'] = '';
  989. $_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
  990. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/add/%E2%88%82%E2%88%82';
  991. $request = new CakeRequest();
  992. $this->assertEquals(array(), $request->query);
  993. }
  994. /**
  995. * generator for environment configurations
  996. *
  997. * @return void
  998. */
  999. public static function environmentGenerator() {
  1000. return array(
  1001. array(
  1002. 'IIS - No rewrite base path',
  1003. array(
  1004. 'App' => array(
  1005. 'base' => false,
  1006. 'baseUrl' => '/index.php',
  1007. 'dir' => 'app',
  1008. 'webroot' => 'webroot'
  1009. ),
  1010. 'SERVER' => array(
  1011. 'SCRIPT_NAME' => '/index.php',
  1012. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1013. 'QUERY_STRING' => '',
  1014. 'REQUEST_URI' => '/index.php',
  1015. 'URL' => '/index.php',
  1016. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php',
  1017. 'ORIG_PATH_INFO' => '/index.php',
  1018. 'PATH_INFO' => '',
  1019. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php',
  1020. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1021. 'PHP_SELF' => '/index.php',
  1022. ),
  1023. ),
  1024. array(
  1025. 'base' => '/index.php',
  1026. 'webroot' => '/app/webroot/',
  1027. 'url' => ''
  1028. ),
  1029. ),
  1030. array(
  1031. 'IIS - No rewrite with path, no PHP_SELF',
  1032. array(
  1033. 'App' => array(
  1034. 'base' => false,
  1035. 'baseUrl' => '/index.php?',
  1036. 'dir' => 'app',
  1037. 'webroot' => 'webroot'
  1038. ),
  1039. 'SERVER' => array(
  1040. 'QUERY_STRING' => '/posts/add',
  1041. 'REQUEST_URI' => '/index.php?/posts/add',
  1042. 'PHP_SELF' => '',
  1043. 'URL' => '/index.php?/posts/add',
  1044. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1045. 'argv' => array('/posts/add'),
  1046. 'argc' => 1
  1047. ),
  1048. ),
  1049. array(
  1050. 'url' => 'posts/add',
  1051. 'base' => '/index.php?',
  1052. 'webroot' => '/app/webroot/'
  1053. )
  1054. ),
  1055. array(
  1056. 'IIS - No rewrite sub dir 2',
  1057. array(
  1058. 'App' => array(
  1059. 'base' => false,
  1060. 'baseUrl' => '/site/index.php',
  1061. 'dir' => 'app',
  1062. 'webroot' => 'webroot',
  1063. ),
  1064. 'SERVER' => array(
  1065. 'SCRIPT_NAME' => '/site/index.php',
  1066. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1067. 'QUERY_STRING' => '',
  1068. 'REQUEST_URI' => '/site/index.php',
  1069. 'URL' => '/site/index.php',
  1070. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1071. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1072. 'PHP_SELF' => '/site/index.php',
  1073. 'argv' => array(),
  1074. 'argc' => 0
  1075. ),
  1076. ),
  1077. array(
  1078. 'url' => '',
  1079. 'base' => '/site/index.php',
  1080. 'webroot' => '/site/app/webroot/'
  1081. ),
  1082. ),
  1083. array(
  1084. 'IIS - No rewrite sub dir 2 with path',
  1085. array(
  1086. 'App' => array(
  1087. 'base' => false,
  1088. 'baseUrl' => '/site/index.php',
  1089. 'dir' => 'app',
  1090. 'webroot' => 'webroot'
  1091. ),
  1092. 'GET' => array('/posts/add' => ''),
  1093. 'SERVER' => array(
  1094. 'SCRIPT_NAME' => '/site/index.php',
  1095. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1096. 'QUERY_STRING' => '/posts/add',
  1097. 'REQUEST_URI' => '/site/index.php/posts/add',
  1098. 'URL' => '/site/index.php/posts/add',
  1099. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1100. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1101. 'PHP_SELF' => '/site/index.php/posts/add',
  1102. 'argv' => array('/posts/add'),
  1103. 'argc' => 1
  1104. ),
  1105. ),
  1106. array(
  1107. 'url' => 'posts/add',
  1108. 'base' => '/site/index.php',
  1109. 'webroot' => '/site/app/webroot/'
  1110. )
  1111. ),
  1112. array(
  1113. 'Apache - No rewrite, document root set to webroot, requesting path',
  1114. array(
  1115. 'App' => array(
  1116. 'base' => false,
  1117. 'baseUrl' => '/index.php',
  1118. 'dir' => 'app',
  1119. 'webroot' => 'webroot'
  1120. ),
  1121. 'SERVER' => array(
  1122. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1123. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1124. 'QUERY_STRING' => '',
  1125. 'REQUEST_URI' => '/index.php/posts/index',
  1126. 'SCRIPT_NAME' => '/index.php',
  1127. 'PATH_INFO' => '/posts/index',
  1128. 'PHP_SELF' => '/index.php/posts/index',
  1129. ),
  1130. ),
  1131. array(
  1132. 'url' => 'posts/index',
  1133. 'base' => '/index.php',
  1134. 'webroot' => '/'
  1135. ),
  1136. ),
  1137. array(
  1138. 'Apache - No rewrite, document root set to webroot, requesting root',
  1139. array(
  1140. 'App' => array(
  1141. 'base' => false,
  1142. 'baseUrl' => '/index.php',
  1143. 'dir' => 'app',
  1144. 'webroot' => 'webroot'
  1145. ),
  1146. 'SERVER' => array(
  1147. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1148. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1149. 'QUERY_STRING' => '',
  1150. 'REQUEST_URI' => '/index.php',
  1151. 'SCRIPT_NAME' => '/index.php',
  1152. 'PATH_INFO' => '',
  1153. 'PHP_SELF' => '/index.php',
  1154. ),
  1155. ),
  1156. array(
  1157. 'url' => '',
  1158. 'base' => '/index.php',
  1159. 'webroot' => '/'
  1160. ),
  1161. ),
  1162. array(
  1163. 'Apache - No rewrite, document root set above top level cake dir, requesting path',
  1164. array(
  1165. 'App' => array(
  1166. 'base' => false,
  1167. 'baseUrl' => '/site/index.php',
  1168. 'dir' => 'app',
  1169. 'webroot' => 'webroot'
  1170. ),
  1171. 'SERVER' => array(
  1172. 'SERVER_NAME' => 'localhost',
  1173. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1174. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1175. 'REQUEST_URI' => '/site/index.php/posts/index',
  1176. 'SCRIPT_NAME' => '/site/index.php',
  1177. 'PATH_INFO' => '/posts/index',
  1178. 'PHP_SELF' => '/site/index.php/posts/index',
  1179. ),
  1180. ),
  1181. array(
  1182. 'url' => 'posts/index',
  1183. 'base' => '/site/index.php',
  1184. 'webroot' => '/site/app/webroot/',
  1185. ),
  1186. ),
  1187. array(
  1188. 'Apache - No rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1189. array(
  1190. 'App' => array(
  1191. 'base' => false,
  1192. 'baseUrl' => '/site/index.php',
  1193. 'dir' => 'app',
  1194. 'webroot' => 'webroot'
  1195. ),
  1196. 'SERVER' => array(
  1197. 'SERVER_NAME' => 'localhost',
  1198. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1199. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1200. 'REQUEST_URI' => '/site/index.php/',
  1201. 'SCRIPT_NAME' => '/site/index.php',
  1202. 'PHP_SELF' => '/site/index.php/',
  1203. ),
  1204. ),
  1205. array(
  1206. 'url' => '',
  1207. 'base' => '/site/index.php',
  1208. 'webroot' => '/site/app/webroot/',
  1209. ),
  1210. ),
  1211. array(
  1212. 'Apache - No rewrite, document root set above top level cake dir, request path, with GET',
  1213. array(
  1214. 'App' => array(
  1215. 'base' => false,
  1216. 'baseUrl' => '/site/index.php',
  1217. 'dir' => 'app',
  1218. 'webroot' => 'webroot'
  1219. ),
  1220. 'GET' => array('a' => 'b', 'c' => 'd'),
  1221. 'SERVER' => array(
  1222. 'SERVER_NAME' => 'localhost',
  1223. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1224. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1225. 'REQUEST_URI' => '/site/index.php/posts/index?a=b&c=d',
  1226. 'SCRIPT_NAME' => '/site/index.php',
  1227. 'PATH_INFO' => '/posts/index',
  1228. 'PHP_SELF' => '/site/index.php/posts/index',
  1229. 'QUERY_STRING' => 'a=b&c=d'
  1230. ),
  1231. ),
  1232. array(
  1233. 'urlParams' => array('a' => 'b', 'c' => 'd'),
  1234. 'url' => 'posts/index',
  1235. 'base' => '/site/index.php',
  1236. 'webroot' => '/site/app/webroot/',
  1237. ),
  1238. ),
  1239. array(
  1240. 'Apache - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1241. array(
  1242. 'App' => array(
  1243. 'base' => false,
  1244. 'baseUrl' => false,
  1245. 'dir' => 'app',
  1246. 'webroot' => 'webroot'
  1247. ),
  1248. 'SERVER' => array(
  1249. 'SERVER_NAME' => 'localhost',
  1250. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1251. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1252. 'REQUEST_URI' => '/site/',
  1253. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1254. 'PHP_SELF' => '/site/app/webroot/index.php',
  1255. ),
  1256. ),
  1257. array(
  1258. 'url' => '',
  1259. 'base' => '/site',
  1260. 'webroot' => '/site/',
  1261. ),
  1262. ),
  1263. array(
  1264. 'Apache - w/rewrite, document root above top level cake dir, request root, no PATH_INFO/REQUEST_URI',
  1265. array(
  1266. 'App' => array(
  1267. 'base' => false,
  1268. 'baseUrl' => false,
  1269. 'dir' => 'app',
  1270. 'webroot' => 'webroot'
  1271. ),
  1272. 'SERVER' => array(
  1273. 'SERVER_NAME' => 'localhost',
  1274. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1275. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1276. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1277. 'PHP_SELF' => '/site/app/webroot/index.php',
  1278. 'PATH_INFO' => null,
  1279. 'REQUEST_URI' => null,
  1280. ),
  1281. ),
  1282. array(
  1283. 'url' => '',
  1284. 'base' => '/site',
  1285. 'webroot' => '/site/',
  1286. ),
  1287. ),
  1288. array(
  1289. 'Apache - w/rewrite, document root set to webroot, request root, no PATH_INFO/REQUEST_URI',
  1290. array(
  1291. 'App' => array(
  1292. 'base' => false,
  1293. 'baseUrl' => false,
  1294. 'dir' => 'app',
  1295. 'webroot' => 'webroot'
  1296. ),
  1297. 'SERVER' => array(
  1298. 'SERVER_NAME' => 'localhost',
  1299. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1300. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1301. 'SCRIPT_NAME' => '/index.php',
  1302. 'PHP_SELF' => '/index.php',
  1303. 'PATH_INFO' => null,
  1304. 'REQUEST_URI' => null,
  1305. ),
  1306. ),
  1307. array(
  1308. 'url' => '',
  1309. 'base' => '',
  1310. 'webroot' => '/',
  1311. ),
  1312. ),
  1313. array(
  1314. 'Nginx - w/rewrite, document root set to webroot, request root, no PATH_INFO',
  1315. array(
  1316. 'App' => array(
  1317. 'base' => false,
  1318. 'baseUrl' => false,
  1319. 'dir' => 'app',
  1320. 'webroot' => 'webroot'
  1321. ),
  1322. 'GET' => array('/posts/add' => ''),
  1323. 'SERVER' => array(
  1324. 'SERVER_NAME' => 'localhost',
  1325. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1326. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1327. 'SCRIPT_NAME' => '/index.php',
  1328. 'QUERY_STRING' => '/posts/add&',
  1329. 'PHP_SELF' => '/index.php',
  1330. 'PATH_INFO' => null,
  1331. 'REQUEST_URI' => '/posts/add',
  1332. ),
  1333. ),
  1334. array(
  1335. 'url' => 'posts/add',
  1336. 'base' => '',
  1337. 'webroot' => '/',
  1338. 'urlParams' => array()
  1339. ),
  1340. ),
  1341. );
  1342. }
  1343. /**
  1344. * testEnvironmentDetection method
  1345. *
  1346. * @dataProvider environmentGenerator
  1347. * @return void
  1348. */
  1349. public function testEnvironmentDetection($name, $env, $expected) {
  1350. $_GET = array();
  1351. $this->__loadEnvironment($env);
  1352. $request = new CakeRequest();
  1353. $this->assertEquals($expected['url'], $request->url, "url error");
  1354. $this->assertEquals($expected['base'], $request->base, "base error");
  1355. $this->assertEquals($expected['webroot'], $request->webroot, "webroot error");
  1356. if (isset($expected['urlParams'])) {
  1357. $this->assertEquals($request->query, $expected['urlParams'], "GET param mismatch");
  1358. }
  1359. }
  1360. /**
  1361. * test the data() method reading
  1362. *
  1363. * @return void
  1364. */
  1365. public function testDataReading() {
  1366. $_POST['data'] = array(
  1367. 'Model' => array(
  1368. 'field' => 'value'
  1369. )
  1370. );
  1371. $request = new CakeRequest('posts/index');
  1372. $result = $request->data('Model');
  1373. $this->assertEquals($_POST['data']['Model'], $result);
  1374. $result = $request->data('Model.imaginary');
  1375. $this->assertNull($result);
  1376. }
  1377. /**
  1378. * test writing with data()
  1379. *
  1380. * @return void
  1381. */
  1382. public function testDataWriting() {
  1383. $_POST['data'] = array(
  1384. 'Model' => array(
  1385. 'field' => 'value'
  1386. )
  1387. );
  1388. $request = new CakeRequest('posts/index');
  1389. $result = $request->data('Model.new_value', 'new value');
  1390. $this->assertSame($result, $request, 'Return was not $this');
  1391. $this->assertEquals($request->data['Model']['new_value'], 'new value');
  1392. $request->data('Post.title', 'New post')->data('Comment.1.author', 'Mark');
  1393. $this->assertEquals($request->data['Post']['title'], 'New post');
  1394. $this->assertEquals($request->data['Comment']['1']['author'], 'Mark');
  1395. }
  1396. /**
  1397. * test writing falsey values.
  1398. *
  1399. * @return void
  1400. */
  1401. public function testDataWritingFalsey() {
  1402. $request = new CakeRequest('posts/index');
  1403. $request->data('Post.null', null);
  1404. $this->assertNull($request->data['Post']['null']);
  1405. $request->data('Post.false', false);
  1406. $this->assertFalse($request->data['Post']['false']);
  1407. $request->data('Post.zero', 0);
  1408. $this->assertSame(0, $request->data['Post']['zero']);
  1409. $request->data('Post.empty', '');
  1410. $this->assertSame('', $request->data['Post']['empty']);
  1411. }
  1412. /**
  1413. * test accept language
  1414. *
  1415. * @return void
  1416. */
  1417. public function testAcceptLanguage() {
  1418. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
  1419. $result = CakeRequest::acceptLanguage();
  1420. $this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
  1421. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx;en_ca';
  1422. $result = CakeRequest::acceptLanguage();
  1423. $this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
  1424. $result = CakeRequest::acceptLanguage('en-ca');
  1425. $this->assertTrue($result);
  1426. $result = CakeRequest::acceptLanguage('en-us');
  1427. $this->assertFalse($result);
  1428. }
  1429. /**
  1430. * test the here() method
  1431. *
  1432. * @return void
  1433. */
  1434. public function testHere() {
  1435. Configure::write('App.base', '/base_path');
  1436. $_GET = array('test' => 'value');
  1437. $request = new CakeRequest('/posts/add/1/name:value');
  1438. $result = $request->here();
  1439. $this->assertEquals('/base_path/posts/add/1/name:value?test=value', $result);
  1440. $result = $request->here(false);
  1441. $this->assertEquals('/posts/add/1/name:value?test=value', $result);
  1442. $request = new CakeRequest('/posts/base_path/1/name:value');
  1443. $result = $request->here();
  1444. $this->assertEquals('/base_path/posts/base_path/1/name:value?test=value', $result);
  1445. $result = $request->here(false);
  1446. $this->assertEquals('/posts/base_path/1/name:value?test=value', $result);
  1447. }
  1448. /**
  1449. * Test the input() method.
  1450. *
  1451. * @return void
  1452. */
  1453. public function testInput() {
  1454. $request = $this->getMock('CakeRequest', array('_readInput'));
  1455. $request->expects($this->once())->method('_readInput')
  1456. ->will($this->returnValue('I came from stdin'));
  1457. $result = $request->input();
  1458. $this->assertEquals('I came from stdin', $result);
  1459. }
  1460. /**
  1461. * Test input() decoding.
  1462. *
  1463. * @return void
  1464. */
  1465. public function testInputDecode() {
  1466. $request = $this->getMock('CakeRequest', array('_readInput'));
  1467. $request->expects($this->once())->method('_readInput')
  1468. ->will($this->returnValue('{"name":"value"}'));
  1469. $result = $request->input('json_decode');
  1470. $this->assertEquals(array('name' => 'value'), (array)$result);
  1471. }
  1472. /**
  1473. * Test input() decoding with additional arguments.
  1474. *
  1475. * @return void
  1476. */
  1477. public function testInputDecodeExtraParams() {
  1478. $xml = <<<XML
  1479. <?xml version="1.0" encoding="utf-8"?>
  1480. <post>
  1481. <title id="title">Test</title>
  1482. </post>
  1483. XML;
  1484. $request = $this->getMock('CakeRequest', array('_readInput'));
  1485. $request->expects($this->once())->method('_readInput')
  1486. ->will($this->returnValue($xml));
  1487. $result = $request->input('Xml::build', array('return' => 'domdocument'));
  1488. $this->assertInstanceOf('DOMDocument', $result);
  1489. $this->assertEquals(
  1490. 'Test',
  1491. $result->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText
  1492. );
  1493. }
  1494. /**
  1495. * Test is('requested') and isRequested()
  1496. *
  1497. * @return void
  1498. */
  1499. public function testIsRequested() {
  1500. $request = new CakeRequest('/posts/index');
  1501. $request->addParams(array(
  1502. 'controller' => 'posts',
  1503. 'action' => 'index',
  1504. 'plugin' => null,
  1505. 'requested' => 1
  1506. ));
  1507. $this->assertTrue($request->is('requested'));
  1508. $this->assertTrue($request->isRequested());
  1509. $request = new CakeRequest('/posts/index');
  1510. $request->addParams(array(
  1511. 'controller' => 'posts',
  1512. 'action' => 'index',
  1513. 'plugin' => null,
  1514. ));
  1515. $this->assertFalse($request->is('requested'));
  1516. $this->assertFalse($request->isRequested());
  1517. }
  1518. /**
  1519. * loadEnvironment method
  1520. *
  1521. * @param mixed $env
  1522. * @return void
  1523. */
  1524. protected function __loadEnvironment($env) {
  1525. if (isset($env['App'])) {
  1526. Configure::write('App', $env['App']);
  1527. }
  1528. if (isset($env['GET'])) {
  1529. foreach ($env['GET'] as $key => $val) {
  1530. $_GET[$key] = $val;
  1531. }
  1532. }
  1533. if (isset($env['POST'])) {
  1534. foreach ($env['POST'] as $key => $val) {
  1535. $_POST[$key] = $val;
  1536. }
  1537. }
  1538. if (isset($env['SERVER'])) {
  1539. foreach ($env['SERVER'] as $key => $val) {
  1540. $_SERVER[$key] = $val;
  1541. }
  1542. }
  1543. }
  1544. }