RequestTest.php 62 KB

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