CakeRequestTest.php 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757
  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('compareCamelCase', array('env' => 'TEST_VAR', 'value' => 'foo'));
  699. $_SERVER['TEST_VAR'] = 'foo';
  700. $this->assertTrue($request->is('compareCamelCase'), 'Value match failed.');
  701. $request->addDetector('banana', array('env' => 'TEST_VAR', 'pattern' => '/^ban.*$/'));
  702. $_SERVER['TEST_VAR'] = 'banana';
  703. $this->assertTrue($request->isBanana());
  704. $_SERVER['TEST_VAR'] = 'wrong value';
  705. $this->assertFalse($request->isBanana());
  706. $request->addDetector('mobile', array('options' => array('Imagination')));
  707. $_SERVER['HTTP_USER_AGENT'] = 'Imagination land';
  708. $this->assertTrue($request->isMobile());
  709. $_SERVER['HTTP_USER_AGENT'] = 'iPhone 3.0';
  710. $this->assertTrue($request->isMobile());
  711. $request->addDetector('callme', array('env' => 'TEST_VAR', 'callback' => array($this, 'detectCallback')));
  712. $request->addDetector('index', array('param' => 'action', 'value' => 'index'));
  713. $request->params['action'] = 'index';
  714. $this->assertTrue($request->isIndex());
  715. $request->params['action'] = 'add';
  716. $this->assertFalse($request->isIndex());
  717. $request->return = true;
  718. $this->assertTrue($request->isCallMe());
  719. $request->return = false;
  720. $this->assertFalse($request->isCallMe());
  721. }
  722. /**
  723. * helper function for testing callbacks.
  724. *
  725. * @return void
  726. */
  727. public function detectCallback($request) {
  728. return $request->return == true;
  729. }
  730. /**
  731. * test getting headers
  732. *
  733. * @return void
  734. */
  735. public function testHeader() {
  736. $_SERVER['HTTP_HOST'] = 'localhost';
  737. $_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';
  738. $request = new CakeRequest('/', false);
  739. $this->assertEquals($_SERVER['HTTP_HOST'], $request->header('host'));
  740. $this->assertEquals($_SERVER['HTTP_USER_AGENT'], $request->header('User-Agent'));
  741. }
  742. /**
  743. * test accepts() with and without parameters
  744. *
  745. * @return void
  746. */
  747. public function testAccepts() {
  748. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml;q=0.9,application/xhtml+xml,text/html,text/plain,image/png';
  749. $request = new CakeRequest('/', false);
  750. $result = $request->accepts();
  751. $expected = array(
  752. 'text/xml', 'application/xhtml+xml', 'text/html', 'text/plain', 'image/png', 'application/xml'
  753. );
  754. $this->assertEquals($expected, $result, 'Content types differ.');
  755. $result = $request->accepts('text/html');
  756. $this->assertTrue($result);
  757. $result = $request->accepts('image/gif');
  758. $this->assertFalse($result);
  759. }
  760. /**
  761. * Test that accept header types are trimmed for comparisons.
  762. *
  763. * @return void
  764. */
  765. public function testAcceptWithWhitespace() {
  766. $_SERVER['HTTP_ACCEPT'] = 'text/xml , text/html , text/plain,image/png';
  767. $request = new CakeRequest('/', false);
  768. $result = $request->accepts();
  769. $expected = array(
  770. 'text/xml', 'text/html', 'text/plain', 'image/png'
  771. );
  772. $this->assertEquals($expected, $result, 'Content types differ.');
  773. $this->assertTrue($request->accepts('text/html'));
  774. }
  775. /**
  776. * Content types from accepts() should respect the client's q preference values.
  777. *
  778. * @return void
  779. */
  780. public function testAcceptWithQvalueSorting() {
  781. $_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0';
  782. $request = new CakeRequest('/', false);
  783. $result = $request->accepts();
  784. $expected = array('application/xml', 'text/html', 'application/json');
  785. $this->assertEquals($expected, $result);
  786. }
  787. /**
  788. * Test the raw parsing of accept headers into the q value formatting.
  789. *
  790. * @return void
  791. */
  792. public function testParseAcceptWithQValue() {
  793. $_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0,image/png';
  794. $request = new CakeRequest('/', false);
  795. $result = $request->parseAccept();
  796. $expected = array(
  797. '1.0' => array('application/xml', 'image/png'),
  798. '0.8' => array('text/html'),
  799. '0.7' => array('application/json'),
  800. );
  801. $this->assertEquals($expected, $result);
  802. }
  803. /**
  804. * testBaseUrlAndWebrootWithModRewrite method
  805. *
  806. * @return void
  807. */
  808. public function testBaseUrlAndWebrootWithModRewrite() {
  809. Configure::write('App.baseUrl', false);
  810. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  811. $_SERVER['PHP_SELF'] = '/1.2.x.x/app/webroot/index.php';
  812. $_SERVER['PATH_INFO'] = '/posts/view/1';
  813. $request = new CakeRequest();
  814. $this->assertEquals($request->base, '/1.2.x.x');
  815. $this->assertEquals($request->webroot, '/1.2.x.x/');
  816. $this->assertEquals($request->url, 'posts/view/1');
  817. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/app/webroot';
  818. $_SERVER['PHP_SELF'] = '/index.php';
  819. $_SERVER['PATH_INFO'] = '/posts/add';
  820. $request = new CakeRequest();
  821. $this->assertEquals($request->base, '');
  822. $this->assertEquals($request->webroot, '/');
  823. $this->assertEquals($request->url, 'posts/add');
  824. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/test/';
  825. $_SERVER['PHP_SELF'] = '/webroot/index.php';
  826. $request = new CakeRequest();
  827. $this->assertEquals('', $request->base);
  828. $this->assertEquals('/', $request->webroot);
  829. $_SERVER['DOCUMENT_ROOT'] = '/some/apps/where';
  830. $_SERVER['PHP_SELF'] = '/app/webroot/index.php';
  831. $request = new CakeRequest();
  832. $this->assertEquals($request->base, '');
  833. $this->assertEquals($request->webroot, '/');
  834. Configure::write('App.dir', 'auth');
  835. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  836. $_SERVER['PHP_SELF'] = '/demos/auth/webroot/index.php';
  837. $request = new CakeRequest();
  838. $this->assertEquals($request->base, '/demos/auth');
  839. $this->assertEquals($request->webroot, '/demos/auth/');
  840. Configure::write('App.dir', 'code');
  841. $_SERVER['DOCUMENT_ROOT'] = '/Library/WebServer/Documents';
  842. $_SERVER['PHP_SELF'] = '/clients/PewterReport/code/webroot/index.php';
  843. $request = new CakeRequest();
  844. $this->assertEquals($request->base, '/clients/PewterReport/code');
  845. $this->assertEquals($request->webroot, '/clients/PewterReport/code/');
  846. }
  847. /**
  848. * testBaseUrlwithModRewriteAlias method
  849. *
  850. * @return void
  851. */
  852. public function testBaseUrlwithModRewriteAlias() {
  853. $_SERVER['DOCUMENT_ROOT'] = '/home/aplusnur/public_html';
  854. $_SERVER['PHP_SELF'] = '/control/index.php';
  855. Configure::write('App.base', '/control');
  856. $request = new CakeRequest();
  857. $this->assertEquals($request->base, '/control');
  858. $this->assertEquals($request->webroot, '/control/');
  859. Configure::write('App.base', false);
  860. Configure::write('App.dir', 'affiliate');
  861. Configure::write('App.webroot', 'newaffiliate');
  862. $_SERVER['DOCUMENT_ROOT'] = '/var/www/abtravaff/html';
  863. $_SERVER['PHP_SELF'] = '/newaffiliate/index.php';
  864. $request = new CakeRequest();
  865. $this->assertEquals($request->base, '/newaffiliate');
  866. $this->assertEquals($request->webroot, '/newaffiliate/');
  867. }
  868. /**
  869. * test base, webroot, and url parsing when there is no url rewriting
  870. *
  871. * @return void
  872. */
  873. public function testBaseUrlWithNoModRewrite() {
  874. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites';
  875. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake/index.php';
  876. $_SERVER['PHP_SELF'] = '/cake/index.php/posts/index';
  877. $_SERVER['REQUEST_URI'] = '/cake/index.php/posts/index';
  878. Configure::write('App', array(
  879. 'dir' => APP_DIR,
  880. 'webroot' => WEBROOT_DIR,
  881. 'base' => false,
  882. 'baseUrl' => '/cake/index.php'
  883. ));
  884. $request = new CakeRequest();
  885. $this->assertEquals($request->base, '/cake/index.php');
  886. $this->assertEquals($request->webroot, '/cake/app/webroot/');
  887. $this->assertEquals($request->url, 'posts/index');
  888. }
  889. /**
  890. * testBaseUrlAndWebrootWithBaseUrl method
  891. *
  892. * @return void
  893. */
  894. public function testBaseUrlAndWebrootWithBaseUrl() {
  895. Configure::write('App.dir', 'app');
  896. Configure::write('App.baseUrl', '/app/webroot/index.php');
  897. $request = new CakeRequest();
  898. $this->assertEquals($request->base, '/app/webroot/index.php');
  899. $this->assertEquals($request->webroot, '/app/webroot/');
  900. Configure::write('App.baseUrl', '/app/webroot/test.php');
  901. $request = new CakeRequest();
  902. $this->assertEquals($request->base, '/app/webroot/test.php');
  903. $this->assertEquals($request->webroot, '/app/webroot/');
  904. Configure::write('App.baseUrl', '/app/index.php');
  905. $request = new CakeRequest();
  906. $this->assertEquals($request->base, '/app/index.php');
  907. $this->assertEquals($request->webroot, '/app/webroot/');
  908. Configure::write('App.baseUrl', '/CakeBB/app/webroot/index.php');
  909. $request = new CakeRequest();
  910. $this->assertEquals($request->base, '/CakeBB/app/webroot/index.php');
  911. $this->assertEquals($request->webroot, '/CakeBB/app/webroot/');
  912. Configure::write('App.baseUrl', '/CakeBB/app/index.php');
  913. $request = new CakeRequest();
  914. $this->assertEquals($request->base, '/CakeBB/app/index.php');
  915. $this->assertEquals($request->webroot, '/CakeBB/app/webroot/');
  916. Configure::write('App.baseUrl', '/CakeBB/index.php');
  917. $request = new CakeRequest();
  918. $this->assertEquals($request->base, '/CakeBB/index.php');
  919. $this->assertEquals($request->webroot, '/CakeBB/app/webroot/');
  920. Configure::write('App.baseUrl', '/dbhauser/index.php');
  921. $_SERVER['DOCUMENT_ROOT'] = '/kunden/homepages/4/d181710652/htdocs/joomla';
  922. $_SERVER['SCRIPT_FILENAME'] = '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php';
  923. $request = new CakeRequest();
  924. $this->assertEquals($request->base, '/dbhauser/index.php');
  925. $this->assertEquals($request->webroot, '/dbhauser/app/webroot/');
  926. }
  927. /**
  928. * test baseUrl with no rewrite and using the top level index.php.
  929. *
  930. * @return void
  931. */
  932. public function testBaseUrlNoRewriteTopLevelIndex() {
  933. Configure::write('App.baseUrl', '/index.php');
  934. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev';
  935. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/index.php';
  936. $request = new CakeRequest();
  937. $this->assertEquals('/index.php', $request->base);
  938. $this->assertEquals('/app/webroot/', $request->webroot);
  939. }
  940. /**
  941. * Check that a sub-directory containing app|webroot doesn't get mishandled when re-writing is off.
  942. *
  943. * @return void
  944. */
  945. public function testBaseUrlWithAppAndWebrootInDirname() {
  946. Configure::write('App.baseUrl', '/approval/index.php');
  947. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
  948. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/approval/index.php';
  949. $request = new CakeRequest();
  950. $this->assertEquals('/approval/index.php', $request->base);
  951. $this->assertEquals('/approval/app/webroot/', $request->webroot);
  952. Configure::write('App.baseUrl', '/webrootable/index.php');
  953. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
  954. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/webrootable/index.php';
  955. $request = new CakeRequest();
  956. $this->assertEquals('/webrootable/index.php', $request->base);
  957. $this->assertEquals('/webrootable/app/webroot/', $request->webroot);
  958. }
  959. /**
  960. * test baseUrl with no rewrite, and using the app/webroot/index.php file as is normal with virtual hosts.
  961. *
  962. * @return void
  963. */
  964. public function testBaseUrlNoRewriteWebrootIndex() {
  965. Configure::write('App.baseUrl', '/index.php');
  966. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev/app/webroot';
  967. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/app/webroot/index.php';
  968. $request = new CakeRequest();
  969. $this->assertEquals('/index.php', $request->base);
  970. $this->assertEquals('/', $request->webroot);
  971. }
  972. /**
  973. * Test that a request with a . in the main GET parameter is filtered out.
  974. * PHP changes GET parameter keys containing dots to _.
  975. *
  976. * @return void
  977. */
  978. public function testGetParamsWithDot() {
  979. $_GET = array();
  980. $_GET['/posts/index/add_add'] = '';
  981. $_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
  982. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/index/add.add';
  983. $request = new CakeRequest();
  984. $this->assertEquals(array(), $request->query);
  985. }
  986. /**
  987. * Test that a request with urlencoded bits in the main GET parameter are filtered out.
  988. *
  989. * @return void
  990. */
  991. public function testGetParamWithUrlencodedElement() {
  992. $_GET = array();
  993. $_GET['/posts/add/∂∂'] = '';
  994. $_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
  995. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/add/%E2%88%82%E2%88%82';
  996. $request = new CakeRequest();
  997. $this->assertEquals(array(), $request->query);
  998. }
  999. /**
  1000. * generator for environment configurations
  1001. *
  1002. * @return void
  1003. */
  1004. public static function environmentGenerator() {
  1005. return array(
  1006. array(
  1007. 'IIS - No rewrite base path',
  1008. array(
  1009. 'App' => array(
  1010. 'base' => false,
  1011. 'baseUrl' => '/index.php',
  1012. 'dir' => 'app',
  1013. 'webroot' => 'webroot'
  1014. ),
  1015. 'SERVER' => array(
  1016. 'SCRIPT_NAME' => '/index.php',
  1017. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1018. 'QUERY_STRING' => '',
  1019. 'REQUEST_URI' => '/index.php',
  1020. 'URL' => '/index.php',
  1021. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php',
  1022. 'ORIG_PATH_INFO' => '/index.php',
  1023. 'PATH_INFO' => '',
  1024. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php',
  1025. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1026. 'PHP_SELF' => '/index.php',
  1027. ),
  1028. ),
  1029. array(
  1030. 'base' => '/index.php',
  1031. 'webroot' => '/app/webroot/',
  1032. 'url' => ''
  1033. ),
  1034. ),
  1035. array(
  1036. 'IIS - No rewrite with path, no PHP_SELF',
  1037. array(
  1038. 'App' => array(
  1039. 'base' => false,
  1040. 'baseUrl' => '/index.php?',
  1041. 'dir' => 'app',
  1042. 'webroot' => 'webroot'
  1043. ),
  1044. 'SERVER' => array(
  1045. 'QUERY_STRING' => '/posts/add',
  1046. 'REQUEST_URI' => '/index.php?/posts/add',
  1047. 'PHP_SELF' => '',
  1048. 'URL' => '/index.php?/posts/add',
  1049. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1050. 'argv' => array('/posts/add'),
  1051. 'argc' => 1
  1052. ),
  1053. ),
  1054. array(
  1055. 'url' => 'posts/add',
  1056. 'base' => '/index.php?',
  1057. 'webroot' => '/app/webroot/'
  1058. )
  1059. ),
  1060. array(
  1061. 'IIS - No rewrite sub dir 2',
  1062. array(
  1063. 'App' => array(
  1064. 'base' => false,
  1065. 'baseUrl' => '/site/index.php',
  1066. 'dir' => 'app',
  1067. 'webroot' => 'webroot',
  1068. ),
  1069. 'SERVER' => array(
  1070. 'SCRIPT_NAME' => '/site/index.php',
  1071. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1072. 'QUERY_STRING' => '',
  1073. 'REQUEST_URI' => '/site/index.php',
  1074. 'URL' => '/site/index.php',
  1075. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1076. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1077. 'PHP_SELF' => '/site/index.php',
  1078. 'argv' => array(),
  1079. 'argc' => 0
  1080. ),
  1081. ),
  1082. array(
  1083. 'url' => '',
  1084. 'base' => '/site/index.php',
  1085. 'webroot' => '/site/app/webroot/'
  1086. ),
  1087. ),
  1088. array(
  1089. 'IIS - No rewrite sub dir 2 with path',
  1090. array(
  1091. 'App' => array(
  1092. 'base' => false,
  1093. 'baseUrl' => '/site/index.php',
  1094. 'dir' => 'app',
  1095. 'webroot' => 'webroot'
  1096. ),
  1097. 'GET' => array('/posts/add' => ''),
  1098. 'SERVER' => array(
  1099. 'SCRIPT_NAME' => '/site/index.php',
  1100. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1101. 'QUERY_STRING' => '/posts/add',
  1102. 'REQUEST_URI' => '/site/index.php/posts/add',
  1103. 'URL' => '/site/index.php/posts/add',
  1104. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1105. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1106. 'PHP_SELF' => '/site/index.php/posts/add',
  1107. 'argv' => array('/posts/add'),
  1108. 'argc' => 1
  1109. ),
  1110. ),
  1111. array(
  1112. 'url' => 'posts/add',
  1113. 'base' => '/site/index.php',
  1114. 'webroot' => '/site/app/webroot/'
  1115. )
  1116. ),
  1117. array(
  1118. 'Apache - No rewrite, document root set to webroot, requesting path',
  1119. array(
  1120. 'App' => array(
  1121. 'base' => false,
  1122. 'baseUrl' => '/index.php',
  1123. 'dir' => 'app',
  1124. 'webroot' => 'webroot'
  1125. ),
  1126. 'SERVER' => array(
  1127. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1128. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1129. 'QUERY_STRING' => '',
  1130. 'REQUEST_URI' => '/index.php/posts/index',
  1131. 'SCRIPT_NAME' => '/index.php',
  1132. 'PATH_INFO' => '/posts/index',
  1133. 'PHP_SELF' => '/index.php/posts/index',
  1134. ),
  1135. ),
  1136. array(
  1137. 'url' => 'posts/index',
  1138. 'base' => '/index.php',
  1139. 'webroot' => '/'
  1140. ),
  1141. ),
  1142. array(
  1143. 'Apache - No rewrite, document root set to webroot, requesting root',
  1144. array(
  1145. 'App' => array(
  1146. 'base' => false,
  1147. 'baseUrl' => '/index.php',
  1148. 'dir' => 'app',
  1149. 'webroot' => 'webroot'
  1150. ),
  1151. 'SERVER' => array(
  1152. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1153. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1154. 'QUERY_STRING' => '',
  1155. 'REQUEST_URI' => '/index.php',
  1156. 'SCRIPT_NAME' => '/index.php',
  1157. 'PATH_INFO' => '',
  1158. 'PHP_SELF' => '/index.php',
  1159. ),
  1160. ),
  1161. array(
  1162. 'url' => '',
  1163. 'base' => '/index.php',
  1164. 'webroot' => '/'
  1165. ),
  1166. ),
  1167. array(
  1168. 'Apache - No rewrite, document root set above top level cake dir, requesting path',
  1169. array(
  1170. 'App' => array(
  1171. 'base' => false,
  1172. 'baseUrl' => '/site/index.php',
  1173. 'dir' => 'app',
  1174. 'webroot' => 'webroot'
  1175. ),
  1176. 'SERVER' => array(
  1177. 'SERVER_NAME' => 'localhost',
  1178. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1179. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1180. 'REQUEST_URI' => '/site/index.php/posts/index',
  1181. 'SCRIPT_NAME' => '/site/index.php',
  1182. 'PATH_INFO' => '/posts/index',
  1183. 'PHP_SELF' => '/site/index.php/posts/index',
  1184. ),
  1185. ),
  1186. array(
  1187. 'url' => 'posts/index',
  1188. 'base' => '/site/index.php',
  1189. 'webroot' => '/site/app/webroot/',
  1190. ),
  1191. ),
  1192. array(
  1193. 'Apache - No rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1194. array(
  1195. 'App' => array(
  1196. 'base' => false,
  1197. 'baseUrl' => '/site/index.php',
  1198. 'dir' => 'app',
  1199. 'webroot' => 'webroot'
  1200. ),
  1201. 'SERVER' => array(
  1202. 'SERVER_NAME' => 'localhost',
  1203. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1204. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1205. 'REQUEST_URI' => '/site/index.php/',
  1206. 'SCRIPT_NAME' => '/site/index.php',
  1207. 'PHP_SELF' => '/site/index.php/',
  1208. ),
  1209. ),
  1210. array(
  1211. 'url' => '',
  1212. 'base' => '/site/index.php',
  1213. 'webroot' => '/site/app/webroot/',
  1214. ),
  1215. ),
  1216. array(
  1217. 'Apache - No rewrite, document root set above top level cake dir, request path, with GET',
  1218. array(
  1219. 'App' => array(
  1220. 'base' => false,
  1221. 'baseUrl' => '/site/index.php',
  1222. 'dir' => 'app',
  1223. 'webroot' => 'webroot'
  1224. ),
  1225. 'GET' => array('a' => 'b', 'c' => 'd'),
  1226. 'SERVER' => array(
  1227. 'SERVER_NAME' => 'localhost',
  1228. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1229. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1230. 'REQUEST_URI' => '/site/index.php/posts/index?a=b&c=d',
  1231. 'SCRIPT_NAME' => '/site/index.php',
  1232. 'PATH_INFO' => '/posts/index',
  1233. 'PHP_SELF' => '/site/index.php/posts/index',
  1234. 'QUERY_STRING' => 'a=b&c=d'
  1235. ),
  1236. ),
  1237. array(
  1238. 'urlParams' => array('a' => 'b', 'c' => 'd'),
  1239. 'url' => 'posts/index',
  1240. 'base' => '/site/index.php',
  1241. 'webroot' => '/site/app/webroot/',
  1242. ),
  1243. ),
  1244. array(
  1245. 'Apache - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1246. array(
  1247. 'App' => array(
  1248. 'base' => false,
  1249. 'baseUrl' => false,
  1250. 'dir' => 'app',
  1251. 'webroot' => 'webroot'
  1252. ),
  1253. 'SERVER' => array(
  1254. 'SERVER_NAME' => 'localhost',
  1255. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1256. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1257. 'REQUEST_URI' => '/site/',
  1258. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1259. 'PHP_SELF' => '/site/app/webroot/index.php',
  1260. ),
  1261. ),
  1262. array(
  1263. 'url' => '',
  1264. 'base' => '/site',
  1265. 'webroot' => '/site/',
  1266. ),
  1267. ),
  1268. array(
  1269. 'Apache - w/rewrite, document root above top level cake dir, request root, no PATH_INFO/REQUEST_URI',
  1270. array(
  1271. 'App' => array(
  1272. 'base' => false,
  1273. 'baseUrl' => false,
  1274. 'dir' => 'app',
  1275. 'webroot' => 'webroot'
  1276. ),
  1277. 'SERVER' => array(
  1278. 'SERVER_NAME' => 'localhost',
  1279. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1280. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1281. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1282. 'PHP_SELF' => '/site/app/webroot/index.php',
  1283. 'PATH_INFO' => null,
  1284. 'REQUEST_URI' => null,
  1285. ),
  1286. ),
  1287. array(
  1288. 'url' => '',
  1289. 'base' => '/site',
  1290. 'webroot' => '/site/',
  1291. ),
  1292. ),
  1293. array(
  1294. 'Apache - w/rewrite, document root set to webroot, request root, no PATH_INFO/REQUEST_URI',
  1295. array(
  1296. 'App' => array(
  1297. 'base' => false,
  1298. 'baseUrl' => false,
  1299. 'dir' => 'app',
  1300. 'webroot' => 'webroot'
  1301. ),
  1302. 'SERVER' => array(
  1303. 'SERVER_NAME' => 'localhost',
  1304. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1305. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1306. 'SCRIPT_NAME' => '/index.php',
  1307. 'PHP_SELF' => '/index.php',
  1308. 'PATH_INFO' => null,
  1309. 'REQUEST_URI' => null,
  1310. ),
  1311. ),
  1312. array(
  1313. 'url' => '',
  1314. 'base' => '',
  1315. 'webroot' => '/',
  1316. ),
  1317. ),
  1318. array(
  1319. 'Nginx - w/rewrite, document root set to webroot, request root, no PATH_INFO',
  1320. array(
  1321. 'App' => array(
  1322. 'base' => false,
  1323. 'baseUrl' => false,
  1324. 'dir' => 'app',
  1325. 'webroot' => 'webroot'
  1326. ),
  1327. 'GET' => array('/posts/add' => ''),
  1328. 'SERVER' => array(
  1329. 'SERVER_NAME' => 'localhost',
  1330. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1331. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1332. 'SCRIPT_NAME' => '/index.php',
  1333. 'QUERY_STRING' => '/posts/add&',
  1334. 'PHP_SELF' => '/index.php',
  1335. 'PATH_INFO' => null,
  1336. 'REQUEST_URI' => '/posts/add',
  1337. ),
  1338. ),
  1339. array(
  1340. 'url' => 'posts/add',
  1341. 'base' => '',
  1342. 'webroot' => '/',
  1343. 'urlParams' => array()
  1344. ),
  1345. ),
  1346. );
  1347. }
  1348. /**
  1349. * testEnvironmentDetection method
  1350. *
  1351. * @dataProvider environmentGenerator
  1352. * @return void
  1353. */
  1354. public function testEnvironmentDetection($name, $env, $expected) {
  1355. $_GET = array();
  1356. $this->__loadEnvironment($env);
  1357. $request = new CakeRequest();
  1358. $this->assertEquals($expected['url'], $request->url, "url error");
  1359. $this->assertEquals($expected['base'], $request->base, "base error");
  1360. $this->assertEquals($expected['webroot'], $request->webroot, "webroot error");
  1361. if (isset($expected['urlParams'])) {
  1362. $this->assertEquals($request->query, $expected['urlParams'], "GET param mismatch");
  1363. }
  1364. }
  1365. /**
  1366. * test the data() method reading
  1367. *
  1368. * @return void
  1369. */
  1370. public function testDataReading() {
  1371. $_POST['data'] = array(
  1372. 'Model' => array(
  1373. 'field' => 'value'
  1374. )
  1375. );
  1376. $request = new CakeRequest('posts/index');
  1377. $result = $request->data('Model');
  1378. $this->assertEquals($_POST['data']['Model'], $result);
  1379. $result = $request->data('Model.imaginary');
  1380. $this->assertNull($result);
  1381. }
  1382. /**
  1383. * test writing with data()
  1384. *
  1385. * @return void
  1386. */
  1387. public function testDataWriting() {
  1388. $_POST['data'] = array(
  1389. 'Model' => array(
  1390. 'field' => 'value'
  1391. )
  1392. );
  1393. $request = new CakeRequest('posts/index');
  1394. $result = $request->data('Model.new_value', 'new value');
  1395. $this->assertSame($result, $request, 'Return was not $this');
  1396. $this->assertEquals($request->data['Model']['new_value'], 'new value');
  1397. $request->data('Post.title', 'New post')->data('Comment.1.author', 'Mark');
  1398. $this->assertEquals($request->data['Post']['title'], 'New post');
  1399. $this->assertEquals($request->data['Comment']['1']['author'], 'Mark');
  1400. }
  1401. /**
  1402. * test writing falsey values.
  1403. *
  1404. * @return void
  1405. */
  1406. public function testDataWritingFalsey() {
  1407. $request = new CakeRequest('posts/index');
  1408. $request->data('Post.null', null);
  1409. $this->assertNull($request->data['Post']['null']);
  1410. $request->data('Post.false', false);
  1411. $this->assertFalse($request->data['Post']['false']);
  1412. $request->data('Post.zero', 0);
  1413. $this->assertSame(0, $request->data['Post']['zero']);
  1414. $request->data('Post.empty', '');
  1415. $this->assertSame('', $request->data['Post']['empty']);
  1416. }
  1417. /**
  1418. * test accept language
  1419. *
  1420. * @return void
  1421. */
  1422. public function testAcceptLanguage() {
  1423. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
  1424. $result = CakeRequest::acceptLanguage();
  1425. $this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
  1426. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx;en_ca';
  1427. $result = CakeRequest::acceptLanguage();
  1428. $this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
  1429. $result = CakeRequest::acceptLanguage('en-ca');
  1430. $this->assertTrue($result);
  1431. $result = CakeRequest::acceptLanguage('en-us');
  1432. $this->assertFalse($result);
  1433. }
  1434. /**
  1435. * test the here() method
  1436. *
  1437. * @return void
  1438. */
  1439. public function testHere() {
  1440. Configure::write('App.base', '/base_path');
  1441. $_GET = array('test' => 'value');
  1442. $request = new CakeRequest('/posts/add/1/name:value');
  1443. $result = $request->here();
  1444. $this->assertEquals('/base_path/posts/add/1/name:value?test=value', $result);
  1445. $result = $request->here(false);
  1446. $this->assertEquals('/posts/add/1/name:value?test=value', $result);
  1447. $request = new CakeRequest('/posts/base_path/1/name:value');
  1448. $result = $request->here();
  1449. $this->assertEquals('/base_path/posts/base_path/1/name:value?test=value', $result);
  1450. $result = $request->here(false);
  1451. $this->assertEquals('/posts/base_path/1/name:value?test=value', $result);
  1452. }
  1453. /**
  1454. * Test the input() method.
  1455. *
  1456. * @return void
  1457. */
  1458. public function testInput() {
  1459. $request = $this->getMock('CakeRequest', array('_readInput'));
  1460. $request->expects($this->once())->method('_readInput')
  1461. ->will($this->returnValue('I came from stdin'));
  1462. $result = $request->input();
  1463. $this->assertEquals('I came from stdin', $result);
  1464. }
  1465. /**
  1466. * Test input() decoding.
  1467. *
  1468. * @return void
  1469. */
  1470. public function testInputDecode() {
  1471. $request = $this->getMock('CakeRequest', array('_readInput'));
  1472. $request->expects($this->once())->method('_readInput')
  1473. ->will($this->returnValue('{"name":"value"}'));
  1474. $result = $request->input('json_decode');
  1475. $this->assertEquals(array('name' => 'value'), (array)$result);
  1476. }
  1477. /**
  1478. * Test input() decoding with additional arguments.
  1479. *
  1480. * @return void
  1481. */
  1482. public function testInputDecodeExtraParams() {
  1483. $xml = <<<XML
  1484. <?xml version="1.0" encoding="utf-8"?>
  1485. <post>
  1486. <title id="title">Test</title>
  1487. </post>
  1488. XML;
  1489. $request = $this->getMock('CakeRequest', array('_readInput'));
  1490. $request->expects($this->once())->method('_readInput')
  1491. ->will($this->returnValue($xml));
  1492. $result = $request->input('Xml::build', array('return' => 'domdocument'));
  1493. $this->assertInstanceOf('DOMDocument', $result);
  1494. $this->assertEquals(
  1495. 'Test',
  1496. $result->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText
  1497. );
  1498. }
  1499. /**
  1500. * Test is('requested') and isRequested()
  1501. *
  1502. * @return void
  1503. */
  1504. public function testIsRequested() {
  1505. $request = new CakeRequest('/posts/index');
  1506. $request->addParams(array(
  1507. 'controller' => 'posts',
  1508. 'action' => 'index',
  1509. 'plugin' => null,
  1510. 'requested' => 1
  1511. ));
  1512. $this->assertTrue($request->is('requested'));
  1513. $this->assertTrue($request->isRequested());
  1514. $request = new CakeRequest('/posts/index');
  1515. $request->addParams(array(
  1516. 'controller' => 'posts',
  1517. 'action' => 'index',
  1518. 'plugin' => null,
  1519. ));
  1520. $this->assertFalse($request->is('requested'));
  1521. $this->assertFalse($request->isRequested());
  1522. }
  1523. /**
  1524. * loadEnvironment method
  1525. *
  1526. * @param mixed $env
  1527. * @return void
  1528. */
  1529. protected function __loadEnvironment($env) {
  1530. if (isset($env['App'])) {
  1531. Configure::write('App', $env['App']);
  1532. }
  1533. if (isset($env['GET'])) {
  1534. foreach ($env['GET'] as $key => $val) {
  1535. $_GET[$key] = $val;
  1536. }
  1537. }
  1538. if (isset($env['POST'])) {
  1539. foreach ($env['POST'] as $key => $val) {
  1540. $_POST[$key] = $val;
  1541. }
  1542. }
  1543. if (isset($env['SERVER'])) {
  1544. foreach ($env['SERVER'] as $key => $val) {
  1545. $_SERVER[$key] = $val;
  1546. }
  1547. }
  1548. }
  1549. }