RequestTest.php 62 KB

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