RequestTest.php 61 KB

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