RequestTest.php 62 KB

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