RequestTest.php 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280
  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 BadMethodCallException
  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. $request = new Request();
  613. $request->env('HTTPS', 1);
  614. $this->assertTrue($request->is('ssl'));
  615. $request->env('HTTPS', 'on');
  616. $this->assertTrue($request->is('ssl'));
  617. $request->env('HTTPS', '1');
  618. $this->assertTrue($request->is('ssl'));
  619. $request->env('HTTPS', 'I am not empty');
  620. $this->assertFalse($request->is('ssl'));
  621. $request->env('HTTPS', 'off');
  622. $this->assertFalse($request->is('ssl'));
  623. $request->env('HTTPS', false);
  624. $this->assertFalse($request->is('ssl'));
  625. $request->env('HTTPS', '');
  626. $this->assertFalse($request->is('ssl'));
  627. }
  628. /**
  629. * Test getting request params with object properties.
  630. *
  631. * @return void
  632. */
  633. public function testMagicget() {
  634. $request = new Request();
  635. $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
  636. $this->assertEquals('posts', $request->controller);
  637. $this->assertEquals('view', $request->action);
  638. $this->assertEquals('blogs', $request->plugin);
  639. $this->assertNull($request->banana);
  640. }
  641. /**
  642. * Test isset()/empty() with overloaded properties.
  643. *
  644. * @return void
  645. */
  646. public function testMagicisset() {
  647. $request = new Request();
  648. $request->params = array(
  649. 'controller' => 'posts',
  650. 'action' => 'view',
  651. 'plugin' => 'blogs',
  652. );
  653. $this->assertTrue(isset($request->controller));
  654. $this->assertFalse(isset($request->notthere));
  655. $this->assertFalse(empty($request->controller));
  656. }
  657. /**
  658. * Test the array access implementation
  659. *
  660. * @return void
  661. */
  662. public function testArrayAccess() {
  663. $request = new Request();
  664. $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
  665. $this->assertEquals('posts', $request['controller']);
  666. $request['slug'] = 'speedy-slug';
  667. $this->assertEquals('speedy-slug', $request->slug);
  668. $this->assertEquals('speedy-slug', $request['slug']);
  669. $this->assertTrue(isset($request['action']));
  670. $this->assertFalse(isset($request['wrong-param']));
  671. $this->assertTrue(isset($request['plugin']));
  672. unset($request['plugin']);
  673. $this->assertFalse(isset($request['plugin']));
  674. $this->assertNull($request['plugin']);
  675. $this->assertNull($request->plugin);
  676. $request = new Request(array('url' => 'some/path?one=something&two=else'));
  677. $this->assertTrue(isset($request['url']['one']));
  678. $request->data = array('Post' => array('title' => 'something'));
  679. $this->assertEquals('something', $request['data']['Post']['title']);
  680. }
  681. /**
  682. * Test adding detectors and having them work.
  683. *
  684. * @return void
  685. */
  686. public function testAddDetector() {
  687. $request = new Request();
  688. Request::addDetector('closure', function ($request) {
  689. return true;
  690. });
  691. $this->assertTrue($request->is('closure'));
  692. Request::addDetector('get', function ($request) {
  693. return $request->env('REQUEST_METHOD') === 'GET';
  694. });
  695. $request->env('REQUEST_METHOD', 'GET');
  696. $this->assertTrue($request->is('get'));
  697. Request::addDetector('compare', array('env' => 'TEST_VAR', 'value' => 'something'));
  698. $request->env('TEST_VAR', 'something');
  699. $this->assertTrue($request->is('compare'), 'Value match failed.');
  700. $request->env('TEST_VAR', 'wrong');
  701. $this->assertFalse($request->is('compare'), 'Value mis-match failed.');
  702. Request::addDetector('compareCamelCase', array('env' => 'TEST_VAR', 'value' => 'foo'));
  703. $request->env('TEST_VAR', 'foo');
  704. $this->assertTrue($request->is('compareCamelCase'), 'Value match failed.');
  705. $this->assertTrue($request->is('comparecamelcase'), 'detectors should be case insensitive');
  706. $this->assertTrue($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
  707. $request->env('TEST_VAR', 'not foo');
  708. $this->assertFalse($request->is('compareCamelCase'), 'Value match failed.');
  709. $this->assertFalse($request->is('comparecamelcase'), 'detectors should be case insensitive');
  710. $this->assertFalse($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
  711. Request::addDetector('banana', array('env' => 'TEST_VAR', 'pattern' => '/^ban.*$/'));
  712. $request->env('TEST_VAR', 'banana');
  713. $this->assertTrue($request->isBanana());
  714. $request->env('TEST_VAR', 'wrong value');
  715. $this->assertFalse($request->isBanana());
  716. Request::addDetector('mobile', array('env' => 'HTTP_USER_AGENT', 'options' => array('Imagination')));
  717. $request->env('HTTP_USER_AGENT', 'Imagination land');
  718. $this->assertTrue($request->isMobile());
  719. Request::addDetector('callme', array('env' => 'TEST_VAR', 'callback' => array($this, 'detectCallback')));
  720. Request::addDetector('index', array('param' => 'action', 'value' => 'index'));
  721. $request->params['action'] = 'index';
  722. $this->assertTrue($request->isIndex());
  723. $request->params['action'] = 'add';
  724. $this->assertFalse($request->isIndex());
  725. $request->return = true;
  726. $this->assertTrue($request->isCallMe());
  727. $request->return = false;
  728. $this->assertFalse($request->isCallMe());
  729. Request::addDetector('callme', array($this, 'detectCallback'));
  730. $request->return = true;
  731. $this->assertTrue($request->isCallMe());
  732. Request::addDetector('extension', array('param' => 'ext', 'options' => array('pdf', 'png', 'txt')));
  733. $request->params['ext'] = 'pdf';
  734. $this->assertTrue($request->is('extension'));
  735. $request->params['ext'] = 'exe';
  736. $this->assertFalse($request->isExtension());
  737. }
  738. /**
  739. * Helper function for testing callbacks.
  740. *
  741. * @param $request
  742. * @return bool
  743. */
  744. public function detectCallback($request) {
  745. return (bool)$request->return;
  746. }
  747. /**
  748. * Test getting headers
  749. *
  750. * @return void
  751. */
  752. public function testHeader() {
  753. $request = new Request(['environment' => [
  754. 'HTTP_HOST' => 'localhost',
  755. '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'
  756. ]]);
  757. $this->assertEquals($request->env('HTTP_HOST'), $request->header('host'));
  758. $this->assertEquals($request->env('HTTP_USER_AGENT'), $request->header('User-Agent'));
  759. }
  760. /**
  761. * Test accepts() with and without parameters
  762. *
  763. * @return void
  764. */
  765. public function testAccepts() {
  766. $request = new Request(['environment' => [
  767. 'HTTP_ACCEPT' => 'text/xml,application/xml;q=0.9,application/xhtml+xml,text/html,text/plain,image/png'
  768. ]]);
  769. $result = $request->accepts();
  770. $expected = array(
  771. 'text/xml', 'application/xhtml+xml', 'text/html', 'text/plain', 'image/png', 'application/xml'
  772. );
  773. $this->assertEquals($expected, $result, 'Content types differ.');
  774. $result = $request->accepts('text/html');
  775. $this->assertTrue($result);
  776. $result = $request->accepts('image/gif');
  777. $this->assertFalse($result);
  778. }
  779. /**
  780. * Test that accept header types are trimmed for comparisons.
  781. *
  782. * @return void
  783. */
  784. public function testAcceptWithWhitespace() {
  785. $request = new Request(['environment' => [
  786. 'HTTP_ACCEPT' => 'text/xml , text/html , text/plain,image/png'
  787. ]]);
  788. $result = $request->accepts();
  789. $expected = array(
  790. 'text/xml', 'text/html', 'text/plain', 'image/png'
  791. );
  792. $this->assertEquals($expected, $result, 'Content types differ.');
  793. $this->assertTrue($request->accepts('text/html'));
  794. }
  795. /**
  796. * Content types from accepts() should respect the client's q preference values.
  797. *
  798. * @return void
  799. */
  800. public function testAcceptWithQvalueSorting() {
  801. $request = new Request(['environment' => [
  802. 'HTTP_ACCEPT' => 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0'
  803. ]]);
  804. $result = $request->accepts();
  805. $expected = array('application/xml', 'text/html', 'application/json');
  806. $this->assertEquals($expected, $result);
  807. }
  808. /**
  809. * Test the raw parsing of accept headers into the q value formatting.
  810. *
  811. * @return void
  812. */
  813. public function testParseAcceptWithQValue() {
  814. $request = new Request(['environment' => [
  815. 'HTTP_ACCEPT' => 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0,image/png'
  816. ]]);
  817. $result = $request->parseAccept();
  818. $expected = array(
  819. '1.0' => array('application/xml', 'image/png'),
  820. '0.8' => array('text/html'),
  821. '0.7' => array('application/json'),
  822. );
  823. $this->assertEquals($expected, $result);
  824. }
  825. /**
  826. * Test parsing accept with a confusing accept value.
  827. *
  828. * @return void
  829. */
  830. public function testParseAcceptNoQValues() {
  831. $request = new Request(['environment' => [
  832. 'HTTP_ACCEPT' => 'application/json, text/plain, */*'
  833. ]]);
  834. $result = $request->parseAccept();
  835. $expected = array(
  836. '1.0' => array('application/json', 'text/plain', '*/*'),
  837. );
  838. $this->assertEquals($expected, $result);
  839. }
  840. /**
  841. * Test parsing accept ignores index param
  842. *
  843. * @return void
  844. */
  845. public function testParseAcceptIgnoreAcceptExtensions() {
  846. $request = new Request(['environment' => [
  847. 'url' => '/',
  848. 'HTTP_ACCEPT' => 'application/json;level=1, text/plain, */*'
  849. ]], false);
  850. $result = $request->parseAccept();
  851. $expected = array(
  852. '1.0' => array('application/json', 'text/plain', '*/*'),
  853. );
  854. $this->assertEquals($expected, $result);
  855. }
  856. /**
  857. * Test that parsing accept headers with invalid syntax works.
  858. *
  859. * The header used is missing a q value for application/xml.
  860. *
  861. * @return void
  862. */
  863. public function testParseAcceptInvalidSyntax() {
  864. $request = new Request(['environment' => [
  865. 'url' => '/',
  866. 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;image/png,image/jpeg,image/*;q=0.9,*/*;q=0.8'
  867. ]], false);
  868. $result = $request->parseAccept();
  869. $expected = array(
  870. '1.0' => array('text/html', 'application/xhtml+xml', 'application/xml', 'image/jpeg'),
  871. '0.9' => array('image/*'),
  872. '0.8' => array('*/*'),
  873. );
  874. $this->assertEquals($expected, $result);
  875. }
  876. /**
  877. * Test baseUrl and webroot with ModRewrite
  878. *
  879. * @return void
  880. */
  881. public function testBaseUrlAndWebrootWithModRewrite() {
  882. Configure::write('App.baseUrl', false);
  883. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  884. $_SERVER['PHP_SELF'] = '/urlencode me/webroot/index.php';
  885. $_SERVER['PATH_INFO'] = '/posts/view/1';
  886. $request = Request::createFromGlobals();
  887. $this->assertEquals('/urlencode%20me', $request->base);
  888. $this->assertEquals('/urlencode%20me/', $request->webroot);
  889. $this->assertEquals('posts/view/1', $request->url);
  890. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  891. $_SERVER['PHP_SELF'] = '/1.2.x.x/webroot/index.php';
  892. $_SERVER['PATH_INFO'] = '/posts/view/1';
  893. $request = Request::createFromGlobals();
  894. $this->assertEquals('/1.2.x.x', $request->base);
  895. $this->assertEquals('/1.2.x.x/', $request->webroot);
  896. $this->assertEquals('posts/view/1', $request->url);
  897. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/webroot';
  898. $_SERVER['PHP_SELF'] = '/index.php';
  899. $_SERVER['PATH_INFO'] = '/posts/add';
  900. $request = Request::createFromGlobals();
  901. $this->assertEquals('', $request->base);
  902. $this->assertEquals('/', $request->webroot);
  903. $this->assertEquals('posts/add', $request->url);
  904. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/test/';
  905. $_SERVER['PHP_SELF'] = '/webroot/index.php';
  906. $request = Request::createFromGlobals();
  907. $this->assertEquals('', $request->base);
  908. $this->assertEquals('/', $request->webroot);
  909. $_SERVER['DOCUMENT_ROOT'] = '/some/apps/where';
  910. $_SERVER['PHP_SELF'] = '/webroot/index.php';
  911. $request = Request::createFromGlobals();
  912. $this->assertEquals('', $request->base);
  913. $this->assertEquals('/', $request->webroot);
  914. Configure::write('App.dir', 'auth');
  915. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  916. $_SERVER['PHP_SELF'] = '/demos/webroot/index.php';
  917. $request = Request::createFromGlobals();
  918. $this->assertEquals('/demos', $request->base);
  919. $this->assertEquals('/demos/', $request->webroot);
  920. Configure::write('App.dir', 'code');
  921. $_SERVER['DOCUMENT_ROOT'] = '/Library/WebServer/Documents';
  922. $_SERVER['PHP_SELF'] = '/clients/PewterReport/webroot/index.php';
  923. $request = Request::createFromGlobals();
  924. $this->assertEquals('/clients/PewterReport', $request->base);
  925. $this->assertEquals('/clients/PewterReport/', $request->webroot);
  926. }
  927. /**
  928. * Test baseUrl with ModRewrite alias
  929. *
  930. * @return void
  931. */
  932. public function testBaseUrlwithModRewriteAlias() {
  933. $_SERVER['DOCUMENT_ROOT'] = '/home/aplusnur/public_html';
  934. $_SERVER['PHP_SELF'] = '/control/index.php';
  935. Configure::write('App.base', '/control');
  936. $request = Request::createFromGlobals();
  937. $this->assertEquals('/control', $request->base);
  938. $this->assertEquals('/control/', $request->webroot);
  939. Configure::write('App.base', false);
  940. Configure::write('App.dir', 'affiliate');
  941. Configure::write('App.webroot', 'newaffiliate');
  942. $_SERVER['DOCUMENT_ROOT'] = '/var/www/abtravaff/html';
  943. $_SERVER['PHP_SELF'] = '/newaffiliate/index.php';
  944. $request = Request::createFromGlobals();
  945. $this->assertEquals('', $request->base);
  946. $this->assertEquals('/', $request->webroot);
  947. }
  948. /**
  949. * Test base, webroot, URL and here parsing when there is URL rewriting but
  950. * CakePHP gets called with index.php in URL nonetheless.
  951. *
  952. * Tests uri with
  953. * - index.php/
  954. * - index.php/
  955. * - index.php/apples/
  956. * - index.php/bananas/eat/tasty_banana
  957. *
  958. * @link https://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/3318
  959. * @return void
  960. */
  961. public function testBaseUrlWithModRewriteAndIndexPhp() {
  962. $_SERVER['REQUEST_URI'] = '/cakephp/webroot/index.php';
  963. $_SERVER['PHP_SELF'] = '/cakephp/webroot/index.php';
  964. unset($_SERVER['PATH_INFO']);
  965. $request = Request::createFromGlobals();
  966. $this->assertEquals('/cakephp', $request->base);
  967. $this->assertEquals('/cakephp/', $request->webroot);
  968. $this->assertEquals('', $request->url);
  969. $this->assertEquals('/cakephp/', $request->here);
  970. $_SERVER['REQUEST_URI'] = '/cakephp/webroot/index.php/';
  971. $_SERVER['PHP_SELF'] = '/cakephp/webroot/index.php/';
  972. $_SERVER['PATH_INFO'] = '/';
  973. $request = Request::createFromGlobals();
  974. $this->assertEquals('/cakephp', $request->base);
  975. $this->assertEquals('/cakephp/', $request->webroot);
  976. $this->assertEquals('', $request->url);
  977. $this->assertEquals('/cakephp/', $request->here);
  978. $_SERVER['REQUEST_URI'] = '/cakephp/webroot/index.php/apples';
  979. $_SERVER['PHP_SELF'] = '/cakephp/webroot/index.php/apples';
  980. $_SERVER['PATH_INFO'] = '/apples';
  981. $request = Request::createFromGlobals();
  982. $this->assertEquals('/cakephp', $request->base);
  983. $this->assertEquals('/cakephp/', $request->webroot);
  984. $this->assertEquals('apples', $request->url);
  985. $this->assertEquals('/cakephp/apples', $request->here);
  986. $_SERVER['REQUEST_URI'] = '/cakephp/webroot/index.php/melons/share/';
  987. $_SERVER['PHP_SELF'] = '/cakephp/webroot/index.php/melons/share/';
  988. $_SERVER['PATH_INFO'] = '/melons/share/';
  989. $request = Request::createFromGlobals();
  990. $this->assertEquals('/cakephp', $request->base);
  991. $this->assertEquals('/cakephp/', $request->webroot);
  992. $this->assertEquals('melons/share/', $request->url);
  993. $this->assertEquals('/cakephp/melons/share/', $request->here);
  994. $_SERVER['REQUEST_URI'] = '/cakephp/webroot/index.php/bananas/eat/tasty_banana';
  995. $_SERVER['PHP_SELF'] = '/cakephp/webroot/index.php/bananas/eat/tasty_banana';
  996. $_SERVER['PATH_INFO'] = '/bananas/eat/tasty_banana';
  997. $request = Request::createFromGlobals();
  998. $this->assertEquals('/cakephp', $request->base);
  999. $this->assertEquals('/cakephp/', $request->webroot);
  1000. $this->assertEquals('bananas/eat/tasty_banana', $request->url);
  1001. $this->assertEquals('/cakephp/bananas/eat/tasty_banana', $request->here);
  1002. }
  1003. /**
  1004. * Test base, webroot, and URL parsing when there is no URL rewriting
  1005. *
  1006. * @return void
  1007. */
  1008. public function testBaseUrlWithNoModRewrite() {
  1009. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites';
  1010. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake/index.php';
  1011. $_SERVER['PHP_SELF'] = '/cake/index.php/posts/index';
  1012. $_SERVER['REQUEST_URI'] = '/cake/index.php/posts/index';
  1013. Configure::write('App', array(
  1014. 'dir' => APP_DIR,
  1015. 'webroot' => WEBROOT_DIR,
  1016. 'base' => false,
  1017. 'baseUrl' => '/cake/index.php'
  1018. ));
  1019. $request = Request::createFromGlobals();
  1020. $this->assertEquals('/cake/index.php', $request->base);
  1021. $this->assertEquals('/cake/webroot/', $request->webroot);
  1022. $this->assertEquals('posts/index', $request->url);
  1023. }
  1024. /**
  1025. * Test baseUrl and webroot with baseUrl
  1026. *
  1027. * @return void
  1028. */
  1029. public function testBaseUrlAndWebrootWithBaseUrl() {
  1030. Configure::write('App.dir', 'App');
  1031. Configure::write('App.baseUrl', '/App/webroot/index.php');
  1032. $request = Request::createFromGlobals();
  1033. $this->assertEquals('/App/webroot/index.php', $request->base);
  1034. $this->assertEquals('/App/webroot/', $request->webroot);
  1035. Configure::write('App.baseUrl', '/App/webroot/test.php');
  1036. $request = Request::createFromGlobals();
  1037. $this->assertEquals('/App/webroot/test.php', $request->base);
  1038. $this->assertEquals('/App/webroot/', $request->webroot);
  1039. Configure::write('App.baseUrl', '/App/index.php');
  1040. $request = Request::createFromGlobals();
  1041. $this->assertEquals('/App/index.php', $request->base);
  1042. $this->assertEquals('/App/webroot/', $request->webroot);
  1043. Configure::write('App.baseUrl', '/CakeBB/App/webroot/index.php');
  1044. $request = Request::createFromGlobals();
  1045. $this->assertEquals('/CakeBB/App/webroot/index.php', $request->base);
  1046. $this->assertEquals('/CakeBB/App/webroot/', $request->webroot);
  1047. Configure::write('App.baseUrl', '/CakeBB/App/index.php');
  1048. $request = Request::createFromGlobals();
  1049. $this->assertEquals('/CakeBB/App/index.php', $request->base);
  1050. $this->assertEquals('/CakeBB/App/webroot/', $request->webroot);
  1051. Configure::write('App.baseUrl', '/CakeBB/index.php');
  1052. $request = Request::createFromGlobals();
  1053. $this->assertEquals('/CakeBB/index.php', $request->base);
  1054. $this->assertEquals('/CakeBB/webroot/', $request->webroot);
  1055. Configure::write('App.baseUrl', '/dbhauser/index.php');
  1056. $_SERVER['DOCUMENT_ROOT'] = '/kunden/homepages/4/d181710652/htdocs/joomla';
  1057. $_SERVER['SCRIPT_FILENAME'] = '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php';
  1058. $request = Request::createFromGlobals();
  1059. $this->assertEquals('/dbhauser/index.php', $request->base);
  1060. $this->assertEquals('/dbhauser/webroot/', $request->webroot);
  1061. }
  1062. /**
  1063. * Test baseUrl with no rewrite and using the top level index.php.
  1064. *
  1065. * @return void
  1066. */
  1067. public function testBaseUrlNoRewriteTopLevelIndex() {
  1068. Configure::write('App.baseUrl', '/index.php');
  1069. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev';
  1070. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/index.php';
  1071. $request = Request::createFromGlobals();
  1072. $this->assertEquals('/index.php', $request->base);
  1073. $this->assertEquals('/webroot/', $request->webroot);
  1074. }
  1075. /**
  1076. * Check that a sub-directory containing app|webroot doesn't get mishandled when re-writing is off.
  1077. *
  1078. * @return void
  1079. */
  1080. public function testBaseUrlWithAppAndWebrootInDirname() {
  1081. Configure::write('App.baseUrl', '/approval/index.php');
  1082. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
  1083. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/approval/index.php';
  1084. $request = Request::createFromGlobals();
  1085. $this->assertEquals('/approval/index.php', $request->base);
  1086. $this->assertEquals('/approval/webroot/', $request->webroot);
  1087. Configure::write('App.baseUrl', '/webrootable/index.php');
  1088. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
  1089. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/webrootable/index.php';
  1090. $request = Request::createFromGlobals();
  1091. $this->assertEquals('/webrootable/index.php', $request->base);
  1092. $this->assertEquals('/webrootable/webroot/', $request->webroot);
  1093. }
  1094. /**
  1095. * Test baseUrl with no rewrite, and using the app/webroot/index.php file as is normal with virtual hosts.
  1096. *
  1097. * @return void
  1098. */
  1099. public function testBaseUrlNoRewriteWebrootIndex() {
  1100. Configure::write('App.baseUrl', '/index.php');
  1101. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev/webroot';
  1102. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/webroot/index.php';
  1103. $request = Request::createFromGlobals();
  1104. $this->assertEquals('/index.php', $request->base);
  1105. $this->assertEquals('/', $request->webroot);
  1106. }
  1107. /**
  1108. * Test that a request with a . in the main GET parameter is filtered out.
  1109. * PHP changes GET parameter keys containing dots to _.
  1110. *
  1111. * @return void
  1112. */
  1113. public function testGetParamsWithDot() {
  1114. $_GET = array();
  1115. $_GET['/posts/index/add_add'] = '';
  1116. $_SERVER['PHP_SELF'] = '/webroot/index.php';
  1117. $_SERVER['REQUEST_URI'] = '/posts/index/add.add';
  1118. $request = Request::createFromGlobals();
  1119. $this->assertEquals('', $request->base);
  1120. $this->assertEquals(array(), $request->query);
  1121. $_GET = array();
  1122. $_GET['/cake_dev/posts/index/add_add'] = '';
  1123. $_SERVER['PHP_SELF'] = '/cake_dev/webroot/index.php';
  1124. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/index/add.add';
  1125. $request = Request::createFromGlobals();
  1126. $this->assertEquals('/cake_dev', $request->base);
  1127. $this->assertEquals(array(), $request->query);
  1128. }
  1129. /**
  1130. * Test that a request with urlencoded bits in the main GET parameter are filtered out.
  1131. *
  1132. * @return void
  1133. */
  1134. public function testGetParamWithUrlencodedElement() {
  1135. $_GET = array();
  1136. $_GET['/posts/add/∂∂'] = '';
  1137. $_SERVER['PHP_SELF'] = '/webroot/index.php';
  1138. $_SERVER['REQUEST_URI'] = '/posts/add/%E2%88%82%E2%88%82';
  1139. $request = Request::createFromGlobals();
  1140. $this->assertEquals('', $request->base);
  1141. $this->assertEquals(array(), $request->query);
  1142. $_GET = array();
  1143. $_GET['/cake_dev/posts/add/∂∂'] = '';
  1144. $_SERVER['PHP_SELF'] = '/cake_dev/webroot/index.php';
  1145. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/add/%E2%88%82%E2%88%82';
  1146. $request = Request::createFromGlobals();
  1147. $this->assertEquals('/cake_dev', $request->base);
  1148. $this->assertEquals(array(), $request->query);
  1149. }
  1150. /**
  1151. * Generator for environment configurations
  1152. *
  1153. * @return array Environment array
  1154. */
  1155. public static function environmentGenerator() {
  1156. return array(
  1157. array(
  1158. 'IIS - No rewrite base path',
  1159. array(
  1160. 'App' => array(
  1161. 'base' => false,
  1162. 'baseUrl' => '/index.php',
  1163. 'dir' => 'TestApp',
  1164. 'webroot' => 'webroot'
  1165. ),
  1166. 'SERVER' => array(
  1167. 'SCRIPT_NAME' => '/index.php',
  1168. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1169. 'QUERY_STRING' => '',
  1170. 'REQUEST_URI' => '/index.php',
  1171. 'URL' => '/index.php',
  1172. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php',
  1173. 'ORIG_PATH_INFO' => '/index.php',
  1174. 'PATH_INFO' => '',
  1175. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php',
  1176. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1177. 'PHP_SELF' => '/index.php',
  1178. ),
  1179. ),
  1180. array(
  1181. 'base' => '/index.php',
  1182. 'webroot' => '/webroot/',
  1183. 'url' => ''
  1184. ),
  1185. ),
  1186. array(
  1187. 'IIS - No rewrite with path, no PHP_SELF',
  1188. array(
  1189. 'App' => array(
  1190. 'base' => false,
  1191. 'baseUrl' => '/index.php?',
  1192. 'dir' => 'TestApp',
  1193. 'webroot' => 'webroot'
  1194. ),
  1195. 'SERVER' => array(
  1196. 'QUERY_STRING' => '/posts/add',
  1197. 'REQUEST_URI' => '/index.php?/posts/add',
  1198. 'PHP_SELF' => '',
  1199. 'URL' => '/index.php?/posts/add',
  1200. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1201. 'argv' => array('/posts/add'),
  1202. 'argc' => 1
  1203. ),
  1204. ),
  1205. array(
  1206. 'url' => 'posts/add',
  1207. 'base' => '/index.php?',
  1208. 'webroot' => '/webroot/'
  1209. )
  1210. ),
  1211. array(
  1212. 'IIS - No rewrite sub dir 2',
  1213. array(
  1214. 'App' => array(
  1215. 'base' => false,
  1216. 'baseUrl' => '/site/index.php',
  1217. 'dir' => 'TestApp',
  1218. 'webroot' => 'webroot',
  1219. ),
  1220. 'SERVER' => array(
  1221. 'SCRIPT_NAME' => '/site/index.php',
  1222. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1223. 'QUERY_STRING' => '',
  1224. 'REQUEST_URI' => '/site/index.php',
  1225. 'URL' => '/site/index.php',
  1226. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1227. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1228. 'PHP_SELF' => '/site/index.php',
  1229. 'argv' => array(),
  1230. 'argc' => 0
  1231. ),
  1232. ),
  1233. array(
  1234. 'url' => '',
  1235. 'base' => '/site/index.php',
  1236. 'webroot' => '/site/webroot/'
  1237. ),
  1238. ),
  1239. array(
  1240. 'IIS - No rewrite sub dir 2 with path',
  1241. array(
  1242. 'App' => array(
  1243. 'base' => false,
  1244. 'baseUrl' => '/site/index.php',
  1245. 'dir' => 'TestApp',
  1246. 'webroot' => 'webroot'
  1247. ),
  1248. 'GET' => array('/posts/add' => ''),
  1249. 'SERVER' => array(
  1250. 'SCRIPT_NAME' => '/site/index.php',
  1251. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1252. 'QUERY_STRING' => '/posts/add',
  1253. 'REQUEST_URI' => '/site/index.php/posts/add',
  1254. 'URL' => '/site/index.php/posts/add',
  1255. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1256. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1257. 'PHP_SELF' => '/site/index.php/posts/add',
  1258. 'argv' => array('/posts/add'),
  1259. 'argc' => 1
  1260. ),
  1261. ),
  1262. array(
  1263. 'url' => 'posts/add',
  1264. 'base' => '/site/index.php',
  1265. 'webroot' => '/site/webroot/'
  1266. )
  1267. ),
  1268. array(
  1269. 'Apache - No rewrite, document root set to webroot, requesting path',
  1270. array(
  1271. 'App' => array(
  1272. 'base' => false,
  1273. 'baseUrl' => '/index.php',
  1274. 'dir' => 'TestApp',
  1275. 'webroot' => 'webroot'
  1276. ),
  1277. 'SERVER' => array(
  1278. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/App/webroot',
  1279. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/App/webroot/index.php',
  1280. 'QUERY_STRING' => '',
  1281. 'REQUEST_URI' => '/index.php/posts/index',
  1282. 'SCRIPT_NAME' => '/index.php',
  1283. 'PATH_INFO' => '/posts/index',
  1284. 'PHP_SELF' => '/index.php/posts/index',
  1285. ),
  1286. ),
  1287. array(
  1288. 'url' => 'posts/index',
  1289. 'base' => '/index.php',
  1290. 'webroot' => '/'
  1291. ),
  1292. ),
  1293. array(
  1294. 'Apache - No rewrite, document root set to webroot, requesting root',
  1295. array(
  1296. 'App' => array(
  1297. 'base' => false,
  1298. 'baseUrl' => '/index.php',
  1299. 'dir' => 'TestApp',
  1300. 'webroot' => 'webroot'
  1301. ),
  1302. 'SERVER' => array(
  1303. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/App/webroot',
  1304. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/App/webroot/index.php',
  1305. 'QUERY_STRING' => '',
  1306. 'REQUEST_URI' => '/index.php',
  1307. 'SCRIPT_NAME' => '/index.php',
  1308. 'PATH_INFO' => '',
  1309. 'PHP_SELF' => '/index.php',
  1310. ),
  1311. ),
  1312. array(
  1313. 'url' => '',
  1314. 'base' => '/index.php',
  1315. 'webroot' => '/'
  1316. ),
  1317. ),
  1318. array(
  1319. 'Apache - No rewrite, document root set above top level cake dir, requesting path',
  1320. array(
  1321. 'App' => array(
  1322. 'base' => false,
  1323. 'baseUrl' => '/site/index.php',
  1324. 'dir' => 'TestApp',
  1325. 'webroot' => 'webroot'
  1326. ),
  1327. 'SERVER' => array(
  1328. 'SERVER_NAME' => 'localhost',
  1329. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1330. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1331. 'REQUEST_URI' => '/site/index.php/posts/index',
  1332. 'SCRIPT_NAME' => '/site/index.php',
  1333. 'PATH_INFO' => '/posts/index',
  1334. 'PHP_SELF' => '/site/index.php/posts/index',
  1335. ),
  1336. ),
  1337. array(
  1338. 'url' => 'posts/index',
  1339. 'base' => '/site/index.php',
  1340. 'webroot' => '/site/webroot/',
  1341. ),
  1342. ),
  1343. array(
  1344. 'Apache - No rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1345. array(
  1346. 'App' => array(
  1347. 'base' => false,
  1348. 'baseUrl' => '/site/index.php',
  1349. 'dir' => 'TestApp',
  1350. 'webroot' => 'webroot'
  1351. ),
  1352. 'SERVER' => array(
  1353. 'SERVER_NAME' => 'localhost',
  1354. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1355. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1356. 'REQUEST_URI' => '/site/index.php/',
  1357. 'SCRIPT_NAME' => '/site/index.php',
  1358. 'PHP_SELF' => '/site/index.php/',
  1359. ),
  1360. ),
  1361. array(
  1362. 'url' => '',
  1363. 'base' => '/site/index.php',
  1364. 'webroot' => '/site/webroot/',
  1365. ),
  1366. ),
  1367. array(
  1368. 'Apache - No rewrite, document root set above top level cake dir, request path, with GET',
  1369. array(
  1370. 'App' => array(
  1371. 'base' => false,
  1372. 'baseUrl' => '/site/index.php',
  1373. 'dir' => 'TestApp',
  1374. 'webroot' => 'webroot'
  1375. ),
  1376. 'GET' => array('a' => 'b', 'c' => 'd'),
  1377. 'SERVER' => array(
  1378. 'SERVER_NAME' => 'localhost',
  1379. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1380. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1381. 'REQUEST_URI' => '/site/index.php/posts/index?a=b&c=d',
  1382. 'SCRIPT_NAME' => '/site/index.php',
  1383. 'PATH_INFO' => '/posts/index',
  1384. 'PHP_SELF' => '/site/index.php/posts/index',
  1385. 'QUERY_STRING' => 'a=b&c=d'
  1386. ),
  1387. ),
  1388. array(
  1389. 'urlParams' => array('a' => 'b', 'c' => 'd'),
  1390. 'url' => 'posts/index',
  1391. 'base' => '/site/index.php',
  1392. 'webroot' => '/site/webroot/',
  1393. ),
  1394. ),
  1395. array(
  1396. 'Apache - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1397. array(
  1398. 'App' => array(
  1399. 'base' => false,
  1400. 'baseUrl' => false,
  1401. 'dir' => 'TestApp',
  1402. 'webroot' => 'webroot'
  1403. ),
  1404. 'SERVER' => array(
  1405. 'SERVER_NAME' => 'localhost',
  1406. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1407. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1408. 'REQUEST_URI' => '/site/',
  1409. 'SCRIPT_NAME' => '/site/webroot/index.php',
  1410. 'PHP_SELF' => '/site/webroot/index.php',
  1411. ),
  1412. ),
  1413. array(
  1414. 'url' => '',
  1415. 'base' => '/site',
  1416. 'webroot' => '/site/',
  1417. ),
  1418. ),
  1419. array(
  1420. 'Apache - w/rewrite, document root above top level cake dir, request root, no PATH_INFO/REQUEST_URI',
  1421. array(
  1422. 'App' => array(
  1423. 'base' => false,
  1424. 'baseUrl' => false,
  1425. 'dir' => 'TestApp',
  1426. 'webroot' => 'webroot'
  1427. ),
  1428. 'SERVER' => array(
  1429. 'SERVER_NAME' => 'localhost',
  1430. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1431. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1432. 'SCRIPT_NAME' => '/site/webroot/index.php',
  1433. 'PHP_SELF' => '/site/webroot/index.php',
  1434. 'PATH_INFO' => null,
  1435. 'REQUEST_URI' => null,
  1436. ),
  1437. ),
  1438. array(
  1439. 'url' => '',
  1440. 'base' => '/site',
  1441. 'webroot' => '/site/',
  1442. ),
  1443. ),
  1444. array(
  1445. 'Apache - w/rewrite, document root set to webroot, request root, no PATH_INFO/REQUEST_URI',
  1446. array(
  1447. 'App' => array(
  1448. 'base' => false,
  1449. 'baseUrl' => false,
  1450. 'dir' => 'TestApp',
  1451. 'webroot' => 'webroot'
  1452. ),
  1453. 'SERVER' => array(
  1454. 'SERVER_NAME' => 'localhost',
  1455. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/webroot',
  1456. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/webroot/index.php',
  1457. 'SCRIPT_NAME' => '/index.php',
  1458. 'PHP_SELF' => '/index.php',
  1459. 'PATH_INFO' => null,
  1460. 'REQUEST_URI' => null,
  1461. ),
  1462. ),
  1463. array(
  1464. 'url' => '',
  1465. 'base' => '',
  1466. 'webroot' => '/',
  1467. ),
  1468. ),
  1469. array(
  1470. 'Apache - w/rewrite, document root set above top level cake dir, request root, absolute REQUEST_URI',
  1471. array(
  1472. 'App' => array(
  1473. 'base' => false,
  1474. 'baseUrl' => false,
  1475. 'dir' => 'TestApp',
  1476. 'webroot' => 'webroot'
  1477. ),
  1478. 'SERVER' => array(
  1479. 'SERVER_NAME' => 'localhost',
  1480. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1481. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1482. 'REQUEST_URI' => '/site/posts/index',
  1483. 'SCRIPT_NAME' => '/site/webroot/index.php',
  1484. 'PHP_SELF' => '/site/webroot/index.php',
  1485. ),
  1486. ),
  1487. array(
  1488. 'url' => 'posts/index',
  1489. 'base' => '/site',
  1490. 'webroot' => '/site/',
  1491. ),
  1492. ),
  1493. array(
  1494. 'Nginx - w/rewrite, document root set to webroot, request root, no PATH_INFO',
  1495. array(
  1496. 'App' => array(
  1497. 'base' => false,
  1498. 'baseUrl' => false,
  1499. 'dir' => 'TestApp',
  1500. 'webroot' => 'webroot'
  1501. ),
  1502. 'GET' => array('/posts/add' => ''),
  1503. 'SERVER' => array(
  1504. 'SERVER_NAME' => 'localhost',
  1505. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/webroot',
  1506. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/webroot/index.php',
  1507. 'SCRIPT_NAME' => '/index.php',
  1508. 'QUERY_STRING' => '/posts/add&',
  1509. 'PHP_SELF' => '/index.php',
  1510. 'PATH_INFO' => null,
  1511. 'REQUEST_URI' => '/posts/add',
  1512. ),
  1513. ),
  1514. array(
  1515. 'url' => 'posts/add',
  1516. 'base' => '',
  1517. 'webroot' => '/',
  1518. 'urlParams' => array()
  1519. ),
  1520. ),
  1521. array(
  1522. 'Nginx - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO, base parameter set',
  1523. array(
  1524. 'App' => array(
  1525. 'base' => false,
  1526. 'baseUrl' => false,
  1527. 'dir' => 'app',
  1528. 'webroot' => 'webroot'
  1529. ),
  1530. 'GET' => array('/site/posts/add' => ''),
  1531. 'SERVER' => array(
  1532. 'SERVER_NAME' => 'localhost',
  1533. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1534. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/App/webroot/index.php',
  1535. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1536. 'QUERY_STRING' => '/site/posts/add&',
  1537. 'PHP_SELF' => '/site/webroot/index.php',
  1538. 'PATH_INFO' => null,
  1539. 'REQUEST_URI' => '/site/posts/add',
  1540. ),
  1541. ),
  1542. array(
  1543. 'url' => 'posts/add',
  1544. 'base' => '/site',
  1545. 'webroot' => '/site/',
  1546. 'urlParams' => array()
  1547. ),
  1548. ),
  1549. );
  1550. }
  1551. /**
  1552. * Test environment detection
  1553. *
  1554. * @dataProvider environmentGenerator
  1555. * @param $name
  1556. * @param $env
  1557. * @param $expected
  1558. * @return void
  1559. */
  1560. public function testEnvironmentDetection($name, $env, $expected) {
  1561. $_GET = array();
  1562. $this->_loadEnvironment($env);
  1563. $request = Request::createFromGlobals();
  1564. $this->assertEquals($expected['url'], $request->url, "url error");
  1565. $this->assertEquals($expected['base'], $request->base, "base error");
  1566. $this->assertEquals($expected['webroot'], $request->webroot, "webroot error");
  1567. if (isset($expected['urlParams'])) {
  1568. $this->assertEquals($expected['urlParams'], $request->query, "GET param mismatch");
  1569. }
  1570. }
  1571. /**
  1572. * Test the query() method
  1573. *
  1574. * @return void
  1575. */
  1576. public function testQuery() {
  1577. $request = new Request([
  1578. 'query' => ['foo' => 'bar']
  1579. ]);
  1580. $result = $request->query('foo');
  1581. $this->assertEquals('bar', $result);
  1582. $result = $request->query('imaginary');
  1583. $this->assertNull($result);
  1584. }
  1585. /**
  1586. * Test the query() method with arrays passed via $_GET
  1587. *
  1588. * @return void
  1589. */
  1590. public function testQueryWithArray() {
  1591. $get['test'] = array('foo', 'bar');
  1592. $request = new Request([
  1593. 'query' => $get
  1594. ]);
  1595. $result = $request->query('test');
  1596. $this->assertEquals(array('foo', 'bar'), $result);
  1597. $result = $request->query('test.1');
  1598. $this->assertEquals('bar', $result);
  1599. $result = $request->query('test.2');
  1600. $this->assertNull($result);
  1601. }
  1602. /**
  1603. * Test using param()
  1604. *
  1605. * @return void
  1606. */
  1607. public function testReadingParams() {
  1608. $request = new Request();
  1609. $request->addParams(array(
  1610. 'controller' => 'posts',
  1611. 'admin' => true,
  1612. 'truthy' => 1,
  1613. 'zero' => '0',
  1614. ));
  1615. $this->assertFalse($request->param('not_set'));
  1616. $this->assertTrue($request->param('admin'));
  1617. $this->assertEquals(1, $request->param('truthy'));
  1618. $this->assertEquals('posts', $request->param('controller'));
  1619. $this->assertEquals('0', $request->param('zero'));
  1620. }
  1621. /**
  1622. * Test the data() method reading
  1623. *
  1624. * @return void
  1625. */
  1626. public function testDataReading() {
  1627. $post = array(
  1628. 'Model' => array(
  1629. 'field' => 'value'
  1630. )
  1631. );
  1632. $request = new Request(compact('post'));
  1633. $result = $request->data('Model');
  1634. $this->assertEquals($post['Model'], $result);
  1635. $result = $request->data();
  1636. $this->assertEquals($post, $result);
  1637. $result = $request->data('Model.imaginary');
  1638. $this->assertNull($result);
  1639. }
  1640. /**
  1641. * Test writing with data()
  1642. *
  1643. * @return void
  1644. */
  1645. public function testDataWriting() {
  1646. $_POST['data'] = array(
  1647. 'Model' => array(
  1648. 'field' => 'value'
  1649. )
  1650. );
  1651. $request = new Request();
  1652. $result = $request->data('Model.new_value', 'new value');
  1653. $this->assertSame($result, $request, 'Return was not $this');
  1654. $this->assertEquals('new value', $request->data['Model']['new_value']);
  1655. $request->data('Post.title', 'New post')->data('Comment.1.author', 'Mark');
  1656. $this->assertEquals('New post', $request->data['Post']['title']);
  1657. $this->assertEquals('Mark', $request->data['Comment']['1']['author']);
  1658. }
  1659. /**
  1660. * Test writing falsey values.
  1661. *
  1662. * @return void
  1663. */
  1664. public function testDataWritingFalsey() {
  1665. $request = new Request();
  1666. $request->data('Post.null', null);
  1667. $this->assertNull($request->data['Post']['null']);
  1668. $request->data('Post.false', false);
  1669. $this->assertFalse($request->data['Post']['false']);
  1670. $request->data('Post.zero', 0);
  1671. $this->assertSame(0, $request->data['Post']['zero']);
  1672. $request->data('Post.empty', '');
  1673. $this->assertSame('', $request->data['Post']['empty']);
  1674. }
  1675. /**
  1676. * Test reading params
  1677. *
  1678. * @dataProvider paramReadingDataProvider
  1679. */
  1680. public function testParamReading($toRead, $expected) {
  1681. $request = new Request('/');
  1682. $request->addParams(array(
  1683. 'action' => 'index',
  1684. 'foo' => 'bar',
  1685. 'baz' => array(
  1686. 'a' => array(
  1687. 'b' => 'c',
  1688. ),
  1689. ),
  1690. 'admin' => true,
  1691. 'truthy' => 1,
  1692. 'zero' => '0',
  1693. ));
  1694. $this->assertSame($expected, $request->param($toRead));
  1695. }
  1696. /**
  1697. * Data provider for testing reading values with Request::param()
  1698. *
  1699. * @return array
  1700. */
  1701. public function paramReadingDataProvider() {
  1702. return array(
  1703. array(
  1704. 'action',
  1705. 'index',
  1706. ),
  1707. array(
  1708. 'baz',
  1709. array(
  1710. 'a' => array(
  1711. 'b' => 'c',
  1712. ),
  1713. ),
  1714. ),
  1715. array(
  1716. 'baz.a.b',
  1717. 'c',
  1718. ),
  1719. array(
  1720. 'does_not_exist',
  1721. false,
  1722. ),
  1723. array(
  1724. 'admin',
  1725. true,
  1726. ),
  1727. array(
  1728. 'truthy',
  1729. 1,
  1730. ),
  1731. array(
  1732. 'zero',
  1733. '0',
  1734. ),
  1735. );
  1736. }
  1737. /**
  1738. * test writing request params with param()
  1739. *
  1740. * @return void
  1741. */
  1742. public function testParamWriting() {
  1743. $request = new Request('/');
  1744. $request->addParams(array(
  1745. 'action' => 'index',
  1746. ));
  1747. $this->assertInstanceOf('Cake\Network\Request', $request->param('some', 'thing'), 'Method has not returned $this');
  1748. $request->param('Post.null', null);
  1749. $this->assertNull($request->params['Post']['null']);
  1750. $request->param('Post.false', false);
  1751. $this->assertFalse($request->params['Post']['false']);
  1752. $request->param('Post.zero', 0);
  1753. $this->assertSame(0, $request->params['Post']['zero']);
  1754. $request->param('Post.empty', '');
  1755. $this->assertSame('', $request->params['Post']['empty']);
  1756. $this->assertSame('index', $request->action);
  1757. $request->param('action', 'edit');
  1758. $this->assertSame('edit', $request->action);
  1759. }
  1760. /**
  1761. * Test accept language
  1762. *
  1763. * @return void
  1764. */
  1765. public function testAcceptLanguage() {
  1766. $request = new Request();
  1767. // Weird language
  1768. $request->env('HTTP_ACCEPT_LANGUAGE', 'inexistent,en-ca');
  1769. $result = $request->acceptLanguage();
  1770. $this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
  1771. // No qualifier
  1772. $request->env('HTTP_ACCEPT_LANGUAGE', 'es_mx,en_ca');
  1773. $result = $request->acceptLanguage();
  1774. $this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
  1775. // With qualifier
  1776. $request->env('HTTP_ACCEPT_LANGUAGE', 'en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4');
  1777. $result = $request->acceptLanguage();
  1778. $this->assertEquals(array('en-us', 'en', 'pt-br', 'pt'), $result, 'Languages do not match');
  1779. // With spaces
  1780. $request->env('HTTP_ACCEPT_LANGUAGE', 'da, en-gb;q=0.8, en;q=0.7');
  1781. $result = $request->acceptLanguage();
  1782. $this->assertEquals(array('da', 'en-gb', 'en'), $result, 'Languages do not match');
  1783. // Checking if requested
  1784. $request->env('HTTP_ACCEPT_LANGUAGE', 'es_mx,en_ca');
  1785. $result = $request->acceptLanguage();
  1786. $result = $request->acceptLanguage('en-ca');
  1787. $this->assertTrue($result);
  1788. $result = $request->acceptLanguage('en-CA');
  1789. $this->assertTrue($result);
  1790. $result = $request->acceptLanguage('en-us');
  1791. $this->assertFalse($result);
  1792. $result = $request->acceptLanguage('en-US');
  1793. $this->assertFalse($result);
  1794. }
  1795. /**
  1796. * Test the here() method
  1797. *
  1798. * @return void
  1799. */
  1800. public function testHere() {
  1801. Configure::write('App.base', '/base_path');
  1802. $q = array('test' => 'value');
  1803. $request = new Request(array(
  1804. 'query' => $q,
  1805. 'url' => '/posts/add/1/value',
  1806. 'base' => '/base_path'
  1807. ));
  1808. $result = $request->here();
  1809. $this->assertEquals('/base_path/posts/add/1/value?test=value', $result);
  1810. $result = $request->here(false);
  1811. $this->assertEquals('/posts/add/1/value?test=value', $result);
  1812. $request = new Request(array(
  1813. 'url' => '/posts/base_path/1/value',
  1814. 'query' => array('test' => 'value'),
  1815. 'base' => '/base_path'
  1816. ));
  1817. $result = $request->here();
  1818. $this->assertEquals('/base_path/posts/base_path/1/value?test=value', $result);
  1819. $result = $request->here(false);
  1820. $this->assertEquals('/posts/base_path/1/value?test=value', $result);
  1821. }
  1822. /**
  1823. * Test the here() with space in URL
  1824. *
  1825. * @return void
  1826. */
  1827. public function testHereWithSpaceInUrl() {
  1828. Configure::write('App.base', '');
  1829. $_GET = array('/admin/settings/settings/prefix/Access_Control' => '');
  1830. $request = new Request('/admin/settings/settings/prefix/Access%20Control');
  1831. $result = $request->here();
  1832. $this->assertEquals('/admin/settings/settings/prefix/Access%20Control', $result);
  1833. }
  1834. /**
  1835. * Test the input() method.
  1836. *
  1837. * @return void
  1838. */
  1839. public function testSetInput() {
  1840. $request = new Request();
  1841. $request->setInput('I came from setInput');
  1842. $result = $request->input();
  1843. $this->assertEquals('I came from setInput', $result);
  1844. }
  1845. /**
  1846. * Test the input() method.
  1847. *
  1848. * @return void
  1849. */
  1850. public function testInput() {
  1851. $request = $this->getMock('Cake\Network\Request', array('_readInput'));
  1852. $request->expects($this->once())->method('_readInput')
  1853. ->will($this->returnValue('I came from stdin'));
  1854. $result = $request->input();
  1855. $this->assertEquals('I came from stdin', $result);
  1856. }
  1857. /**
  1858. * Test input() decoding.
  1859. *
  1860. * @return void
  1861. */
  1862. public function testInputDecode() {
  1863. $request = $this->getMock('Cake\Network\Request', array('_readInput'));
  1864. $request->expects($this->once())->method('_readInput')
  1865. ->will($this->returnValue('{"name":"value"}'));
  1866. $result = $request->input('json_decode');
  1867. $this->assertEquals(array('name' => 'value'), (array)$result);
  1868. }
  1869. /**
  1870. * Test input() decoding with additional arguments.
  1871. *
  1872. * @return void
  1873. */
  1874. public function testInputDecodeExtraParams() {
  1875. $xml = <<<XML
  1876. <?xml version="1.0" encoding="utf-8"?>
  1877. <post>
  1878. <title id="title">Test</title>
  1879. </post>
  1880. XML;
  1881. $request = $this->getMock('Cake\Network\Request', array('_readInput'));
  1882. $request->expects($this->once())->method('_readInput')
  1883. ->will($this->returnValue($xml));
  1884. $result = $request->input('Cake\Utility\Xml::build', array('return' => 'domdocument'));
  1885. $this->assertInstanceOf('DOMDocument', $result);
  1886. $this->assertEquals(
  1887. 'Test',
  1888. $result->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText
  1889. );
  1890. }
  1891. /**
  1892. * Test is('requested') and isRequested()
  1893. *
  1894. * @return void
  1895. */
  1896. public function testIsRequested() {
  1897. $request = new Request();
  1898. $request->addParams(array(
  1899. 'controller' => 'posts',
  1900. 'action' => 'index',
  1901. 'plugin' => null,
  1902. 'requested' => 1
  1903. ));
  1904. $this->assertTrue($request->is('requested'));
  1905. $this->assertTrue($request->isRequested());
  1906. $request = new Request();
  1907. $request->addParams(array(
  1908. 'controller' => 'posts',
  1909. 'action' => 'index',
  1910. 'plugin' => null,
  1911. ));
  1912. $this->assertFalse($request->is('requested'));
  1913. $this->assertFalse($request->isRequested());
  1914. }
  1915. /**
  1916. * Test the cookie() method.
  1917. *
  1918. * @return void
  1919. */
  1920. public function testReadCookie() {
  1921. $request = new Request(array(
  1922. 'cookies' => array(
  1923. 'testing' => 'A value in the cookie'
  1924. )
  1925. ));
  1926. $result = $request->cookie('testing');
  1927. $this->assertEquals('A value in the cookie', $result);
  1928. $result = $request->cookie('not there');
  1929. $this->assertNull($result);
  1930. }
  1931. /**
  1932. * TestAllowMethod
  1933. *
  1934. * @return void
  1935. */
  1936. public function testAllowMethod() {
  1937. $request = new Request(['environment' => [
  1938. 'url' => '/posts/edit/1',
  1939. 'REQUEST_METHOD' => 'PUT'
  1940. ]]);
  1941. $this->assertTrue($request->allowMethod('put'));
  1942. $request->env('REQUEST_METHOD', 'DELETE');
  1943. $this->assertTrue($request->allowMethod(['post', 'delete']));
  1944. }
  1945. /**
  1946. * Test allowMethod throwing exception
  1947. *
  1948. * @return void
  1949. */
  1950. public function testAllowMethodException() {
  1951. $request = new Request([
  1952. 'url' => '/posts/edit/1',
  1953. 'environment' => ['REQUEST_METHOD' => 'PUT']
  1954. ]);
  1955. try {
  1956. $request->allowMethod(['POST', 'DELETE']);
  1957. $this->fail('An expected exception has not been raised.');
  1958. } catch (Error\MethodNotAllowedException $e) {
  1959. $this->assertEquals(array('Allow' => 'POST, DELETE'), $e->responseHeader());
  1960. }
  1961. $this->setExpectedException('Cake\Error\MethodNotAllowedException');
  1962. $request->allowMethod('POST');
  1963. }
  1964. /**
  1965. * Tests getting the sessions from the request
  1966. *
  1967. * @return void
  1968. */
  1969. public function testSession() {
  1970. $session = new Session;
  1971. $request = new Request(['session' => $session]);
  1972. $this->assertSame($session, $request->session());
  1973. $request = Request::createFromGlobals();
  1974. $this->assertEquals($session, $request->session());
  1975. }
  1976. /**
  1977. * loadEnvironment method
  1978. *
  1979. * @param array $env
  1980. * @return void
  1981. */
  1982. protected function _loadEnvironment($env) {
  1983. if (isset($env['App'])) {
  1984. Configure::write('App', $env['App']);
  1985. }
  1986. if (isset($env['GET'])) {
  1987. foreach ($env['GET'] as $key => $val) {
  1988. $_GET[$key] = $val;
  1989. }
  1990. }
  1991. if (isset($env['POST'])) {
  1992. foreach ($env['POST'] as $key => $val) {
  1993. $_POST[$key] = $val;
  1994. }
  1995. }
  1996. if (isset($env['SERVER'])) {
  1997. foreach ($env['SERVER'] as $key => $val) {
  1998. $_SERVER[$key] = $val;
  1999. }
  2000. }
  2001. }
  2002. }