CakeRequestTest.php 43 KB

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