CakeRequestTest.php 47 KB

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