CakeRequestTest.php 48 KB

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