CakeRequestTest.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764
  1. <?php
  2. /**
  3. * CakeRequest Test case file.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Test.Case.Network
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Dispatcher', 'Routing');
  20. App::uses('Xml', 'Utility');
  21. App::uses('CakeRequest', 'Network');
  22. class CakeRequestTest extends CakeTestCase {
  23. /**
  24. * setup callback
  25. *
  26. * @return void
  27. */
  28. public function setUp() {
  29. parent::setUp();
  30. $this->_server = $_SERVER;
  31. $this->_get = $_GET;
  32. $this->_post = $_POST;
  33. $this->_files = $_FILES;
  34. $this->_app = Configure::read('App');
  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. $_SERVER = $this->_server;
  50. $_GET = $this->_get;
  51. $_POST = $this->_post;
  52. $_FILES = $this->_files;
  53. if (!empty($this->_case)) {
  54. $_GET['case'] = $this->_case;
  55. }
  56. Configure::write('App', $this->_app);
  57. }
  58. /**
  59. * test that the autoparse = false constructor works.
  60. *
  61. * @return void
  62. */
  63. public function testNoAutoParseConstruction() {
  64. $_GET = array(
  65. 'one' => 'param'
  66. );
  67. $request = new CakeRequest(null, false);
  68. $this->assertFalse(isset($request->query['one']));
  69. }
  70. /**
  71. * test construction
  72. *
  73. * @return void
  74. */
  75. public function testConstructionGetParsing() {
  76. $_GET = array(
  77. 'one' => 'param',
  78. 'two' => 'banana'
  79. );
  80. $request = new CakeRequest('some/path');
  81. $this->assertEquals($request->query, $_GET);
  82. $_GET = array(
  83. 'one' => 'param',
  84. 'two' => 'banana',
  85. );
  86. $request = new CakeRequest('some/path');
  87. $this->assertEquals($request->query, $_GET);
  88. $this->assertEquals($request->url, 'some/path');
  89. }
  90. /**
  91. * Test that querystring args provided in the url string are parsed.
  92. *
  93. * @return void
  94. */
  95. public function testQueryStringParsingFromInputUrl() {
  96. $_GET = array();
  97. $request = new CakeRequest('some/path?one=something&two=else');
  98. $expected = array('one' => 'something', 'two' => 'else');
  99. $this->assertEquals($expected, $request->query);
  100. $this->assertEquals('some/path?one=something&two=else', $request->url);
  101. }
  102. /**
  103. * Test that named arguments + querystrings are handled correctly.
  104. *
  105. * @return void
  106. */
  107. public function testQueryStringAndNamedParams() {
  108. $_SERVER['REQUEST_URI'] = '/tasks/index/page:1?ts=123456';
  109. $request = new CakeRequest();
  110. $this->assertEquals('tasks/index/page:1', $request->url);
  111. $_SERVER['REQUEST_URI'] = '/tasks/index/page:1/?ts=123456';
  112. $request = new CakeRequest();
  113. $this->assertEquals('tasks/index/page:1/', $request->url);
  114. }
  115. /**
  116. * test addParams() method
  117. *
  118. * @return void
  119. */
  120. public function testAddParams() {
  121. $request = new CakeRequest('some/path');
  122. $request->params = array('controller' => 'posts', 'action' => 'view');
  123. $result = $request->addParams(array('plugin' => null, 'action' => 'index'));
  124. $this->assertSame($result, $request, 'Method did not return itself. %s');
  125. $this->assertEquals($request->controller, 'posts');
  126. $this->assertEquals($request->action, 'index');
  127. $this->assertEquals($request->plugin, null);
  128. }
  129. /**
  130. * test splicing in paths.
  131. *
  132. * @return void
  133. */
  134. public function testAddPaths() {
  135. $request = new CakeRequest('some/path');
  136. $request->webroot = '/some/path/going/here/';
  137. $result = $request->addPaths(array(
  138. 'random' => '/something', 'webroot' => '/', 'here' => '/', 'base' => '/base_dir'
  139. ));
  140. $this->assertSame($result, $request, 'Method did not return itself. %s');
  141. $this->assertEquals($request->webroot, '/');
  142. $this->assertEquals($request->base, '/base_dir');
  143. $this->assertEquals($request->here, '/');
  144. $this->assertFalse(isset($request->random));
  145. }
  146. /**
  147. * test parsing POST data into the object.
  148. *
  149. * @return void
  150. */
  151. public function testPostParsing() {
  152. $_POST = array('data' => array(
  153. 'Article' => array('title')
  154. ));
  155. $request = new CakeRequest('some/path');
  156. $this->assertEquals($_POST['data'], $request->data);
  157. $_POST = array('one' => 1, 'two' => 'three');
  158. $request = new CakeRequest('some/path');
  159. $this->assertEquals($_POST, $request->data);
  160. $_POST = array(
  161. 'data' => array(
  162. 'Article' => array('title' => 'Testing'),
  163. ),
  164. 'action' => 'update'
  165. );
  166. $request = new CakeRequest('some/path');
  167. $expected = array(
  168. 'Article' => array('title' => 'Testing'),
  169. 'action' => 'update'
  170. );
  171. $this->assertEquals($expected, $request->data);
  172. $_POST = array('data' => array(
  173. 'Article' => array('title'),
  174. 'Tag' => array('Tag' => array(1, 2))
  175. ));
  176. $request = new CakeRequest('some/path');
  177. $this->assertEquals($_POST['data'], $request->data);
  178. $_POST = array('data' => array(
  179. 'Article' => array('title' => 'some title'),
  180. 'Tag' => array('Tag' => array(1, 2))
  181. ));
  182. $request = new CakeRequest('some/path');
  183. $this->assertEquals($_POST['data'], $request->data);
  184. $_POST = array(
  185. 'a' => array(1, 2),
  186. 'b' => array(1, 2)
  187. );
  188. $request = new CakeRequest('some/path');
  189. $this->assertEquals($_POST, $request->data);
  190. }
  191. /**
  192. * test parsing of FILES array
  193. *
  194. * @return void
  195. */
  196. public function testFILESParsing() {
  197. $_FILES = array('data' => array('name' => array(
  198. 'File' => array(
  199. array('data' => 'cake_sqlserver_patch.patch'),
  200. array('data' => 'controller.diff'),
  201. array('data' => ''),
  202. array('data' => ''),
  203. ),
  204. 'Post' => array('attachment' => 'jquery-1.2.1.js'),
  205. ),
  206. 'type' => array(
  207. 'File' => array(
  208. array('data' => ''),
  209. array('data' => ''),
  210. array('data' => ''),
  211. array('data' => ''),
  212. ),
  213. 'Post' => array('attachment' => 'application/x-javascript'),
  214. ),
  215. 'tmp_name' => array(
  216. 'File' => array(
  217. array('data' => '/private/var/tmp/phpy05Ywj'),
  218. array('data' => '/private/var/tmp/php7MBztY'),
  219. array('data' => ''),
  220. array('data' => ''),
  221. ),
  222. 'Post' => array('attachment' => '/private/var/tmp/phpEwlrIo'),
  223. ),
  224. 'error' => array(
  225. 'File' => array(
  226. array('data' => 0),
  227. array('data' => 0),
  228. array('data' => 4),
  229. array('data' => 4)
  230. ),
  231. 'Post' => array('attachment' => 0)
  232. ),
  233. 'size' => array(
  234. 'File' => array(
  235. array('data' => 6271),
  236. array('data' => 350),
  237. array('data' => 0),
  238. array('data' => 0),
  239. ),
  240. 'Post' => array('attachment' => 80469)
  241. ),
  242. ));
  243. $request = new CakeRequest('some/path');
  244. $expected = array(
  245. 'File' => array(
  246. array('data' => array(
  247. 'name' => 'cake_sqlserver_patch.patch',
  248. 'type' => '',
  249. 'tmp_name' => '/private/var/tmp/phpy05Ywj',
  250. 'error' => 0,
  251. 'size' => 6271,
  252. )),
  253. array(
  254. 'data' => array(
  255. 'name' => 'controller.diff',
  256. 'type' => '',
  257. 'tmp_name' => '/private/var/tmp/php7MBztY',
  258. 'error' => 0,
  259. 'size' => 350,
  260. )),
  261. array('data' => array(
  262. 'name' => '',
  263. 'type' => '',
  264. 'tmp_name' => '',
  265. 'error' => 4,
  266. 'size' => 0,
  267. )),
  268. array('data' => array(
  269. 'name' => '',
  270. 'type' => '',
  271. 'tmp_name' => '',
  272. 'error' => 4,
  273. 'size' => 0,
  274. )),
  275. ),
  276. 'Post' => array('attachment' => array(
  277. 'name' => 'jquery-1.2.1.js',
  278. 'type' => 'application/x-javascript',
  279. 'tmp_name' => '/private/var/tmp/phpEwlrIo',
  280. 'error' => 0,
  281. 'size' => 80469,
  282. ))
  283. );
  284. $this->assertEquals($request->data, $expected);
  285. $_FILES = array(
  286. 'data' => array(
  287. 'name' => array(
  288. 'Document' => array(
  289. 1 => array(
  290. 'birth_cert' => 'born on.txt',
  291. 'passport' => 'passport.txt',
  292. 'drivers_license' => 'ugly pic.jpg'
  293. ),
  294. 2 => array(
  295. 'birth_cert' => 'aunt betty.txt',
  296. 'passport' => 'betty-passport.txt',
  297. 'drivers_license' => 'betty-photo.jpg'
  298. ),
  299. ),
  300. ),
  301. 'type' => array(
  302. 'Document' => array(
  303. 1 => array(
  304. 'birth_cert' => 'application/octet-stream',
  305. 'passport' => 'application/octet-stream',
  306. 'drivers_license' => 'application/octet-stream',
  307. ),
  308. 2 => array(
  309. 'birth_cert' => 'application/octet-stream',
  310. 'passport' => 'application/octet-stream',
  311. 'drivers_license' => 'application/octet-stream',
  312. )
  313. )
  314. ),
  315. 'tmp_name' => array(
  316. 'Document' => array(
  317. 1 => array(
  318. 'birth_cert' => '/private/var/tmp/phpbsUWfH',
  319. 'passport' => '/private/var/tmp/php7f5zLt',
  320. 'drivers_license' => '/private/var/tmp/phpMXpZgT',
  321. ),
  322. 2 => array(
  323. 'birth_cert' => '/private/var/tmp/php5kHZt0',
  324. 'passport' => '/private/var/tmp/phpnYkOuM',
  325. 'drivers_license' => '/private/var/tmp/php9Rq0P3',
  326. )
  327. )
  328. ),
  329. 'error' => array(
  330. 'Document' => array(
  331. 1 => array(
  332. 'birth_cert' => 0,
  333. 'passport' => 0,
  334. 'drivers_license' => 0,
  335. ),
  336. 2 => array(
  337. 'birth_cert' => 0,
  338. 'passport' => 0,
  339. 'drivers_license' => 0,
  340. )
  341. )
  342. ),
  343. 'size' => array(
  344. 'Document' => array(
  345. 1 => array(
  346. 'birth_cert' => 123,
  347. 'passport' => 458,
  348. 'drivers_license' => 875,
  349. ),
  350. 2 => array(
  351. 'birth_cert' => 876,
  352. 'passport' => 976,
  353. 'drivers_license' => 9783,
  354. )
  355. )
  356. )
  357. )
  358. );
  359. $request = new CakeRequest('some/path');
  360. $expected = array(
  361. 'Document' => array(
  362. 1 => array(
  363. 'birth_cert' => array(
  364. 'name' => 'born on.txt',
  365. 'tmp_name' => '/private/var/tmp/phpbsUWfH',
  366. 'error' => 0,
  367. 'size' => 123,
  368. 'type' => 'application/octet-stream',
  369. ),
  370. 'passport' => array(
  371. 'name' => 'passport.txt',
  372. 'tmp_name' => '/private/var/tmp/php7f5zLt',
  373. 'error' => 0,
  374. 'size' => 458,
  375. 'type' => 'application/octet-stream',
  376. ),
  377. 'drivers_license' => array(
  378. 'name' => 'ugly pic.jpg',
  379. 'tmp_name' => '/private/var/tmp/phpMXpZgT',
  380. 'error' => 0,
  381. 'size' => 875,
  382. 'type' => 'application/octet-stream',
  383. ),
  384. ),
  385. 2 => array(
  386. 'birth_cert' => array(
  387. 'name' => 'aunt betty.txt',
  388. 'tmp_name' => '/private/var/tmp/php5kHZt0',
  389. 'error' => 0,
  390. 'size' => 876,
  391. 'type' => 'application/octet-stream',
  392. ),
  393. 'passport' => array(
  394. 'name' => 'betty-passport.txt',
  395. 'tmp_name' => '/private/var/tmp/phpnYkOuM',
  396. 'error' => 0,
  397. 'size' => 976,
  398. 'type' => 'application/octet-stream',
  399. ),
  400. 'drivers_license' => array(
  401. 'name' => 'betty-photo.jpg',
  402. 'tmp_name' => '/private/var/tmp/php9Rq0P3',
  403. 'error' => 0,
  404. 'size' => 9783,
  405. 'type' => 'application/octet-stream',
  406. ),
  407. ),
  408. )
  409. );
  410. $this->assertEquals($request->data, $expected);
  411. $_FILES = array(
  412. 'data' => array(
  413. 'name' => array('birth_cert' => 'born on.txt'),
  414. 'type' => array('birth_cert' => 'application/octet-stream'),
  415. 'tmp_name' => array('birth_cert' => '/private/var/tmp/phpbsUWfH'),
  416. 'error' => array('birth_cert' => 0),
  417. 'size' => array('birth_cert' => 123)
  418. )
  419. );
  420. $request = new CakeRequest('some/path');
  421. $expected = array(
  422. 'birth_cert' => array(
  423. 'name' => 'born on.txt',
  424. 'type' => 'application/octet-stream',
  425. 'tmp_name' => '/private/var/tmp/phpbsUWfH',
  426. 'error' => 0,
  427. 'size' => 123
  428. )
  429. );
  430. $this->assertEquals($request->data, $expected);
  431. $_FILES = array(
  432. 'something' => array(
  433. 'name' => 'something.txt',
  434. 'type' => 'text/plain',
  435. 'tmp_name' => '/some/file',
  436. 'error' => 0,
  437. 'size' => 123
  438. )
  439. );
  440. $request = new CakeRequest('some/path');
  441. $this->assertEquals($request->params['form'], $_FILES);
  442. }
  443. /**
  444. * test method overrides coming in from POST data.
  445. *
  446. * @return void
  447. */
  448. public function testMethodOverrides() {
  449. $_POST = array('_method' => 'POST');
  450. $request = new CakeRequest('some/path');
  451. $this->assertEquals(env('REQUEST_METHOD'), 'POST');
  452. $_POST = array('_method' => 'DELETE');
  453. $request = new CakeRequest('some/path');
  454. $this->assertEquals(env('REQUEST_METHOD'), 'DELETE');
  455. $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
  456. $request = new CakeRequest('some/path');
  457. $this->assertEquals(env('REQUEST_METHOD'), 'PUT');
  458. }
  459. /**
  460. * test the clientIp method.
  461. *
  462. * @return void
  463. */
  464. public function testclientIp() {
  465. $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.5, 10.0.1.1, proxy.com';
  466. $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
  467. $_SERVER['REMOTE_ADDR'] = '192.168.1.3';
  468. $request = new CakeRequest('some/path');
  469. $this->assertEquals($request->clientIp(false), '192.168.1.5');
  470. $this->assertEquals($request->clientIp(), '192.168.1.2');
  471. unset($_SERVER['HTTP_X_FORWARDED_FOR']);
  472. $this->assertEquals($request->clientIp(), '192.168.1.2');
  473. unset($_SERVER['HTTP_CLIENT_IP']);
  474. $this->assertEquals($request->clientIp(), '192.168.1.3');
  475. $_SERVER['HTTP_CLIENTADDRESS'] = '10.0.1.2, 10.0.1.1';
  476. $this->assertEquals($request->clientIp(), '10.0.1.2');
  477. }
  478. /**
  479. * test the referer function.
  480. *
  481. * @return void
  482. */
  483. public function testReferer() {
  484. $request = new CakeRequest('some/path');
  485. $request->webroot = '/';
  486. $_SERVER['HTTP_REFERER'] = 'http://cakephp.org';
  487. $result = $request->referer();
  488. $this->assertSame($result, 'http://cakephp.org');
  489. $_SERVER['HTTP_REFERER'] = '';
  490. $result = $request->referer();
  491. $this->assertSame($result, '/');
  492. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
  493. $result = $request->referer(true);
  494. $this->assertSame($result, '/some/path');
  495. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
  496. $result = $request->referer(false);
  497. $this->assertSame($result, FULL_BASE_URL . '/some/path');
  498. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
  499. $result = $request->referer(true);
  500. $this->assertSame($result, '/some/path');
  501. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/recipes/add';
  502. $result = $request->referer(true);
  503. $this->assertSame($result, '/recipes/add');
  504. $_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org';
  505. $result = $request->referer();
  506. $this->assertSame($result, 'cakephp.org');
  507. }
  508. /**
  509. * test the simple uses of is()
  510. *
  511. * @return void
  512. */
  513. public function testIsHttpMethods() {
  514. $request = new CakeRequest('some/path');
  515. $this->assertFalse($request->is('undefined-behavior'));
  516. $_SERVER['REQUEST_METHOD'] = 'GET';
  517. $this->assertTrue($request->is('get'));
  518. $_SERVER['REQUEST_METHOD'] = 'POST';
  519. $this->assertTrue($request->is('POST'));
  520. $_SERVER['REQUEST_METHOD'] = 'PUT';
  521. $this->assertTrue($request->is('put'));
  522. $this->assertFalse($request->is('get'));
  523. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  524. $this->assertTrue($request->is('delete'));
  525. $this->assertTrue($request->isDelete());
  526. $_SERVER['REQUEST_METHOD'] = 'delete';
  527. $this->assertFalse($request->is('delete'));
  528. }
  529. /**
  530. * test the method() method.
  531. *
  532. * @return void
  533. */
  534. public function testMethod() {
  535. $_SERVER['REQUEST_METHOD'] = 'delete';
  536. $request = new CakeRequest('some/path');
  537. $this->assertEquals('delete', $request->method());
  538. }
  539. /**
  540. * test host retrieval.
  541. *
  542. * @return void
  543. */
  544. public function testHost() {
  545. $_SERVER['HTTP_HOST'] = 'localhost';
  546. $request = new CakeRequest('some/path');
  547. $this->assertEquals('localhost', $request->host());
  548. }
  549. /**
  550. * test domain retrieval.
  551. *
  552. * @return void
  553. */
  554. public function testDomain() {
  555. $_SERVER['HTTP_HOST'] = 'something.example.com';
  556. $request = new CakeRequest('some/path');
  557. $this->assertEquals('example.com', $request->domain());
  558. $_SERVER['HTTP_HOST'] = 'something.example.co.uk';
  559. $this->assertEquals('example.co.uk', $request->domain(2));
  560. }
  561. /**
  562. * test getting subdomains for a host.
  563. *
  564. * @return void
  565. */
  566. public function testSubdomain() {
  567. $_SERVER['HTTP_HOST'] = 'something.example.com';
  568. $request = new CakeRequest('some/path');
  569. $this->assertEquals(array('something'), $request->subdomains());
  570. $_SERVER['HTTP_HOST'] = 'www.something.example.com';
  571. $this->assertEquals(array('www', 'something'), $request->subdomains());
  572. $_SERVER['HTTP_HOST'] = 'www.something.example.co.uk';
  573. $this->assertEquals(array('www', 'something'), $request->subdomains(2));
  574. $_SERVER['HTTP_HOST'] = 'example.co.uk';
  575. $this->assertEquals(array(), $request->subdomains(2));
  576. }
  577. /**
  578. * test ajax, flash and friends
  579. *
  580. * @return void
  581. */
  582. public function testisAjaxFlashAndFriends() {
  583. $request = new CakeRequest('some/path');
  584. $_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
  585. $this->assertTrue($request->is('flash'));
  586. $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash';
  587. $this->assertTrue($request->is('flash'));
  588. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  589. $this->assertTrue($request->is('ajax'));
  590. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHTTPREQUEST';
  591. $this->assertFalse($request->is('ajax'));
  592. $this->assertFalse($request->isAjax());
  593. $_SERVER['HTTP_USER_AGENT'] = 'Android 2.0';
  594. $this->assertTrue($request->is('mobile'));
  595. $this->assertTrue($request->isMobile());
  596. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 5.1; rv:2.0b6pre) Gecko/20100902 Firefox/4.0b6pre Fennec/2.0b1pre';
  597. $this->assertTrue($request->is('mobile'));
  598. $this->assertTrue($request->isMobile());
  599. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; SAMSUNG; OMNIA7)';
  600. $this->assertTrue($request->is('mobile'));
  601. $this->assertTrue($request->isMobile());
  602. }
  603. /**
  604. * test __call expcetions
  605. *
  606. * @expectedException CakeException
  607. * @return void
  608. */
  609. public function test__callExceptionOnUnknownMethod() {
  610. $request = new CakeRequest('some/path');
  611. $request->IamABanana();
  612. }
  613. /**
  614. * test is(ssl)
  615. *
  616. * @return void
  617. */
  618. public function testIsSsl() {
  619. $request = new CakeRequest('some/path');
  620. $_SERVER['HTTPS'] = 1;
  621. $this->assertTrue($request->is('ssl'));
  622. $_SERVER['HTTPS'] = 'on';
  623. $this->assertTrue($request->is('ssl'));
  624. $_SERVER['HTTPS'] = '1';
  625. $this->assertTrue($request->is('ssl'));
  626. $_SERVER['HTTPS'] = 'I am not empty';
  627. $this->assertTrue($request->is('ssl'));
  628. $_SERVER['HTTPS'] = 1;
  629. $this->assertTrue($request->is('ssl'));
  630. $_SERVER['HTTPS'] = 'off';
  631. $this->assertFalse($request->is('ssl'));
  632. $_SERVER['HTTPS'] = false;
  633. $this->assertFalse($request->is('ssl'));
  634. $_SERVER['HTTPS'] = '';
  635. $this->assertFalse($request->is('ssl'));
  636. }
  637. /**
  638. * test getting request params with object properties.
  639. *
  640. * @return void
  641. */
  642. public function test__get() {
  643. $request = new CakeRequest('some/path');
  644. $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
  645. $this->assertEquals($request->controller, 'posts');
  646. $this->assertEquals($request->action, 'view');
  647. $this->assertEquals($request->plugin, 'blogs');
  648. $this->assertSame($request->banana, null);
  649. }
  650. /**
  651. * Test isset()/empty() with overloaded properties.
  652. *
  653. * @return void
  654. */
  655. public function test__isset() {
  656. $request = new CakeRequest('some/path');
  657. $request->params = array(
  658. 'controller' => 'posts',
  659. 'action' => 'view',
  660. 'plugin' => 'blogs',
  661. 'named' => array()
  662. );
  663. $this->assertTrue(isset($request->controller));
  664. $this->assertFalse(isset($request->notthere));
  665. $this->assertFalse(empty($request->controller));
  666. $this->assertTrue(empty($request->named));
  667. }
  668. /**
  669. * test the array access implementation
  670. *
  671. * @return void
  672. */
  673. public function testArrayAccess() {
  674. $request = new CakeRequest('some/path');
  675. $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
  676. $this->assertEquals($request['controller'], 'posts');
  677. $request['slug'] = 'speedy-slug';
  678. $this->assertEquals($request->slug, 'speedy-slug');
  679. $this->assertEquals($request['slug'], 'speedy-slug');
  680. $this->assertTrue(isset($request['action']));
  681. $this->assertFalse(isset($request['wrong-param']));
  682. $this->assertTrue(isset($request['plugin']));
  683. unset($request['plugin']);
  684. $this->assertFalse(isset($request['plugin']));
  685. $this->assertNull($request['plugin']);
  686. $this->assertNull($request->plugin);
  687. $request = new CakeRequest('some/path?one=something&two=else');
  688. $this->assertTrue(isset($request['url']['one']));
  689. $request->data = array('Post' => array('title' => 'something'));
  690. $this->assertEquals($request['data']['Post']['title'], 'something');
  691. }
  692. /**
  693. * test adding detectors and having them work.
  694. *
  695. * @return void
  696. */
  697. public function testAddDetector() {
  698. $request = new CakeRequest('some/path');
  699. $request->addDetector('compare', array('env' => 'TEST_VAR', 'value' => 'something'));
  700. $_SERVER['TEST_VAR'] = 'something';
  701. $this->assertTrue($request->is('compare'), 'Value match failed.');
  702. $_SERVER['TEST_VAR'] = 'wrong';
  703. $this->assertFalse($request->is('compare'), 'Value mis-match failed.');
  704. $request->addDetector('compareCamelCase', array('env' => 'TEST_VAR', 'value' => 'foo'));
  705. $_SERVER['TEST_VAR'] = 'foo';
  706. $this->assertTrue($request->is('compareCamelCase'), 'Value match failed.');
  707. $request->addDetector('banana', array('env' => 'TEST_VAR', 'pattern' => '/^ban.*$/'));
  708. $_SERVER['TEST_VAR'] = 'banana';
  709. $this->assertTrue($request->isBanana());
  710. $_SERVER['TEST_VAR'] = 'wrong value';
  711. $this->assertFalse($request->isBanana());
  712. $request->addDetector('mobile', array('options' => array('Imagination')));
  713. $_SERVER['HTTP_USER_AGENT'] = 'Imagination land';
  714. $this->assertTrue($request->isMobile());
  715. $_SERVER['HTTP_USER_AGENT'] = 'iPhone 3.0';
  716. $this->assertTrue($request->isMobile());
  717. $request->addDetector('callme', array('env' => 'TEST_VAR', 'callback' => array($this, 'detectCallback')));
  718. $request->addDetector('index', array('param' => 'action', 'value' => 'index'));
  719. $request->params['action'] = 'index';
  720. $this->assertTrue($request->isIndex());
  721. $request->params['action'] = 'add';
  722. $this->assertFalse($request->isIndex());
  723. $request->return = true;
  724. $this->assertTrue($request->isCallMe());
  725. $request->return = false;
  726. $this->assertFalse($request->isCallMe());
  727. }
  728. /**
  729. * helper function for testing callbacks.
  730. *
  731. * @return void
  732. */
  733. public function detectCallback($request) {
  734. return $request->return == true;
  735. }
  736. /**
  737. * test getting headers
  738. *
  739. * @return void
  740. */
  741. public function testHeader() {
  742. $_SERVER['HTTP_HOST'] = 'localhost';
  743. $_SERVER['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';
  744. $request = new CakeRequest('/', false);
  745. $this->assertEquals($_SERVER['HTTP_HOST'], $request->header('host'));
  746. $this->assertEquals($_SERVER['HTTP_USER_AGENT'], $request->header('User-Agent'));
  747. }
  748. /**
  749. * test accepts() with and without parameters
  750. *
  751. * @return void
  752. */
  753. public function testAccepts() {
  754. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml;q=0.9,application/xhtml+xml,text/html,text/plain,image/png';
  755. $request = new CakeRequest('/', false);
  756. $result = $request->accepts();
  757. $expected = array(
  758. 'text/xml', 'application/xhtml+xml', 'text/html', 'text/plain', 'image/png', 'application/xml'
  759. );
  760. $this->assertEquals($expected, $result, 'Content types differ.');
  761. $result = $request->accepts('text/html');
  762. $this->assertTrue($result);
  763. $result = $request->accepts('image/gif');
  764. $this->assertFalse($result);
  765. }
  766. /**
  767. * Test that accept header types are trimmed for comparisons.
  768. *
  769. * @return void
  770. */
  771. public function testAcceptWithWhitespace() {
  772. $_SERVER['HTTP_ACCEPT'] = 'text/xml , text/html , text/plain,image/png';
  773. $request = new CakeRequest('/', false);
  774. $result = $request->accepts();
  775. $expected = array(
  776. 'text/xml', 'text/html', 'text/plain', 'image/png'
  777. );
  778. $this->assertEquals($expected, $result, 'Content types differ.');
  779. $this->assertTrue($request->accepts('text/html'));
  780. }
  781. /**
  782. * Content types from accepts() should respect the client's q preference values.
  783. *
  784. * @return void
  785. */
  786. public function testAcceptWithQvalueSorting() {
  787. $_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0';
  788. $request = new CakeRequest('/', false);
  789. $result = $request->accepts();
  790. $expected = array('application/xml', 'text/html', 'application/json');
  791. $this->assertEquals($expected, $result);
  792. }
  793. /**
  794. * Test the raw parsing of accept headers into the q value formatting.
  795. *
  796. * @return void
  797. */
  798. public function testParseAcceptWithQValue() {
  799. $_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0,image/png';
  800. $request = new CakeRequest('/', false);
  801. $result = $request->parseAccept();
  802. $expected = array(
  803. '1.0' => array('application/xml', 'image/png'),
  804. '0.8' => array('text/html'),
  805. '0.7' => array('application/json'),
  806. );
  807. $this->assertEquals($expected, $result);
  808. }
  809. /**
  810. * testBaseUrlAndWebrootWithModRewrite method
  811. *
  812. * @return void
  813. */
  814. public function testBaseUrlAndWebrootWithModRewrite() {
  815. Configure::write('App.baseUrl', false);
  816. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  817. $_SERVER['PHP_SELF'] = '/1.2.x.x/app/webroot/index.php';
  818. $_SERVER['PATH_INFO'] = '/posts/view/1';
  819. $request = new CakeRequest();
  820. $this->assertEquals($request->base, '/1.2.x.x');
  821. $this->assertEquals($request->webroot, '/1.2.x.x/');
  822. $this->assertEquals($request->url, 'posts/view/1');
  823. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/app/webroot';
  824. $_SERVER['PHP_SELF'] = '/index.php';
  825. $_SERVER['PATH_INFO'] = '/posts/add';
  826. $request = new CakeRequest();
  827. $this->assertEquals($request->base, '');
  828. $this->assertEquals($request->webroot, '/');
  829. $this->assertEquals($request->url, 'posts/add');
  830. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/test/';
  831. $_SERVER['PHP_SELF'] = '/webroot/index.php';
  832. $request = new CakeRequest();
  833. $this->assertEquals('', $request->base);
  834. $this->assertEquals('/', $request->webroot);
  835. $_SERVER['DOCUMENT_ROOT'] = '/some/apps/where';
  836. $_SERVER['PHP_SELF'] = '/app/webroot/index.php';
  837. $request = new CakeRequest();
  838. $this->assertEquals($request->base, '');
  839. $this->assertEquals($request->webroot, '/');
  840. Configure::write('App.dir', 'auth');
  841. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  842. $_SERVER['PHP_SELF'] = '/demos/auth/webroot/index.php';
  843. $request = new CakeRequest();
  844. $this->assertEquals($request->base, '/demos/auth');
  845. $this->assertEquals($request->webroot, '/demos/auth/');
  846. Configure::write('App.dir', 'code');
  847. $_SERVER['DOCUMENT_ROOT'] = '/Library/WebServer/Documents';
  848. $_SERVER['PHP_SELF'] = '/clients/PewterReport/code/webroot/index.php';
  849. $request = new CakeRequest();
  850. $this->assertEquals($request->base, '/clients/PewterReport/code');
  851. $this->assertEquals($request->webroot, '/clients/PewterReport/code/');
  852. }
  853. /**
  854. * testBaseUrlwithModRewriteAlias method
  855. *
  856. * @return void
  857. */
  858. public function testBaseUrlwithModRewriteAlias() {
  859. $_SERVER['DOCUMENT_ROOT'] = '/home/aplusnur/public_html';
  860. $_SERVER['PHP_SELF'] = '/control/index.php';
  861. Configure::write('App.base', '/control');
  862. $request = new CakeRequest();
  863. $this->assertEquals($request->base, '/control');
  864. $this->assertEquals($request->webroot, '/control/');
  865. Configure::write('App.base', false);
  866. Configure::write('App.dir', 'affiliate');
  867. Configure::write('App.webroot', 'newaffiliate');
  868. $_SERVER['DOCUMENT_ROOT'] = '/var/www/abtravaff/html';
  869. $_SERVER['PHP_SELF'] = '/newaffiliate/index.php';
  870. $request = new CakeRequest();
  871. $this->assertEquals($request->base, '/newaffiliate');
  872. $this->assertEquals($request->webroot, '/newaffiliate/');
  873. }
  874. /**
  875. * test base, webroot, and url parsing when there is no url rewriting
  876. *
  877. * @return void
  878. */
  879. public function testBaseUrlWithNoModRewrite() {
  880. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites';
  881. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake/index.php';
  882. $_SERVER['PHP_SELF'] = '/cake/index.php/posts/index';
  883. $_SERVER['REQUEST_URI'] = '/cake/index.php/posts/index';
  884. Configure::write('App', array(
  885. 'dir' => APP_DIR,
  886. 'webroot' => WEBROOT_DIR,
  887. 'base' => false,
  888. 'baseUrl' => '/cake/index.php'
  889. ));
  890. $request = new CakeRequest();
  891. $this->assertEquals($request->base, '/cake/index.php');
  892. $this->assertEquals($request->webroot, '/cake/app/webroot/');
  893. $this->assertEquals($request->url, 'posts/index');
  894. }
  895. /**
  896. * testBaseUrlAndWebrootWithBaseUrl method
  897. *
  898. * @return void
  899. */
  900. public function testBaseUrlAndWebrootWithBaseUrl() {
  901. Configure::write('App.dir', 'app');
  902. Configure::write('App.baseUrl', '/app/webroot/index.php');
  903. $request = new CakeRequest();
  904. $this->assertEquals($request->base, '/app/webroot/index.php');
  905. $this->assertEquals($request->webroot, '/app/webroot/');
  906. Configure::write('App.baseUrl', '/app/webroot/test.php');
  907. $request = new CakeRequest();
  908. $this->assertEquals($request->base, '/app/webroot/test.php');
  909. $this->assertEquals($request->webroot, '/app/webroot/');
  910. Configure::write('App.baseUrl', '/app/index.php');
  911. $request = new CakeRequest();
  912. $this->assertEquals($request->base, '/app/index.php');
  913. $this->assertEquals($request->webroot, '/app/webroot/');
  914. Configure::write('App.baseUrl', '/CakeBB/app/webroot/index.php');
  915. $request = new CakeRequest();
  916. $this->assertEquals($request->base, '/CakeBB/app/webroot/index.php');
  917. $this->assertEquals($request->webroot, '/CakeBB/app/webroot/');
  918. Configure::write('App.baseUrl', '/CakeBB/app/index.php');
  919. $request = new CakeRequest();
  920. $this->assertEquals($request->base, '/CakeBB/app/index.php');
  921. $this->assertEquals($request->webroot, '/CakeBB/app/webroot/');
  922. Configure::write('App.baseUrl', '/CakeBB/index.php');
  923. $request = new CakeRequest();
  924. $this->assertEquals($request->base, '/CakeBB/index.php');
  925. $this->assertEquals($request->webroot, '/CakeBB/app/webroot/');
  926. Configure::write('App.baseUrl', '/dbhauser/index.php');
  927. $_SERVER['DOCUMENT_ROOT'] = '/kunden/homepages/4/d181710652/htdocs/joomla';
  928. $_SERVER['SCRIPT_FILENAME'] = '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php';
  929. $request = new CakeRequest();
  930. $this->assertEquals($request->base, '/dbhauser/index.php');
  931. $this->assertEquals($request->webroot, '/dbhauser/app/webroot/');
  932. }
  933. /**
  934. * test baseUrl with no rewrite and using the top level index.php.
  935. *
  936. * @return void
  937. */
  938. public function testBaseUrlNoRewriteTopLevelIndex() {
  939. Configure::write('App.baseUrl', '/index.php');
  940. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev';
  941. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/index.php';
  942. $request = new CakeRequest();
  943. $this->assertEquals('/index.php', $request->base);
  944. $this->assertEquals('/app/webroot/', $request->webroot);
  945. }
  946. /**
  947. * Check that a sub-directory containing app|webroot doesn't get mishandled when re-writing is off.
  948. *
  949. * @return void
  950. */
  951. public function testBaseUrlWithAppAndWebrootInDirname() {
  952. Configure::write('App.baseUrl', '/approval/index.php');
  953. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
  954. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/approval/index.php';
  955. $request = new CakeRequest();
  956. $this->assertEquals('/approval/index.php', $request->base);
  957. $this->assertEquals('/approval/app/webroot/', $request->webroot);
  958. Configure::write('App.baseUrl', '/webrootable/index.php');
  959. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
  960. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/webrootable/index.php';
  961. $request = new CakeRequest();
  962. $this->assertEquals('/webrootable/index.php', $request->base);
  963. $this->assertEquals('/webrootable/app/webroot/', $request->webroot);
  964. }
  965. /**
  966. * test baseUrl with no rewrite, and using the app/webroot/index.php file as is normal with virtual hosts.
  967. *
  968. * @return void
  969. */
  970. public function testBaseUrlNoRewriteWebrootIndex() {
  971. Configure::write('App.baseUrl', '/index.php');
  972. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev/app/webroot';
  973. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/app/webroot/index.php';
  974. $request = new CakeRequest();
  975. $this->assertEquals('/index.php', $request->base);
  976. $this->assertEquals('/', $request->webroot);
  977. }
  978. /**
  979. * Test that a request with a . in the main GET parameter is filtered out.
  980. * PHP changes GET parameter keys containing dots to _.
  981. *
  982. * @return void
  983. */
  984. public function testGetParamsWithDot() {
  985. $_GET = array();
  986. $_GET['/posts/index/add_add'] = '';
  987. $_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
  988. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/index/add.add';
  989. $request = new CakeRequest();
  990. $this->assertEquals(array(), $request->query);
  991. }
  992. /**
  993. * Test that a request with urlencoded bits in the main GET parameter are filtered out.
  994. *
  995. * @return void
  996. */
  997. public function testGetParamWithUrlencodedElement() {
  998. $_GET = array();
  999. $_GET['/posts/add/∂∂'] = '';
  1000. $_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
  1001. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/add/%E2%88%82%E2%88%82';
  1002. $request = new CakeRequest();
  1003. $this->assertEquals(array(), $request->query);
  1004. }
  1005. /**
  1006. * generator for environment configurations
  1007. *
  1008. * @return void
  1009. */
  1010. public static function environmentGenerator() {
  1011. return array(
  1012. array(
  1013. 'IIS - No rewrite base path',
  1014. array(
  1015. 'App' => array(
  1016. 'base' => false,
  1017. 'baseUrl' => '/index.php',
  1018. 'dir' => 'app',
  1019. 'webroot' => 'webroot'
  1020. ),
  1021. 'SERVER' => array(
  1022. 'SCRIPT_NAME' => '/index.php',
  1023. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1024. 'QUERY_STRING' => '',
  1025. 'REQUEST_URI' => '/index.php',
  1026. 'URL' => '/index.php',
  1027. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php',
  1028. 'ORIG_PATH_INFO' => '/index.php',
  1029. 'PATH_INFO' => '',
  1030. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php',
  1031. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1032. 'PHP_SELF' => '/index.php',
  1033. ),
  1034. ),
  1035. array(
  1036. 'base' => '/index.php',
  1037. 'webroot' => '/app/webroot/',
  1038. 'url' => ''
  1039. ),
  1040. ),
  1041. array(
  1042. 'IIS - No rewrite with path, no PHP_SELF',
  1043. array(
  1044. 'App' => array(
  1045. 'base' => false,
  1046. 'baseUrl' => '/index.php?',
  1047. 'dir' => 'app',
  1048. 'webroot' => 'webroot'
  1049. ),
  1050. 'SERVER' => array(
  1051. 'QUERY_STRING' => '/posts/add',
  1052. 'REQUEST_URI' => '/index.php?/posts/add',
  1053. 'PHP_SELF' => '',
  1054. 'URL' => '/index.php?/posts/add',
  1055. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1056. 'argv' => array('/posts/add'),
  1057. 'argc' => 1
  1058. ),
  1059. ),
  1060. array(
  1061. 'url' => 'posts/add',
  1062. 'base' => '/index.php?',
  1063. 'webroot' => '/app/webroot/'
  1064. )
  1065. ),
  1066. array(
  1067. 'IIS - No rewrite sub dir 2',
  1068. array(
  1069. 'App' => array(
  1070. 'base' => false,
  1071. 'baseUrl' => '/site/index.php',
  1072. 'dir' => 'app',
  1073. 'webroot' => 'webroot',
  1074. ),
  1075. 'SERVER' => array(
  1076. 'SCRIPT_NAME' => '/site/index.php',
  1077. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1078. 'QUERY_STRING' => '',
  1079. 'REQUEST_URI' => '/site/index.php',
  1080. 'URL' => '/site/index.php',
  1081. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1082. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1083. 'PHP_SELF' => '/site/index.php',
  1084. 'argv' => array(),
  1085. 'argc' => 0
  1086. ),
  1087. ),
  1088. array(
  1089. 'url' => '',
  1090. 'base' => '/site/index.php',
  1091. 'webroot' => '/site/app/webroot/'
  1092. ),
  1093. ),
  1094. array(
  1095. 'IIS - No rewrite sub dir 2 with path',
  1096. array(
  1097. 'App' => array(
  1098. 'base' => false,
  1099. 'baseUrl' => '/site/index.php',
  1100. 'dir' => 'app',
  1101. 'webroot' => 'webroot'
  1102. ),
  1103. 'GET' => array('/posts/add' => ''),
  1104. 'SERVER' => array(
  1105. 'SCRIPT_NAME' => '/site/index.php',
  1106. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1107. 'QUERY_STRING' => '/posts/add',
  1108. 'REQUEST_URI' => '/site/index.php/posts/add',
  1109. 'URL' => '/site/index.php/posts/add',
  1110. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1111. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1112. 'PHP_SELF' => '/site/index.php/posts/add',
  1113. 'argv' => array('/posts/add'),
  1114. 'argc' => 1
  1115. ),
  1116. ),
  1117. array(
  1118. 'url' => 'posts/add',
  1119. 'base' => '/site/index.php',
  1120. 'webroot' => '/site/app/webroot/'
  1121. )
  1122. ),
  1123. array(
  1124. 'Apache - No rewrite, document root set to webroot, requesting path',
  1125. array(
  1126. 'App' => array(
  1127. 'base' => false,
  1128. 'baseUrl' => '/index.php',
  1129. 'dir' => 'app',
  1130. 'webroot' => 'webroot'
  1131. ),
  1132. 'SERVER' => array(
  1133. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1134. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1135. 'QUERY_STRING' => '',
  1136. 'REQUEST_URI' => '/index.php/posts/index',
  1137. 'SCRIPT_NAME' => '/index.php',
  1138. 'PATH_INFO' => '/posts/index',
  1139. 'PHP_SELF' => '/index.php/posts/index',
  1140. ),
  1141. ),
  1142. array(
  1143. 'url' => 'posts/index',
  1144. 'base' => '/index.php',
  1145. 'webroot' => '/'
  1146. ),
  1147. ),
  1148. array(
  1149. 'Apache - No rewrite, document root set to webroot, requesting root',
  1150. array(
  1151. 'App' => array(
  1152. 'base' => false,
  1153. 'baseUrl' => '/index.php',
  1154. 'dir' => 'app',
  1155. 'webroot' => 'webroot'
  1156. ),
  1157. 'SERVER' => array(
  1158. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1159. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1160. 'QUERY_STRING' => '',
  1161. 'REQUEST_URI' => '/index.php',
  1162. 'SCRIPT_NAME' => '/index.php',
  1163. 'PATH_INFO' => '',
  1164. 'PHP_SELF' => '/index.php',
  1165. ),
  1166. ),
  1167. array(
  1168. 'url' => '',
  1169. 'base' => '/index.php',
  1170. 'webroot' => '/'
  1171. ),
  1172. ),
  1173. array(
  1174. 'Apache - No rewrite, document root set above top level cake dir, requesting path',
  1175. array(
  1176. 'App' => array(
  1177. 'base' => false,
  1178. 'baseUrl' => '/site/index.php',
  1179. 'dir' => 'app',
  1180. 'webroot' => 'webroot'
  1181. ),
  1182. 'SERVER' => array(
  1183. 'SERVER_NAME' => 'localhost',
  1184. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1185. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1186. 'REQUEST_URI' => '/site/index.php/posts/index',
  1187. 'SCRIPT_NAME' => '/site/index.php',
  1188. 'PATH_INFO' => '/posts/index',
  1189. 'PHP_SELF' => '/site/index.php/posts/index',
  1190. ),
  1191. ),
  1192. array(
  1193. 'url' => 'posts/index',
  1194. 'base' => '/site/index.php',
  1195. 'webroot' => '/site/app/webroot/',
  1196. ),
  1197. ),
  1198. array(
  1199. 'Apache - No rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1200. array(
  1201. 'App' => array(
  1202. 'base' => false,
  1203. 'baseUrl' => '/site/index.php',
  1204. 'dir' => 'app',
  1205. 'webroot' => 'webroot'
  1206. ),
  1207. 'SERVER' => array(
  1208. 'SERVER_NAME' => 'localhost',
  1209. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1210. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1211. 'REQUEST_URI' => '/site/index.php/',
  1212. 'SCRIPT_NAME' => '/site/index.php',
  1213. 'PHP_SELF' => '/site/index.php/',
  1214. ),
  1215. ),
  1216. array(
  1217. 'url' => '',
  1218. 'base' => '/site/index.php',
  1219. 'webroot' => '/site/app/webroot/',
  1220. ),
  1221. ),
  1222. array(
  1223. 'Apache - No rewrite, document root set above top level cake dir, request path, with GET',
  1224. array(
  1225. 'App' => array(
  1226. 'base' => false,
  1227. 'baseUrl' => '/site/index.php',
  1228. 'dir' => 'app',
  1229. 'webroot' => 'webroot'
  1230. ),
  1231. 'GET' => array('a' => 'b', 'c' => 'd'),
  1232. 'SERVER' => array(
  1233. 'SERVER_NAME' => 'localhost',
  1234. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1235. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1236. 'REQUEST_URI' => '/site/index.php/posts/index?a=b&c=d',
  1237. 'SCRIPT_NAME' => '/site/index.php',
  1238. 'PATH_INFO' => '/posts/index',
  1239. 'PHP_SELF' => '/site/index.php/posts/index',
  1240. 'QUERY_STRING' => 'a=b&c=d'
  1241. ),
  1242. ),
  1243. array(
  1244. 'urlParams' => array('a' => 'b', 'c' => 'd'),
  1245. 'url' => 'posts/index',
  1246. 'base' => '/site/index.php',
  1247. 'webroot' => '/site/app/webroot/',
  1248. ),
  1249. ),
  1250. array(
  1251. 'Apache - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1252. array(
  1253. 'App' => array(
  1254. 'base' => false,
  1255. 'baseUrl' => false,
  1256. 'dir' => 'app',
  1257. 'webroot' => 'webroot'
  1258. ),
  1259. 'SERVER' => array(
  1260. 'SERVER_NAME' => 'localhost',
  1261. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1262. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1263. 'REQUEST_URI' => '/site/',
  1264. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1265. 'PHP_SELF' => '/site/app/webroot/index.php',
  1266. ),
  1267. ),
  1268. array(
  1269. 'url' => '',
  1270. 'base' => '/site',
  1271. 'webroot' => '/site/',
  1272. ),
  1273. ),
  1274. array(
  1275. 'Apache - w/rewrite, document root above top level cake dir, request root, no PATH_INFO/REQUEST_URI',
  1276. array(
  1277. 'App' => array(
  1278. 'base' => false,
  1279. 'baseUrl' => false,
  1280. 'dir' => 'app',
  1281. 'webroot' => 'webroot'
  1282. ),
  1283. 'SERVER' => array(
  1284. 'SERVER_NAME' => 'localhost',
  1285. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1286. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1287. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1288. 'PHP_SELF' => '/site/app/webroot/index.php',
  1289. 'PATH_INFO' => null,
  1290. 'REQUEST_URI' => null,
  1291. ),
  1292. ),
  1293. array(
  1294. 'url' => '',
  1295. 'base' => '/site',
  1296. 'webroot' => '/site/',
  1297. ),
  1298. ),
  1299. array(
  1300. 'Apache - w/rewrite, document root set to webroot, request root, no PATH_INFO/REQUEST_URI',
  1301. array(
  1302. 'App' => array(
  1303. 'base' => false,
  1304. 'baseUrl' => false,
  1305. 'dir' => 'app',
  1306. 'webroot' => 'webroot'
  1307. ),
  1308. 'SERVER' => array(
  1309. 'SERVER_NAME' => 'localhost',
  1310. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1311. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1312. 'SCRIPT_NAME' => '/index.php',
  1313. 'PHP_SELF' => '/index.php',
  1314. 'PATH_INFO' => null,
  1315. 'REQUEST_URI' => null,
  1316. ),
  1317. ),
  1318. array(
  1319. 'url' => '',
  1320. 'base' => '',
  1321. 'webroot' => '/',
  1322. ),
  1323. ),
  1324. array(
  1325. 'Nginx - w/rewrite, document root set to webroot, request root, no PATH_INFO',
  1326. array(
  1327. 'App' => array(
  1328. 'base' => false,
  1329. 'baseUrl' => false,
  1330. 'dir' => 'app',
  1331. 'webroot' => 'webroot'
  1332. ),
  1333. 'GET' => array('/posts/add' => ''),
  1334. 'SERVER' => array(
  1335. 'SERVER_NAME' => 'localhost',
  1336. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1337. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1338. 'SCRIPT_NAME' => '/index.php',
  1339. 'QUERY_STRING' => '/posts/add&',
  1340. 'PHP_SELF' => '/index.php',
  1341. 'PATH_INFO' => null,
  1342. 'REQUEST_URI' => '/posts/add',
  1343. ),
  1344. ),
  1345. array(
  1346. 'url' => 'posts/add',
  1347. 'base' => '',
  1348. 'webroot' => '/',
  1349. 'urlParams' => array()
  1350. ),
  1351. ),
  1352. );
  1353. }
  1354. /**
  1355. * testEnvironmentDetection method
  1356. *
  1357. * @dataProvider environmentGenerator
  1358. * @return void
  1359. */
  1360. public function testEnvironmentDetection($name, $env, $expected) {
  1361. $_GET = array();
  1362. $this->__loadEnvironment($env);
  1363. $request = new CakeRequest();
  1364. $this->assertEquals($expected['url'], $request->url, "url error");
  1365. $this->assertEquals($expected['base'], $request->base, "base error");
  1366. $this->assertEquals($expected['webroot'], $request->webroot, "webroot error");
  1367. if (isset($expected['urlParams'])) {
  1368. $this->assertEquals($request->query, $expected['urlParams'], "GET param mismatch");
  1369. }
  1370. }
  1371. /**
  1372. * test the data() method reading
  1373. *
  1374. * @return void
  1375. */
  1376. public function testDataReading() {
  1377. $_POST['data'] = array(
  1378. 'Model' => array(
  1379. 'field' => 'value'
  1380. )
  1381. );
  1382. $request = new CakeRequest('posts/index');
  1383. $result = $request->data('Model');
  1384. $this->assertEquals($_POST['data']['Model'], $result);
  1385. $result = $request->data('Model.imaginary');
  1386. $this->assertNull($result);
  1387. }
  1388. /**
  1389. * test writing with data()
  1390. *
  1391. * @return void
  1392. */
  1393. public function testDataWriting() {
  1394. $_POST['data'] = array(
  1395. 'Model' => array(
  1396. 'field' => 'value'
  1397. )
  1398. );
  1399. $request = new CakeRequest('posts/index');
  1400. $result = $request->data('Model.new_value', 'new value');
  1401. $this->assertSame($result, $request, 'Return was not $this');
  1402. $this->assertEquals($request->data['Model']['new_value'], 'new value');
  1403. $request->data('Post.title', 'New post')->data('Comment.1.author', 'Mark');
  1404. $this->assertEquals($request->data['Post']['title'], 'New post');
  1405. $this->assertEquals($request->data['Comment']['1']['author'], 'Mark');
  1406. }
  1407. /**
  1408. * test writing falsey values.
  1409. *
  1410. * @return void
  1411. */
  1412. public function testDataWritingFalsey() {
  1413. $request = new CakeRequest('posts/index');
  1414. $request->data('Post.null', null);
  1415. $this->assertNull($request->data['Post']['null']);
  1416. $request->data('Post.false', false);
  1417. $this->assertFalse($request->data['Post']['false']);
  1418. $request->data('Post.zero', 0);
  1419. $this->assertSame(0, $request->data['Post']['zero']);
  1420. $request->data('Post.empty', '');
  1421. $this->assertSame('', $request->data['Post']['empty']);
  1422. }
  1423. /**
  1424. * test accept language
  1425. *
  1426. * @return void
  1427. */
  1428. public function testAcceptLanguage() {
  1429. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
  1430. $result = CakeRequest::acceptLanguage();
  1431. $this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
  1432. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx;en_ca';
  1433. $result = CakeRequest::acceptLanguage();
  1434. $this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
  1435. $result = CakeRequest::acceptLanguage('en-ca');
  1436. $this->assertTrue($result);
  1437. $result = CakeRequest::acceptLanguage('en-us');
  1438. $this->assertFalse($result);
  1439. }
  1440. /**
  1441. * test the here() method
  1442. *
  1443. * @return void
  1444. */
  1445. public function testHere() {
  1446. Configure::write('App.base', '/base_path');
  1447. $_GET = array('test' => 'value');
  1448. $request = new CakeRequest('/posts/add/1/name:value');
  1449. $result = $request->here();
  1450. $this->assertEquals('/base_path/posts/add/1/name:value?test=value', $result);
  1451. $result = $request->here(false);
  1452. $this->assertEquals('/posts/add/1/name:value?test=value', $result);
  1453. $request = new CakeRequest('/posts/base_path/1/name:value');
  1454. $result = $request->here();
  1455. $this->assertEquals('/base_path/posts/base_path/1/name:value?test=value', $result);
  1456. $result = $request->here(false);
  1457. $this->assertEquals('/posts/base_path/1/name:value?test=value', $result);
  1458. }
  1459. /**
  1460. * Test the input() method.
  1461. *
  1462. * @return void
  1463. */
  1464. public function testInput() {
  1465. $request = $this->getMock('CakeRequest', array('_readInput'));
  1466. $request->expects($this->once())->method('_readInput')
  1467. ->will($this->returnValue('I came from stdin'));
  1468. $result = $request->input();
  1469. $this->assertEquals('I came from stdin', $result);
  1470. }
  1471. /**
  1472. * Test input() decoding.
  1473. *
  1474. * @return void
  1475. */
  1476. public function testInputDecode() {
  1477. $request = $this->getMock('CakeRequest', array('_readInput'));
  1478. $request->expects($this->once())->method('_readInput')
  1479. ->will($this->returnValue('{"name":"value"}'));
  1480. $result = $request->input('json_decode');
  1481. $this->assertEquals(array('name' => 'value'), (array)$result);
  1482. }
  1483. /**
  1484. * Test input() decoding with additional arguments.
  1485. *
  1486. * @return void
  1487. */
  1488. public function testInputDecodeExtraParams() {
  1489. $xml = <<<XML
  1490. <?xml version="1.0" encoding="utf-8"?>
  1491. <post>
  1492. <title id="title">Test</title>
  1493. </post>
  1494. XML;
  1495. $request = $this->getMock('CakeRequest', array('_readInput'));
  1496. $request->expects($this->once())->method('_readInput')
  1497. ->will($this->returnValue($xml));
  1498. $result = $request->input('Xml::build', array('return' => 'domdocument'));
  1499. $this->assertInstanceOf('DOMDocument', $result);
  1500. $this->assertEquals(
  1501. 'Test',
  1502. $result->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText
  1503. );
  1504. }
  1505. /**
  1506. * Test is('requested') and isRequested()
  1507. *
  1508. * @return void
  1509. */
  1510. public function testIsRequested() {
  1511. $request = new CakeRequest('/posts/index');
  1512. $request->addParams(array(
  1513. 'controller' => 'posts',
  1514. 'action' => 'index',
  1515. 'plugin' => null,
  1516. 'requested' => 1
  1517. ));
  1518. $this->assertTrue($request->is('requested'));
  1519. $this->assertTrue($request->isRequested());
  1520. $request = new CakeRequest('/posts/index');
  1521. $request->addParams(array(
  1522. 'controller' => 'posts',
  1523. 'action' => 'index',
  1524. 'plugin' => null,
  1525. ));
  1526. $this->assertFalse($request->is('requested'));
  1527. $this->assertFalse($request->isRequested());
  1528. }
  1529. /**
  1530. * loadEnvironment method
  1531. *
  1532. * @param mixed $env
  1533. * @return void
  1534. */
  1535. protected function __loadEnvironment($env) {
  1536. if (isset($env['App'])) {
  1537. Configure::write('App', $env['App']);
  1538. }
  1539. if (isset($env['GET'])) {
  1540. foreach ($env['GET'] as $key => $val) {
  1541. $_GET[$key] = $val;
  1542. }
  1543. }
  1544. if (isset($env['POST'])) {
  1545. foreach ($env['POST'] as $key => $val) {
  1546. $_POST[$key] = $val;
  1547. }
  1548. }
  1549. if (isset($env['SERVER'])) {
  1550. foreach ($env['SERVER'] as $key => $val) {
  1551. $_SERVER[$key] = $val;
  1552. }
  1553. }
  1554. }
  1555. }