RequestTest.php 62 KB

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