RequestTest.php 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since CakePHP(tm) v 2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Network;
  16. use Cake\Core\Configure;
  17. use Cake\Error;
  18. use Cake\Network\Request;
  19. use Cake\Routing\Dispatcher;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\Utility\Xml;
  22. /**
  23. * Class TestRequest
  24. *
  25. */
  26. class RequestTest extends TestCase {
  27. /**
  28. * Setup callback
  29. *
  30. * @return void
  31. */
  32. public function setUp() {
  33. parent::setUp();
  34. $this->_case = null;
  35. if (isset($_GET['case'])) {
  36. $this->_case = $_GET['case'];
  37. unset($_GET['case']);
  38. }
  39. Configure::write('App.baseUrl', false);
  40. }
  41. /**
  42. * TearDown
  43. *
  44. * @return void
  45. */
  46. public function tearDown() {
  47. parent::tearDown();
  48. if (!empty($this->_case)) {
  49. $_GET['case'] = $this->_case;
  50. }
  51. }
  52. /**
  53. * Test that the autoparse = false constructor works.
  54. *
  55. * @return void
  56. */
  57. public function testNoAutoParseConstruction() {
  58. $_GET = array(
  59. 'one' => 'param'
  60. );
  61. $request = new Request();
  62. $this->assertFalse(isset($request->query['one']));
  63. }
  64. /**
  65. * Test construction
  66. *
  67. * @return void
  68. */
  69. public function testConstructionQueryData() {
  70. $data = array(
  71. 'query' => array(
  72. 'one' => 'param',
  73. 'two' => 'banana'
  74. ),
  75. 'url' => 'some/path'
  76. );
  77. $request = new Request($data);
  78. $this->assertEquals($request->query, $data['query']);
  79. $this->assertEquals('some/path', $request->url);
  80. }
  81. /**
  82. * Test that querystring args provided in the URL string are parsed.
  83. *
  84. * @return void
  85. */
  86. public function testQueryStringParsingFromInputUrl() {
  87. $_GET = array();
  88. $request = new Request(array('url' => 'some/path?one=something&two=else'));
  89. $expected = array('one' => 'something', 'two' => 'else');
  90. $this->assertEquals($expected, $request->query);
  91. $this->assertEquals('some/path?one=something&two=else', $request->url);
  92. }
  93. /**
  94. * Test that named arguments + querystrings are handled correctly.
  95. *
  96. * @return void
  97. */
  98. public function testQueryStringAndNamedParams() {
  99. $_SERVER['REQUEST_URI'] = '/tasks/index?ts=123456';
  100. $request = Request::createFromGlobals();
  101. $this->assertEquals('tasks/index', $request->url);
  102. $_SERVER['REQUEST_URI'] = '/tasks/index/?ts=123456';
  103. $request = Request::createFromGlobals();
  104. $this->assertEquals('tasks/index/', $request->url);
  105. $_SERVER['REQUEST_URI'] = '/some/path?url=http://cakephp.org';
  106. $request = Request::createFromGlobals();
  107. $this->assertEquals('some/path', $request->url);
  108. $_SERVER['REQUEST_URI'] = Configure::read('App.fullBaseUrl') . '/other/path?url=http://cakephp.org';
  109. $request = Request::createFromGlobals();
  110. $this->assertEquals('other/path', $request->url);
  111. }
  112. /**
  113. * Test addParams() method
  114. *
  115. * @return void
  116. */
  117. public function testAddParams() {
  118. $request = new Request();
  119. $request->params = array('controller' => 'posts', 'action' => 'view');
  120. $result = $request->addParams(array('plugin' => null, 'action' => 'index'));
  121. $this->assertSame($result, $request, 'Method did not return itself. %s');
  122. $this->assertEquals('posts', $request->controller);
  123. $this->assertEquals('index', $request->action);
  124. $this->assertEquals(null, $request->plugin);
  125. }
  126. /**
  127. * Test splicing in paths.
  128. *
  129. * @return void
  130. */
  131. public function testAddPaths() {
  132. $request = new Request();
  133. $request->webroot = '/some/path/going/here/';
  134. $result = $request->addPaths(array(
  135. 'random' => '/something', 'webroot' => '/', 'here' => '/', 'base' => '/base_dir'
  136. ));
  137. $this->assertSame($result, $request, 'Method did not return itself. %s');
  138. $this->assertEquals('/', $request->webroot);
  139. $this->assertEquals('/base_dir', $request->base);
  140. $this->assertEquals('/', $request->here);
  141. $this->assertFalse(isset($request->random));
  142. }
  143. /**
  144. * Test parsing POST data into the object.
  145. *
  146. * @return void
  147. */
  148. public function testPostParsing() {
  149. $post = array(
  150. 'Article' => array('title')
  151. );
  152. $request = new Request(compact('post'));
  153. $this->assertEquals($post, $request->data);
  154. $post = array('one' => 1, 'two' => 'three');
  155. $request = new Request(compact('post'));
  156. $this->assertEquals($post, $request->data);
  157. $post = array(
  158. 'Article' => array('title' => 'Testing'),
  159. 'action' => 'update'
  160. );
  161. $request = new Request(compact('post'));
  162. $this->assertEquals($post, $request->data);
  163. }
  164. /**
  165. * Test parsing PUT data into the object.
  166. *
  167. * @return void
  168. */
  169. public function testPutParsing() {
  170. $_SERVER['REQUEST_METHOD'] = 'PUT';
  171. $_SERVER['CONTENT_TYPE'] = 'application/x-www-form-urlencoded; charset=UTF-8';
  172. $data = array(
  173. 'Article' => array('title')
  174. );
  175. $request = new Request(array(
  176. 'input' => 'Article[]=title'
  177. ));
  178. $this->assertEquals($data, $request->data);
  179. $data = array('one' => 1, 'two' => 'three');
  180. $request = new Request(array(
  181. 'input' => 'one=1&two=three'
  182. ));
  183. $this->assertEquals($data, $request->data);
  184. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  185. $request = new Request(array(
  186. 'input' => 'Article[title]=Testing&action=update'
  187. ));
  188. $expected = array(
  189. 'Article' => array('title' => 'Testing'),
  190. 'action' => 'update'
  191. );
  192. $this->assertEquals($expected, $request->data);
  193. $_SERVER['REQUEST_METHOD'] = 'PATCH';
  194. $data = array(
  195. 'Article' => array('title'),
  196. 'Tag' => array('Tag' => array(1, 2))
  197. );
  198. $request = new Request(array(
  199. 'input' => 'Article[]=title&Tag[Tag][]=1&Tag[Tag][]=2'
  200. ));
  201. $this->assertEquals($data, $request->data);
  202. }
  203. /**
  204. * Test parsing json PUT data into the object.
  205. *
  206. * @return void
  207. */
  208. public function testPutParsingJSON() {
  209. $_SERVER['REQUEST_METHOD'] = 'PUT';
  210. $_SERVER['CONTENT_TYPE'] = 'application/json';
  211. $data = '{"Article":["title"]}';
  212. $request = new Request([
  213. 'input' => $data
  214. ]);
  215. $this->assertEquals([], $request->data);
  216. $result = $request->input('json_decode', true);
  217. $this->assertEquals(['title'], $result['Article']);
  218. }
  219. /**
  220. * Test parsing of FILES array
  221. *
  222. * @return void
  223. */
  224. public function testFilesParsing() {
  225. $files = array(
  226. 'name' => array(
  227. 'File' => array(
  228. array('data' => 'cake_sqlserver_patch.patch'),
  229. array('data' => 'controller.diff'),
  230. array('data' => ''),
  231. array('data' => ''),
  232. ),
  233. 'Post' => array('attachment' => 'jquery-1.2.1.js'),
  234. ),
  235. 'type' => array(
  236. 'File' => array(
  237. array('data' => ''),
  238. array('data' => ''),
  239. array('data' => ''),
  240. array('data' => ''),
  241. ),
  242. 'Post' => array('attachment' => 'application/x-javascript'),
  243. ),
  244. 'tmp_name' => array(
  245. 'File' => array(
  246. array('data' => '/private/var/tmp/phpy05Ywj'),
  247. array('data' => '/private/var/tmp/php7MBztY'),
  248. array('data' => ''),
  249. array('data' => ''),
  250. ),
  251. 'Post' => array('attachment' => '/private/var/tmp/phpEwlrIo'),
  252. ),
  253. 'error' => array(
  254. 'File' => array(
  255. array('data' => 0),
  256. array('data' => 0),
  257. array('data' => 4),
  258. array('data' => 4)
  259. ),
  260. 'Post' => array('attachment' => 0)
  261. ),
  262. 'size' => array(
  263. 'File' => array(
  264. array('data' => 6271),
  265. array('data' => 350),
  266. array('data' => 0),
  267. array('data' => 0),
  268. ),
  269. 'Post' => array('attachment' => 80469)
  270. ),
  271. );
  272. $request = new Request(compact('files'));
  273. $expected = array(
  274. 'File' => array(
  275. array(
  276. 'data' => array(
  277. 'name' => 'cake_sqlserver_patch.patch',
  278. 'type' => '',
  279. 'tmp_name' => '/private/var/tmp/phpy05Ywj',
  280. 'error' => 0,
  281. 'size' => 6271,
  282. )
  283. ),
  284. array(
  285. 'data' => array(
  286. 'name' => 'controller.diff',
  287. 'type' => '',
  288. 'tmp_name' => '/private/var/tmp/php7MBztY',
  289. 'error' => 0,
  290. 'size' => 350,
  291. )
  292. ),
  293. array(
  294. 'data' => array(
  295. 'name' => '',
  296. 'type' => '',
  297. 'tmp_name' => '',
  298. 'error' => 4,
  299. 'size' => 0,
  300. )
  301. ),
  302. array(
  303. 'data' => array(
  304. 'name' => '',
  305. 'type' => '',
  306. 'tmp_name' => '',
  307. 'error' => 4,
  308. 'size' => 0,
  309. )
  310. ),
  311. ),
  312. 'Post' => array(
  313. 'attachment' => array(
  314. 'name' => 'jquery-1.2.1.js',
  315. 'type' => 'application/x-javascript',
  316. 'tmp_name' => '/private/var/tmp/phpEwlrIo',
  317. 'error' => 0,
  318. 'size' => 80469,
  319. )
  320. )
  321. );
  322. $this->assertEquals($expected, $request->data);
  323. $files = array(
  324. 'name' => array(
  325. 'Document' => array(
  326. 1 => array(
  327. 'birth_cert' => 'born on.txt',
  328. 'passport' => 'passport.txt',
  329. 'drivers_license' => 'ugly pic.jpg'
  330. ),
  331. 2 => array(
  332. 'birth_cert' => 'aunt betty.txt',
  333. 'passport' => 'betty-passport.txt',
  334. 'drivers_license' => 'betty-photo.jpg'
  335. ),
  336. ),
  337. ),
  338. 'type' => array(
  339. 'Document' => array(
  340. 1 => array(
  341. 'birth_cert' => 'application/octet-stream',
  342. 'passport' => 'application/octet-stream',
  343. 'drivers_license' => 'application/octet-stream',
  344. ),
  345. 2 => array(
  346. 'birth_cert' => 'application/octet-stream',
  347. 'passport' => 'application/octet-stream',
  348. 'drivers_license' => 'application/octet-stream',
  349. )
  350. )
  351. ),
  352. 'tmp_name' => array(
  353. 'Document' => array(
  354. 1 => array(
  355. 'birth_cert' => '/private/var/tmp/phpbsUWfH',
  356. 'passport' => '/private/var/tmp/php7f5zLt',
  357. 'drivers_license' => '/private/var/tmp/phpMXpZgT',
  358. ),
  359. 2 => array(
  360. 'birth_cert' => '/private/var/tmp/php5kHZt0',
  361. 'passport' => '/private/var/tmp/phpnYkOuM',
  362. 'drivers_license' => '/private/var/tmp/php9Rq0P3',
  363. )
  364. )
  365. ),
  366. 'error' => array(
  367. 'Document' => array(
  368. 1 => array(
  369. 'birth_cert' => 0,
  370. 'passport' => 0,
  371. 'drivers_license' => 0,
  372. ),
  373. 2 => array(
  374. 'birth_cert' => 0,
  375. 'passport' => 0,
  376. 'drivers_license' => 0,
  377. )
  378. )
  379. ),
  380. 'size' => array(
  381. 'Document' => array(
  382. 1 => array(
  383. 'birth_cert' => 123,
  384. 'passport' => 458,
  385. 'drivers_license' => 875,
  386. ),
  387. 2 => array(
  388. 'birth_cert' => 876,
  389. 'passport' => 976,
  390. 'drivers_license' => 9783,
  391. )
  392. )
  393. )
  394. );
  395. $request = new Request(compact('files'));
  396. $expected = array(
  397. 'Document' => array(
  398. 1 => array(
  399. 'birth_cert' => array(
  400. 'name' => 'born on.txt',
  401. 'tmp_name' => '/private/var/tmp/phpbsUWfH',
  402. 'error' => 0,
  403. 'size' => 123,
  404. 'type' => 'application/octet-stream',
  405. ),
  406. 'passport' => array(
  407. 'name' => 'passport.txt',
  408. 'tmp_name' => '/private/var/tmp/php7f5zLt',
  409. 'error' => 0,
  410. 'size' => 458,
  411. 'type' => 'application/octet-stream',
  412. ),
  413. 'drivers_license' => array(
  414. 'name' => 'ugly pic.jpg',
  415. 'tmp_name' => '/private/var/tmp/phpMXpZgT',
  416. 'error' => 0,
  417. 'size' => 875,
  418. 'type' => 'application/octet-stream',
  419. ),
  420. ),
  421. 2 => array(
  422. 'birth_cert' => array(
  423. 'name' => 'aunt betty.txt',
  424. 'tmp_name' => '/private/var/tmp/php5kHZt0',
  425. 'error' => 0,
  426. 'size' => 876,
  427. 'type' => 'application/octet-stream',
  428. ),
  429. 'passport' => array(
  430. 'name' => 'betty-passport.txt',
  431. 'tmp_name' => '/private/var/tmp/phpnYkOuM',
  432. 'error' => 0,
  433. 'size' => 976,
  434. 'type' => 'application/octet-stream',
  435. ),
  436. 'drivers_license' => array(
  437. 'name' => 'betty-photo.jpg',
  438. 'tmp_name' => '/private/var/tmp/php9Rq0P3',
  439. 'error' => 0,
  440. 'size' => 9783,
  441. 'type' => 'application/octet-stream',
  442. ),
  443. ),
  444. )
  445. );
  446. $this->assertEquals($expected, $request->data);
  447. $files = array(
  448. 'name' => array('birth_cert' => 'born on.txt'),
  449. 'type' => array('birth_cert' => 'application/octet-stream'),
  450. 'tmp_name' => array('birth_cert' => '/private/var/tmp/phpbsUWfH'),
  451. 'error' => array('birth_cert' => 0),
  452. 'size' => array('birth_cert' => 123)
  453. );
  454. $request = new Request(compact('files'));
  455. $expected = array(
  456. 'birth_cert' => array(
  457. 'name' => 'born on.txt',
  458. 'type' => 'application/octet-stream',
  459. 'tmp_name' => '/private/var/tmp/phpbsUWfH',
  460. 'error' => 0,
  461. 'size' => 123
  462. )
  463. );
  464. $this->assertEquals($expected, $request->data);
  465. }
  466. /**
  467. * Test that files in the 0th index work.
  468. *
  469. * @return void
  470. */
  471. public function testFilesZeroithIndex() {
  472. $files = array(
  473. 0 => array(
  474. 'name' => 'cake_sqlserver_patch.patch',
  475. 'type' => 'text/plain',
  476. 'tmp_name' => '/private/var/tmp/phpy05Ywj',
  477. 'error' => 0,
  478. 'size' => 6271,
  479. ),
  480. );
  481. $request = new Request([
  482. 'files' => $files
  483. ]);
  484. $this->assertEquals($files, $request->data);
  485. }
  486. /**
  487. * Test method overrides coming in from POST data.
  488. *
  489. * @return void
  490. */
  491. public function testMethodOverrides() {
  492. $post = array('_method' => 'POST');
  493. $request = new Request(compact('post'));
  494. $this->assertEquals(env('REQUEST_METHOD'), 'POST');
  495. $post = array('_method' => 'DELETE');
  496. $request = new Request(compact('post'));
  497. $this->assertEquals(env('REQUEST_METHOD'), 'DELETE');
  498. $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
  499. $request = new Request();
  500. $this->assertEquals(env('REQUEST_METHOD'), 'PUT');
  501. }
  502. /**
  503. * Test the clientIp method.
  504. *
  505. * @return void
  506. */
  507. public function testclientIp() {
  508. $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.5, 10.0.1.1, proxy.com';
  509. $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
  510. $_SERVER['REMOTE_ADDR'] = '192.168.1.3';
  511. $request = new Request();
  512. $request->trustProxy = true;
  513. $this->assertEquals('192.168.1.5', $request->clientIp());
  514. $request->trustProxy = false;
  515. $this->assertEquals('192.168.1.2', $request->clientIp());
  516. unset($_SERVER['HTTP_X_FORWARDED_FOR']);
  517. $this->assertEquals('192.168.1.2', $request->clientIp());
  518. unset($_SERVER['HTTP_CLIENT_IP']);
  519. $this->assertEquals('192.168.1.3', $request->clientIp());
  520. $_SERVER['HTTP_CLIENTADDRESS'] = '10.0.1.2, 10.0.1.1';
  521. $this->assertEquals('10.0.1.2', $request->clientIp());
  522. }
  523. /**
  524. * Test the referrer function.
  525. *
  526. * @return void
  527. */
  528. public function testReferer() {
  529. $request = new Request();
  530. $request->webroot = '/';
  531. $_SERVER['HTTP_REFERER'] = 'http://cakephp.org';
  532. $result = $request->referer();
  533. $this->assertSame($result, 'http://cakephp.org');
  534. $_SERVER['HTTP_REFERER'] = '';
  535. $result = $request->referer();
  536. $this->assertSame($result, '/');
  537. $_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '/some/path';
  538. $result = $request->referer(true);
  539. $this->assertSame($result, '/some/path');
  540. $_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '/some/path';
  541. $result = $request->referer(false);
  542. $this->assertSame($result, Configure::read('App.fullBaseUrl') . '/some/path');
  543. $_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '/recipes/add';
  544. $result = $request->referer(true);
  545. $this->assertSame($result, '/recipes/add');
  546. $_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org';
  547. $result = $request->referer();
  548. $this->assertSame(Configure::read('App.fullBaseUrl') . '/recipes/add', $result);
  549. $request->trustProxy = true;
  550. $result = $request->referer();
  551. $this->assertSame('cakephp.org', $result);
  552. }
  553. /**
  554. * Test referer() with a base path that duplicates the
  555. * first segment.
  556. *
  557. * @return void
  558. */
  559. public function testRefererBasePath() {
  560. $request = new Request('some/path');
  561. $request->url = 'users/login';
  562. $request->webroot = '/waves/';
  563. $request->base = '/waves';
  564. $request->here = '/waves/users/login';
  565. $_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '/waves/waves/add';
  566. $result = $request->referer(true);
  567. $this->assertSame($result, '/waves/add');
  568. }
  569. /**
  570. * test the simple uses of is()
  571. *
  572. * @return void
  573. */
  574. public function testIsHttpMethods() {
  575. $request = new Request();
  576. $this->assertFalse($request->is('undefined-behavior'));
  577. $_SERVER['REQUEST_METHOD'] = 'GET';
  578. $this->assertTrue($request->is('get'));
  579. $_SERVER['REQUEST_METHOD'] = 'POST';
  580. $this->assertTrue($request->is('POST'));
  581. $_SERVER['REQUEST_METHOD'] = 'PUT';
  582. $this->assertTrue($request->is('put'));
  583. $this->assertFalse($request->is('get'));
  584. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  585. $this->assertTrue($request->is('delete'));
  586. $this->assertTrue($request->isDelete());
  587. $_SERVER['REQUEST_METHOD'] = 'delete';
  588. $this->assertFalse($request->is('delete'));
  589. }
  590. /**
  591. * Test is() with multiple types.
  592. *
  593. * @return void
  594. */
  595. public function testIsMultiple() {
  596. $request = new Request();
  597. $_SERVER['REQUEST_METHOD'] = 'GET';
  598. $this->assertTrue($request->is(array('get', 'post')));
  599. $_SERVER['REQUEST_METHOD'] = 'POST';
  600. $this->assertTrue($request->is(array('get', 'post')));
  601. $_SERVER['REQUEST_METHOD'] = 'PUT';
  602. $this->assertFalse($request->is(array('get', 'post')));
  603. }
  604. /**
  605. * Test isAll()
  606. *
  607. * @return void
  608. */
  609. public function testIsAll() {
  610. $request = new Request();
  611. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  612. $_SERVER['REQUEST_METHOD'] = 'GET';
  613. $this->assertTrue($request->isAll(array('ajax', 'get')));
  614. $this->assertFalse($request->isAll(array('post', 'get')));
  615. $this->assertFalse($request->isAll(array('ajax', 'post')));
  616. }
  617. /**
  618. * Test the method() method.
  619. *
  620. * @return void
  621. */
  622. public function testMethod() {
  623. $_SERVER['REQUEST_METHOD'] = 'delete';
  624. $request = new Request();
  625. $this->assertEquals('delete', $request->method());
  626. }
  627. /**
  628. * Test host retrieval.
  629. *
  630. * @return void
  631. */
  632. public function testHost() {
  633. $_SERVER['HTTP_HOST'] = 'localhost';
  634. $request = new Request();
  635. $this->assertEquals('localhost', $request->host());
  636. }
  637. /**
  638. * test port retrieval.
  639. *
  640. * @return void
  641. */
  642. public function testPort() {
  643. $_SERVER['SERVER_PORT'] = '80';
  644. $request = new Request();
  645. $this->assertEquals('80', $request->port());
  646. $_SERVER['SERVER_PORT'] = '443';
  647. $_SERVER['HTTP_X_FORWARDED_PORT'] = 80;
  648. $this->assertEquals('443', $request->port());
  649. $request->trustProxy = true;
  650. $this->assertEquals('80', $request->port());
  651. }
  652. /**
  653. * test domain retrieval.
  654. *
  655. * @return void
  656. */
  657. public function testDomain() {
  658. $_SERVER['HTTP_HOST'] = 'something.example.com';
  659. $request = new Request();
  660. $this->assertEquals('example.com', $request->domain());
  661. $_SERVER['HTTP_HOST'] = 'something.example.co.uk';
  662. $this->assertEquals('example.co.uk', $request->domain(2));
  663. }
  664. /**
  665. * Test scheme() method.
  666. *
  667. * @return void
  668. */
  669. public function testScheme() {
  670. $_SERVER['HTTPS'] = 'on';
  671. $request = new Request();
  672. $this->assertEquals('https', $request->scheme());
  673. unset($_SERVER['HTTPS']);
  674. $this->assertEquals('http', $request->scheme());
  675. $_SERVER['HTTP_X_FORWARDED_PROTO'] = 'https';
  676. $request->trustProxy = true;
  677. $this->assertEquals('https', $request->scheme());
  678. }
  679. /**
  680. * test getting subdomains for a host.
  681. *
  682. * @return void
  683. */
  684. public function testSubdomain() {
  685. $_SERVER['HTTP_HOST'] = 'something.example.com';
  686. $request = new Request();
  687. $this->assertEquals(array('something'), $request->subdomains());
  688. $_SERVER['HTTP_HOST'] = 'www.something.example.com';
  689. $this->assertEquals(array('www', 'something'), $request->subdomains());
  690. $_SERVER['HTTP_HOST'] = 'www.something.example.co.uk';
  691. $this->assertEquals(array('www', 'something'), $request->subdomains(2));
  692. $_SERVER['HTTP_HOST'] = 'example.co.uk';
  693. $this->assertEquals(array(), $request->subdomains(2));
  694. }
  695. /**
  696. * Test ajax, flash and friends
  697. *
  698. * @return void
  699. */
  700. public function testisAjaxFlashAndFriends() {
  701. $request = new Request();
  702. $_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
  703. $this->assertTrue($request->is('flash'));
  704. $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash';
  705. $this->assertTrue($request->is('flash'));
  706. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  707. $this->assertTrue($request->is('ajax'));
  708. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHTTPREQUEST';
  709. $this->assertFalse($request->is('ajax'));
  710. $this->assertFalse($request->isAjax());
  711. $_SERVER['HTTP_USER_AGENT'] = 'Android 2.0';
  712. $this->assertTrue($request->is('mobile'));
  713. $this->assertTrue($request->isMobile());
  714. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 5.1; rv:2.0b6pre) Gecko/20100902 Firefox/4.0b6pre Fennec/2.0b1pre';
  715. $this->assertTrue($request->is('mobile'));
  716. $this->assertTrue($request->isMobile());
  717. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; SAMSUNG; OMNIA7)';
  718. $this->assertTrue($request->is('mobile'));
  719. $this->assertTrue($request->isMobile());
  720. }
  721. /**
  722. * Test __call exceptions
  723. *
  724. * @expectedException Cake\Error\Exception
  725. * @return void
  726. */
  727. public function testMagicCallExceptionOnUnknownMethod() {
  728. $request = new Request();
  729. $request->IamABanana();
  730. }
  731. /**
  732. * Test is(ssl)
  733. *
  734. * @return void
  735. */
  736. public function testIsSsl() {
  737. $request = new Request();
  738. $_SERVER['HTTPS'] = 1;
  739. $this->assertTrue($request->is('ssl'));
  740. $_SERVER['HTTPS'] = 'on';
  741. $this->assertTrue($request->is('ssl'));
  742. $_SERVER['HTTPS'] = '1';
  743. $this->assertTrue($request->is('ssl'));
  744. $_SERVER['HTTPS'] = 'I am not empty';
  745. $this->assertTrue($request->is('ssl'));
  746. $_SERVER['HTTPS'] = 1;
  747. $this->assertTrue($request->is('ssl'));
  748. $_SERVER['HTTPS'] = 'off';
  749. $this->assertFalse($request->is('ssl'));
  750. $_SERVER['HTTPS'] = false;
  751. $this->assertFalse($request->is('ssl'));
  752. $_SERVER['HTTPS'] = '';
  753. $this->assertFalse($request->is('ssl'));
  754. }
  755. /**
  756. * Test getting request params with object properties.
  757. *
  758. * @return void
  759. */
  760. public function testMagicget() {
  761. $request = new Request();
  762. $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
  763. $this->assertEquals('posts', $request->controller);
  764. $this->assertEquals('view', $request->action);
  765. $this->assertEquals('blogs', $request->plugin);
  766. $this->assertNull($request->banana);
  767. }
  768. /**
  769. * Test isset()/empty() with overloaded properties.
  770. *
  771. * @return void
  772. */
  773. public function testMagicisset() {
  774. $request = new Request();
  775. $request->params = array(
  776. 'controller' => 'posts',
  777. 'action' => 'view',
  778. 'plugin' => 'blogs',
  779. );
  780. $this->assertTrue(isset($request->controller));
  781. $this->assertFalse(isset($request->notthere));
  782. $this->assertFalse(empty($request->controller));
  783. }
  784. /**
  785. * Test the array access implementation
  786. *
  787. * @return void
  788. */
  789. public function testArrayAccess() {
  790. $request = new Request();
  791. $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
  792. $this->assertEquals('posts', $request['controller']);
  793. $request['slug'] = 'speedy-slug';
  794. $this->assertEquals('speedy-slug', $request->slug);
  795. $this->assertEquals('speedy-slug', $request['slug']);
  796. $this->assertTrue(isset($request['action']));
  797. $this->assertFalse(isset($request['wrong-param']));
  798. $this->assertTrue(isset($request['plugin']));
  799. unset($request['plugin']);
  800. $this->assertFalse(isset($request['plugin']));
  801. $this->assertNull($request['plugin']);
  802. $this->assertNull($request->plugin);
  803. $request = new Request(array('url' => 'some/path?one=something&two=else'));
  804. $this->assertTrue(isset($request['url']['one']));
  805. $request->data = array('Post' => array('title' => 'something'));
  806. $this->assertEquals('something', $request['data']['Post']['title']);
  807. }
  808. /**
  809. * Test adding detectors and having them work.
  810. *
  811. * @return void
  812. */
  813. public function testAddDetector() {
  814. $request = new Request();
  815. $request->addDetector('compare', array('env' => 'TEST_VAR', 'value' => 'something'));
  816. $_SERVER['TEST_VAR'] = 'something';
  817. $this->assertTrue($request->is('compare'), 'Value match failed.');
  818. $_SERVER['TEST_VAR'] = 'wrong';
  819. $this->assertFalse($request->is('compare'), 'Value mis-match failed.');
  820. $request->addDetector('compareCamelCase', array('env' => 'TEST_VAR', 'value' => 'foo'));
  821. $_SERVER['TEST_VAR'] = 'foo';
  822. $this->assertTrue($request->is('compareCamelCase'), 'Value match failed.');
  823. $this->assertTrue($request->is('comparecamelcase'), 'detectors should be case insensitive');
  824. $this->assertTrue($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
  825. $_SERVER['TEST_VAR'] = 'not foo';
  826. $this->assertFalse($request->is('compareCamelCase'), 'Value match failed.');
  827. $this->assertFalse($request->is('comparecamelcase'), 'detectors should be case insensitive');
  828. $this->assertFalse($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
  829. $request->addDetector('banana', array('env' => 'TEST_VAR', 'pattern' => '/^ban.*$/'));
  830. $_SERVER['TEST_VAR'] = 'banana';
  831. $this->assertTrue($request->isBanana());
  832. $_SERVER['TEST_VAR'] = 'wrong value';
  833. $this->assertFalse($request->isBanana());
  834. $request->addDetector('mobile', array('options' => array('Imagination')));
  835. $_SERVER['HTTP_USER_AGENT'] = 'Imagination land';
  836. $this->assertTrue($request->isMobile());
  837. $_SERVER['HTTP_USER_AGENT'] = 'iPhone 3.0';
  838. $this->assertTrue($request->isMobile());
  839. $request->addDetector('callme', array('env' => 'TEST_VAR', 'callback' => array($this, 'detectCallback')));
  840. $request->addDetector('index', array('param' => 'action', 'value' => 'index'));
  841. $request->params['action'] = 'index';
  842. $this->assertTrue($request->isIndex());
  843. $request->params['action'] = 'add';
  844. $this->assertFalse($request->isIndex());
  845. $request->return = true;
  846. $this->assertTrue($request->isCallMe());
  847. $request->return = false;
  848. $this->assertFalse($request->isCallMe());
  849. $request->addDetector('extension', array('param' => 'ext', 'options' => array('pdf', 'png', 'txt')));
  850. $request->params['ext'] = 'pdf';
  851. $this->assertTrue($request->is('extension'));
  852. $request->params['ext'] = 'exe';
  853. $this->assertFalse($request->isExtension());
  854. }
  855. /**
  856. * Helper function for testing callbacks.
  857. *
  858. * @param $request
  859. * @return boolean
  860. */
  861. public function detectCallback($request) {
  862. return (bool)$request->return;
  863. }
  864. /**
  865. * Test getting headers
  866. *
  867. * @return void
  868. */
  869. public function testHeader() {
  870. $_SERVER['HTTP_HOST'] = 'localhost';
  871. $_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';
  872. $request = new Request();
  873. $this->assertEquals($_SERVER['HTTP_HOST'], $request->header('host'));
  874. $this->assertEquals($_SERVER['HTTP_USER_AGENT'], $request->header('User-Agent'));
  875. }
  876. /**
  877. * Test accepts() with and without parameters
  878. *
  879. * @return void
  880. */
  881. public function testAccepts() {
  882. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml;q=0.9,application/xhtml+xml,text/html,text/plain,image/png';
  883. $request = new Request();
  884. $result = $request->accepts();
  885. $expected = array(
  886. 'text/xml', 'application/xhtml+xml', 'text/html', 'text/plain', 'image/png', 'application/xml'
  887. );
  888. $this->assertEquals($expected, $result, 'Content types differ.');
  889. $result = $request->accepts('text/html');
  890. $this->assertTrue($result);
  891. $result = $request->accepts('image/gif');
  892. $this->assertFalse($result);
  893. }
  894. /**
  895. * Test that accept header types are trimmed for comparisons.
  896. *
  897. * @return void
  898. */
  899. public function testAcceptWithWhitespace() {
  900. $_SERVER['HTTP_ACCEPT'] = 'text/xml , text/html , text/plain,image/png';
  901. $request = new Request();
  902. $result = $request->accepts();
  903. $expected = array(
  904. 'text/xml', 'text/html', 'text/plain', 'image/png'
  905. );
  906. $this->assertEquals($expected, $result, 'Content types differ.');
  907. $this->assertTrue($request->accepts('text/html'));
  908. }
  909. /**
  910. * Content types from accepts() should respect the client's q preference values.
  911. *
  912. * @return void
  913. */
  914. public function testAcceptWithQvalueSorting() {
  915. $_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0';
  916. $request = new Request();
  917. $result = $request->accepts();
  918. $expected = array('application/xml', 'text/html', 'application/json');
  919. $this->assertEquals($expected, $result);
  920. }
  921. /**
  922. * Test the raw parsing of accept headers into the q value formatting.
  923. *
  924. * @return void
  925. */
  926. public function testParseAcceptWithQValue() {
  927. $_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0,image/png';
  928. $request = new Request();
  929. $result = $request->parseAccept();
  930. $expected = array(
  931. '1.0' => array('application/xml', 'image/png'),
  932. '0.8' => array('text/html'),
  933. '0.7' => array('application/json'),
  934. );
  935. $this->assertEquals($expected, $result);
  936. }
  937. /**
  938. * Test parsing accept with a confusing accept value.
  939. *
  940. * @return void
  941. */
  942. public function testParseAcceptNoQValues() {
  943. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/plain, */*';
  944. $request = new Request();
  945. $result = $request->parseAccept();
  946. $expected = array(
  947. '1.0' => array('application/json', 'text/plain', '*/*'),
  948. );
  949. $this->assertEquals($expected, $result);
  950. }
  951. /**
  952. * Test parsing accept ignores index param
  953. *
  954. * @return void
  955. */
  956. public function testParseAcceptIgnoreAcceptExtensions() {
  957. $_SERVER['HTTP_ACCEPT'] = 'application/json;level=1, text/plain, */*';
  958. $request = new Request('/', false);
  959. $result = $request->parseAccept();
  960. $expected = array(
  961. '1.0' => array('application/json', 'text/plain', '*/*'),
  962. );
  963. $this->assertEquals($expected, $result);
  964. }
  965. /**
  966. * Test that parsing accept headers with invalid syntax works.
  967. *
  968. * The header used is missing a q value for application/xml.
  969. *
  970. * @return void
  971. */
  972. public function testParseAcceptInvalidSyntax() {
  973. $_SERVER['HTTP_ACCEPT'] = 'text/html,application/xhtml+xml,application/xml;image/png,image/jpeg,image/*;q=0.9,*/*;q=0.8';
  974. $request = new Request('/', false);
  975. $result = $request->parseAccept();
  976. $expected = array(
  977. '1.0' => array('text/html', 'application/xhtml+xml', 'application/xml', 'image/jpeg'),
  978. '0.9' => array('image/*'),
  979. '0.8' => array('*/*'),
  980. );
  981. $this->assertEquals($expected, $result);
  982. }
  983. /**
  984. * Test baseUrl and webroot with ModRewrite
  985. *
  986. * @return void
  987. */
  988. public function testBaseUrlAndWebrootWithModRewrite() {
  989. Configure::write('App.baseUrl', false);
  990. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  991. $_SERVER['PHP_SELF'] = '/urlencode me/App/webroot/index.php';
  992. $_SERVER['PATH_INFO'] = '/posts/view/1';
  993. $request = Request::createFromGlobals();
  994. $this->assertEquals('/urlencode%20me', $request->base);
  995. $this->assertEquals('/urlencode%20me/', $request->webroot);
  996. $this->assertEquals('posts/view/1', $request->url);
  997. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  998. $_SERVER['PHP_SELF'] = '/1.2.x.x/App/webroot/index.php';
  999. $_SERVER['PATH_INFO'] = '/posts/view/1';
  1000. $request = Request::createFromGlobals();
  1001. $this->assertEquals('/1.2.x.x', $request->base);
  1002. $this->assertEquals('/1.2.x.x/', $request->webroot);
  1003. $this->assertEquals('posts/view/1', $request->url);
  1004. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/App/webroot';
  1005. $_SERVER['PHP_SELF'] = '/index.php';
  1006. $_SERVER['PATH_INFO'] = '/posts/add';
  1007. $request = Request::createFromGlobals();
  1008. $this->assertEquals('', $request->base);
  1009. $this->assertEquals('/', $request->webroot);
  1010. $this->assertEquals('posts/add', $request->url);
  1011. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/test/';
  1012. $_SERVER['PHP_SELF'] = '/webroot/index.php';
  1013. $request = Request::createFromGlobals();
  1014. $this->assertEquals('', $request->base);
  1015. $this->assertEquals('/', $request->webroot);
  1016. $_SERVER['DOCUMENT_ROOT'] = '/some/apps/where';
  1017. $_SERVER['PHP_SELF'] = '/App/webroot/index.php';
  1018. $request = Request::createFromGlobals();
  1019. $this->assertEquals('', $request->base);
  1020. $this->assertEquals('/', $request->webroot);
  1021. Configure::write('App.dir', 'auth');
  1022. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  1023. $_SERVER['PHP_SELF'] = '/demos/auth/webroot/index.php';
  1024. $request = Request::createFromGlobals();
  1025. $this->assertEquals('/demos/auth', $request->base);
  1026. $this->assertEquals('/demos/auth/', $request->webroot);
  1027. Configure::write('App.dir', 'code');
  1028. $_SERVER['DOCUMENT_ROOT'] = '/Library/WebServer/Documents';
  1029. $_SERVER['PHP_SELF'] = '/clients/PewterReport/code/webroot/index.php';
  1030. $request = Request::createFromGlobals();
  1031. $this->assertEquals('/clients/PewterReport/code', $request->base);
  1032. $this->assertEquals('/clients/PewterReport/code/', $request->webroot);
  1033. }
  1034. /**
  1035. * Test baseUrl with ModRewrite alias
  1036. *
  1037. * @return void
  1038. */
  1039. public function testBaseUrlwithModRewriteAlias() {
  1040. $_SERVER['DOCUMENT_ROOT'] = '/home/aplusnur/public_html';
  1041. $_SERVER['PHP_SELF'] = '/control/index.php';
  1042. Configure::write('App.base', '/control');
  1043. $request = Request::createFromGlobals();
  1044. $this->assertEquals('/control', $request->base);
  1045. $this->assertEquals('/control/', $request->webroot);
  1046. Configure::write('App.base', false);
  1047. Configure::write('App.dir', 'affiliate');
  1048. Configure::write('App.webroot', 'newaffiliate');
  1049. $_SERVER['DOCUMENT_ROOT'] = '/var/www/abtravaff/html';
  1050. $_SERVER['PHP_SELF'] = '/newaffiliate/index.php';
  1051. $request = Request::createFromGlobals();
  1052. $this->assertEquals('/newaffiliate', $request->base);
  1053. $this->assertEquals('/newaffiliate/', $request->webroot);
  1054. }
  1055. /**
  1056. * Test base, webroot, URL and here parsing when there is URL rewriting but
  1057. * CakePHP gets called with index.php in URL nonetheless.
  1058. *
  1059. * Tests uri with
  1060. * - index.php/
  1061. * - index.php/
  1062. * - index.php/apples/
  1063. * - index.php/bananas/eat/tasty_banana
  1064. *
  1065. * @link https://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/3318
  1066. */
  1067. public function testBaseUrlWithModRewriteAndIndexPhp() {
  1068. $_SERVER['REQUEST_URI'] = '/cakephp/App/webroot/index.php';
  1069. $_SERVER['PHP_SELF'] = '/cakephp/App/webroot/index.php';
  1070. unset($_SERVER['PATH_INFO']);
  1071. $request = Request::createFromGlobals();
  1072. $this->assertEquals('/cakephp', $request->base);
  1073. $this->assertEquals('/cakephp/', $request->webroot);
  1074. $this->assertEquals('', $request->url);
  1075. $this->assertEquals('/cakephp/', $request->here);
  1076. $_SERVER['REQUEST_URI'] = '/cakephp/App/webroot/index.php/';
  1077. $_SERVER['PHP_SELF'] = '/cakephp/App/webroot/index.php/';
  1078. $_SERVER['PATH_INFO'] = '/';
  1079. $request = Request::createFromGlobals();
  1080. $this->assertEquals('/cakephp', $request->base);
  1081. $this->assertEquals('/cakephp/', $request->webroot);
  1082. $this->assertEquals('', $request->url);
  1083. $this->assertEquals('/cakephp/', $request->here);
  1084. $_SERVER['REQUEST_URI'] = '/cakephp/App/webroot/index.php/apples';
  1085. $_SERVER['PHP_SELF'] = '/cakephp/App/webroot/index.php/apples';
  1086. $_SERVER['PATH_INFO'] = '/apples';
  1087. $request = Request::createFromGlobals();
  1088. $this->assertEquals('/cakephp', $request->base);
  1089. $this->assertEquals('/cakephp/', $request->webroot);
  1090. $this->assertEquals('apples', $request->url);
  1091. $this->assertEquals('/cakephp/apples', $request->here);
  1092. $_SERVER['REQUEST_URI'] = '/cakephp/App/webroot/index.php/melons/share/';
  1093. $_SERVER['PHP_SELF'] = '/cakephp/App/webroot/index.php/melons/share/';
  1094. $_SERVER['PATH_INFO'] = '/melons/share/';
  1095. $request = Request::createFromGlobals();
  1096. $this->assertEquals('/cakephp', $request->base);
  1097. $this->assertEquals('/cakephp/', $request->webroot);
  1098. $this->assertEquals('melons/share/', $request->url);
  1099. $this->assertEquals('/cakephp/melons/share/', $request->here);
  1100. $_SERVER['REQUEST_URI'] = '/cakephp/App/webroot/index.php/bananas/eat/tasty_banana';
  1101. $_SERVER['PHP_SELF'] = '/cakephp/App/webroot/index.php/bananas/eat/tasty_banana';
  1102. $_SERVER['PATH_INFO'] = '/bananas/eat/tasty_banana';
  1103. $request = Request::createFromGlobals();
  1104. $this->assertEquals('/cakephp', $request->base);
  1105. $this->assertEquals('/cakephp/', $request->webroot);
  1106. $this->assertEquals('bananas/eat/tasty_banana', $request->url);
  1107. $this->assertEquals('/cakephp/bananas/eat/tasty_banana', $request->here);
  1108. }
  1109. /**
  1110. * Test base, webroot, and URL parsing when there is no URL rewriting
  1111. *
  1112. * @return void
  1113. */
  1114. public function testBaseUrlWithNoModRewrite() {
  1115. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites';
  1116. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake/index.php';
  1117. $_SERVER['PHP_SELF'] = '/cake/index.php/posts/index';
  1118. $_SERVER['REQUEST_URI'] = '/cake/index.php/posts/index';
  1119. Configure::write('App', array(
  1120. 'dir' => APP_DIR,
  1121. 'webroot' => WEBROOT_DIR,
  1122. 'base' => false,
  1123. 'baseUrl' => '/cake/index.php'
  1124. ));
  1125. $request = Request::createFromGlobals();
  1126. $this->assertEquals('/cake/index.php', $request->base);
  1127. $this->assertEquals('/cake/App/webroot/', $request->webroot);
  1128. $this->assertEquals('posts/index', $request->url);
  1129. }
  1130. /**
  1131. * Test baseUrl and webroot with baseUrl
  1132. *
  1133. * @return void
  1134. */
  1135. public function testBaseUrlAndWebrootWithBaseUrl() {
  1136. Configure::write('App.dir', 'App');
  1137. Configure::write('App.baseUrl', '/App/webroot/index.php');
  1138. $request = Request::createFromGlobals();
  1139. $this->assertEquals('/App/webroot/index.php', $request->base);
  1140. $this->assertEquals('/App/webroot/', $request->webroot);
  1141. Configure::write('App.baseUrl', '/App/webroot/test.php');
  1142. $request = Request::createFromGlobals();
  1143. $this->assertEquals('/App/webroot/test.php', $request->base);
  1144. $this->assertEquals('/App/webroot/', $request->webroot);
  1145. Configure::write('App.baseUrl', '/App/index.php');
  1146. $request = Request::createFromGlobals();
  1147. $this->assertEquals('/App/index.php', $request->base);
  1148. $this->assertEquals('/App/webroot/', $request->webroot);
  1149. Configure::write('App.baseUrl', '/CakeBB/App/webroot/index.php');
  1150. $request = Request::createFromGlobals();
  1151. $this->assertEquals('/CakeBB/App/webroot/index.php', $request->base);
  1152. $this->assertEquals('/CakeBB/App/webroot/', $request->webroot);
  1153. Configure::write('App.baseUrl', '/CakeBB/App/index.php');
  1154. $request = Request::createFromGlobals();
  1155. $this->assertEquals('/CakeBB/App/index.php', $request->base);
  1156. $this->assertEquals('/CakeBB/App/webroot/', $request->webroot);
  1157. Configure::write('App.baseUrl', '/CakeBB/index.php');
  1158. $request = Request::createFromGlobals();
  1159. $this->assertEquals('/CakeBB/index.php', $request->base);
  1160. $this->assertEquals('/CakeBB/App/webroot/', $request->webroot);
  1161. Configure::write('App.baseUrl', '/dbhauser/index.php');
  1162. $_SERVER['DOCUMENT_ROOT'] = '/kunden/homepages/4/d181710652/htdocs/joomla';
  1163. $_SERVER['SCRIPT_FILENAME'] = '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php';
  1164. $request = Request::createFromGlobals();
  1165. $this->assertEquals('/dbhauser/index.php', $request->base);
  1166. $this->assertEquals('/dbhauser/App/webroot/', $request->webroot);
  1167. }
  1168. /**
  1169. * Test baseUrl with no rewrite and using the top level index.php.
  1170. *
  1171. * @return void
  1172. */
  1173. public function testBaseUrlNoRewriteTopLevelIndex() {
  1174. Configure::write('App.baseUrl', '/index.php');
  1175. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev';
  1176. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/index.php';
  1177. $request = Request::createFromGlobals();
  1178. $this->assertEquals('/index.php', $request->base);
  1179. $this->assertEquals('/App/webroot/', $request->webroot);
  1180. }
  1181. /**
  1182. * Check that a sub-directory containing app|webroot doesn't get mishandled when re-writing is off.
  1183. *
  1184. * @return void
  1185. */
  1186. public function testBaseUrlWithAppAndWebrootInDirname() {
  1187. Configure::write('App.baseUrl', '/approval/index.php');
  1188. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
  1189. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/approval/index.php';
  1190. $request = Request::createFromGlobals();
  1191. $this->assertEquals('/approval/index.php', $request->base);
  1192. $this->assertEquals('/approval/App/webroot/', $request->webroot);
  1193. Configure::write('App.baseUrl', '/webrootable/index.php');
  1194. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
  1195. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/webrootable/index.php';
  1196. $request = Request::createFromGlobals();
  1197. $this->assertEquals('/webrootable/index.php', $request->base);
  1198. $this->assertEquals('/webrootable/App/webroot/', $request->webroot);
  1199. }
  1200. /**
  1201. * Test baseUrl with no rewrite, and using the app/webroot/index.php file as is normal with virtual hosts.
  1202. *
  1203. * @return void
  1204. */
  1205. public function testBaseUrlNoRewriteWebrootIndex() {
  1206. Configure::write('App.baseUrl', '/index.php');
  1207. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev/App/webroot';
  1208. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/App/webroot/index.php';
  1209. $request = Request::createFromGlobals();
  1210. $this->assertEquals('/index.php', $request->base);
  1211. $this->assertEquals('/', $request->webroot);
  1212. }
  1213. /**
  1214. * Test that a request with a . in the main GET parameter is filtered out.
  1215. * PHP changes GET parameter keys containing dots to _.
  1216. *
  1217. * @return void
  1218. */
  1219. public function testGetParamsWithDot() {
  1220. $_GET = array();
  1221. $_GET['/posts/index/add_add'] = '';
  1222. $_SERVER['PHP_SELF'] = '/cake_dev/App/webroot/index.php';
  1223. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/index/add.add';
  1224. $request = Request::createFromGlobals();
  1225. $this->assertEquals(array(), $request->query);
  1226. }
  1227. /**
  1228. * Test that a request with urlencoded bits in the main GET parameter are filtered out.
  1229. *
  1230. * @return void
  1231. */
  1232. public function testGetParamWithUrlencodedElement() {
  1233. $_GET = array();
  1234. $_GET['/posts/add/∂∂'] = '';
  1235. $_SERVER['PHP_SELF'] = '/cake_dev/App/webroot/index.php';
  1236. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/add/%E2%88%82%E2%88%82';
  1237. $request = Request::createFromGlobals();
  1238. $this->assertEquals(array(), $request->query);
  1239. }
  1240. /**
  1241. * Generator for environment configurations
  1242. *
  1243. * @return array Environment array
  1244. */
  1245. public static function environmentGenerator() {
  1246. return array(
  1247. array(
  1248. 'IIS - No rewrite base path',
  1249. array(
  1250. 'App' => array(
  1251. 'base' => false,
  1252. 'baseUrl' => '/index.php',
  1253. 'dir' => 'App',
  1254. 'webroot' => 'webroot'
  1255. ),
  1256. 'SERVER' => array(
  1257. 'SCRIPT_NAME' => '/index.php',
  1258. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1259. 'QUERY_STRING' => '',
  1260. 'REQUEST_URI' => '/index.php',
  1261. 'URL' => '/index.php',
  1262. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php',
  1263. 'ORIG_PATH_INFO' => '/index.php',
  1264. 'PATH_INFO' => '',
  1265. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php',
  1266. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1267. 'PHP_SELF' => '/index.php',
  1268. ),
  1269. ),
  1270. array(
  1271. 'base' => '/index.php',
  1272. 'webroot' => '/App/webroot/',
  1273. 'url' => ''
  1274. ),
  1275. ),
  1276. array(
  1277. 'IIS - No rewrite with path, no PHP_SELF',
  1278. array(
  1279. 'App' => array(
  1280. 'base' => false,
  1281. 'baseUrl' => '/index.php?',
  1282. 'dir' => 'App',
  1283. 'webroot' => 'webroot'
  1284. ),
  1285. 'SERVER' => array(
  1286. 'QUERY_STRING' => '/posts/add',
  1287. 'REQUEST_URI' => '/index.php?/posts/add',
  1288. 'PHP_SELF' => '',
  1289. 'URL' => '/index.php?/posts/add',
  1290. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1291. 'argv' => array('/posts/add'),
  1292. 'argc' => 1
  1293. ),
  1294. ),
  1295. array(
  1296. 'url' => 'posts/add',
  1297. 'base' => '/index.php?',
  1298. 'webroot' => '/App/webroot/'
  1299. )
  1300. ),
  1301. array(
  1302. 'IIS - No rewrite sub dir 2',
  1303. array(
  1304. 'App' => array(
  1305. 'base' => false,
  1306. 'baseUrl' => '/site/index.php',
  1307. 'dir' => 'App',
  1308. 'webroot' => 'webroot',
  1309. ),
  1310. 'SERVER' => array(
  1311. 'SCRIPT_NAME' => '/site/index.php',
  1312. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1313. 'QUERY_STRING' => '',
  1314. 'REQUEST_URI' => '/site/index.php',
  1315. 'URL' => '/site/index.php',
  1316. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1317. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1318. 'PHP_SELF' => '/site/index.php',
  1319. 'argv' => array(),
  1320. 'argc' => 0
  1321. ),
  1322. ),
  1323. array(
  1324. 'url' => '',
  1325. 'base' => '/site/index.php',
  1326. 'webroot' => '/site/App/webroot/'
  1327. ),
  1328. ),
  1329. array(
  1330. 'IIS - No rewrite sub dir 2 with path',
  1331. array(
  1332. 'App' => array(
  1333. 'base' => false,
  1334. 'baseUrl' => '/site/index.php',
  1335. 'dir' => 'App',
  1336. 'webroot' => 'webroot'
  1337. ),
  1338. 'GET' => array('/posts/add' => ''),
  1339. 'SERVER' => array(
  1340. 'SCRIPT_NAME' => '/site/index.php',
  1341. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1342. 'QUERY_STRING' => '/posts/add',
  1343. 'REQUEST_URI' => '/site/index.php/posts/add',
  1344. 'URL' => '/site/index.php/posts/add',
  1345. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1346. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1347. 'PHP_SELF' => '/site/index.php/posts/add',
  1348. 'argv' => array('/posts/add'),
  1349. 'argc' => 1
  1350. ),
  1351. ),
  1352. array(
  1353. 'url' => 'posts/add',
  1354. 'base' => '/site/index.php',
  1355. 'webroot' => '/site/App/webroot/'
  1356. )
  1357. ),
  1358. array(
  1359. 'Apache - No rewrite, document root set to webroot, requesting path',
  1360. array(
  1361. 'App' => array(
  1362. 'base' => false,
  1363. 'baseUrl' => '/index.php',
  1364. 'dir' => 'App',
  1365. 'webroot' => 'webroot'
  1366. ),
  1367. 'SERVER' => array(
  1368. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/App/webroot',
  1369. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/App/webroot/index.php',
  1370. 'QUERY_STRING' => '',
  1371. 'REQUEST_URI' => '/index.php/posts/index',
  1372. 'SCRIPT_NAME' => '/index.php',
  1373. 'PATH_INFO' => '/posts/index',
  1374. 'PHP_SELF' => '/index.php/posts/index',
  1375. ),
  1376. ),
  1377. array(
  1378. 'url' => 'posts/index',
  1379. 'base' => '/index.php',
  1380. 'webroot' => '/'
  1381. ),
  1382. ),
  1383. array(
  1384. 'Apache - No rewrite, document root set to webroot, requesting root',
  1385. array(
  1386. 'App' => array(
  1387. 'base' => false,
  1388. 'baseUrl' => '/index.php',
  1389. 'dir' => 'App',
  1390. 'webroot' => 'webroot'
  1391. ),
  1392. 'SERVER' => array(
  1393. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/App/webroot',
  1394. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/App/webroot/index.php',
  1395. 'QUERY_STRING' => '',
  1396. 'REQUEST_URI' => '/index.php',
  1397. 'SCRIPT_NAME' => '/index.php',
  1398. 'PATH_INFO' => '',
  1399. 'PHP_SELF' => '/index.php',
  1400. ),
  1401. ),
  1402. array(
  1403. 'url' => '',
  1404. 'base' => '/index.php',
  1405. 'webroot' => '/'
  1406. ),
  1407. ),
  1408. array(
  1409. 'Apache - No rewrite, document root set above top level cake dir, requesting path',
  1410. array(
  1411. 'App' => array(
  1412. 'base' => false,
  1413. 'baseUrl' => '/site/index.php',
  1414. 'dir' => 'App',
  1415. 'webroot' => 'webroot'
  1416. ),
  1417. 'SERVER' => array(
  1418. 'SERVER_NAME' => 'localhost',
  1419. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1420. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1421. 'REQUEST_URI' => '/site/index.php/posts/index',
  1422. 'SCRIPT_NAME' => '/site/index.php',
  1423. 'PATH_INFO' => '/posts/index',
  1424. 'PHP_SELF' => '/site/index.php/posts/index',
  1425. ),
  1426. ),
  1427. array(
  1428. 'url' => 'posts/index',
  1429. 'base' => '/site/index.php',
  1430. 'webroot' => '/site/App/webroot/',
  1431. ),
  1432. ),
  1433. array(
  1434. 'Apache - No rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1435. array(
  1436. 'App' => array(
  1437. 'base' => false,
  1438. 'baseUrl' => '/site/index.php',
  1439. 'dir' => 'App',
  1440. 'webroot' => 'webroot'
  1441. ),
  1442. 'SERVER' => array(
  1443. 'SERVER_NAME' => 'localhost',
  1444. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1445. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1446. 'REQUEST_URI' => '/site/index.php/',
  1447. 'SCRIPT_NAME' => '/site/index.php',
  1448. 'PHP_SELF' => '/site/index.php/',
  1449. ),
  1450. ),
  1451. array(
  1452. 'url' => '',
  1453. 'base' => '/site/index.php',
  1454. 'webroot' => '/site/App/webroot/',
  1455. ),
  1456. ),
  1457. array(
  1458. 'Apache - No rewrite, document root set above top level cake dir, request path, with GET',
  1459. array(
  1460. 'App' => array(
  1461. 'base' => false,
  1462. 'baseUrl' => '/site/index.php',
  1463. 'dir' => 'App',
  1464. 'webroot' => 'webroot'
  1465. ),
  1466. 'GET' => array('a' => 'b', 'c' => 'd'),
  1467. 'SERVER' => array(
  1468. 'SERVER_NAME' => 'localhost',
  1469. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1470. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1471. 'REQUEST_URI' => '/site/index.php/posts/index?a=b&c=d',
  1472. 'SCRIPT_NAME' => '/site/index.php',
  1473. 'PATH_INFO' => '/posts/index',
  1474. 'PHP_SELF' => '/site/index.php/posts/index',
  1475. 'QUERY_STRING' => 'a=b&c=d'
  1476. ),
  1477. ),
  1478. array(
  1479. 'urlParams' => array('a' => 'b', 'c' => 'd'),
  1480. 'url' => 'posts/index',
  1481. 'base' => '/site/index.php',
  1482. 'webroot' => '/site/App/webroot/',
  1483. ),
  1484. ),
  1485. array(
  1486. 'Apache - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1487. array(
  1488. 'App' => array(
  1489. 'base' => false,
  1490. 'baseUrl' => false,
  1491. 'dir' => 'App',
  1492. 'webroot' => 'webroot'
  1493. ),
  1494. 'SERVER' => array(
  1495. 'SERVER_NAME' => 'localhost',
  1496. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1497. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1498. 'REQUEST_URI' => '/site/',
  1499. 'SCRIPT_NAME' => '/site/App/webroot/index.php',
  1500. 'PHP_SELF' => '/site/App/webroot/index.php',
  1501. ),
  1502. ),
  1503. array(
  1504. 'url' => '',
  1505. 'base' => '/site',
  1506. 'webroot' => '/site/',
  1507. ),
  1508. ),
  1509. array(
  1510. 'Apache - w/rewrite, document root above top level cake dir, request root, no PATH_INFO/REQUEST_URI',
  1511. array(
  1512. 'App' => array(
  1513. 'base' => false,
  1514. 'baseUrl' => false,
  1515. 'dir' => 'App',
  1516. 'webroot' => 'webroot'
  1517. ),
  1518. 'SERVER' => array(
  1519. 'SERVER_NAME' => 'localhost',
  1520. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1521. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1522. 'SCRIPT_NAME' => '/site/App/webroot/index.php',
  1523. 'PHP_SELF' => '/site/App/webroot/index.php',
  1524. 'PATH_INFO' => null,
  1525. 'REQUEST_URI' => null,
  1526. ),
  1527. ),
  1528. array(
  1529. 'url' => '',
  1530. 'base' => '/site',
  1531. 'webroot' => '/site/',
  1532. ),
  1533. ),
  1534. array(
  1535. 'Apache - w/rewrite, document root set to webroot, request root, no PATH_INFO/REQUEST_URI',
  1536. array(
  1537. 'App' => array(
  1538. 'base' => false,
  1539. 'baseUrl' => false,
  1540. 'dir' => 'App',
  1541. 'webroot' => 'webroot'
  1542. ),
  1543. 'SERVER' => array(
  1544. 'SERVER_NAME' => 'localhost',
  1545. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/App/webroot',
  1546. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/App/webroot/index.php',
  1547. 'SCRIPT_NAME' => '/index.php',
  1548. 'PHP_SELF' => '/index.php',
  1549. 'PATH_INFO' => null,
  1550. 'REQUEST_URI' => null,
  1551. ),
  1552. ),
  1553. array(
  1554. 'url' => '',
  1555. 'base' => '',
  1556. 'webroot' => '/',
  1557. ),
  1558. ),
  1559. array(
  1560. 'Apache - w/rewrite, document root set above top level cake dir, request root, absolute REQUEST_URI',
  1561. array(
  1562. 'App' => array(
  1563. 'base' => false,
  1564. 'baseUrl' => false,
  1565. 'dir' => 'App',
  1566. 'webroot' => 'webroot'
  1567. ),
  1568. 'SERVER' => array(
  1569. 'SERVER_NAME' => 'localhost',
  1570. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1571. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1572. 'REQUEST_URI' => '/site/posts/index',
  1573. 'SCRIPT_NAME' => '/site/App/webroot/index.php',
  1574. 'PHP_SELF' => '/site/App/webroot/index.php',
  1575. ),
  1576. ),
  1577. array(
  1578. 'url' => 'posts/index',
  1579. 'base' => '/site',
  1580. 'webroot' => '/site/',
  1581. ),
  1582. ),
  1583. array(
  1584. 'Nginx - w/rewrite, document root set to webroot, request root, no PATH_INFO',
  1585. array(
  1586. 'App' => array(
  1587. 'base' => false,
  1588. 'baseUrl' => false,
  1589. 'dir' => 'App',
  1590. 'webroot' => 'webroot'
  1591. ),
  1592. 'GET' => array('/posts/add' => ''),
  1593. 'SERVER' => array(
  1594. 'SERVER_NAME' => 'localhost',
  1595. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/App/webroot',
  1596. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/App/webroot/index.php',
  1597. 'SCRIPT_NAME' => '/index.php',
  1598. 'QUERY_STRING' => '/posts/add&',
  1599. 'PHP_SELF' => '/index.php',
  1600. 'PATH_INFO' => null,
  1601. 'REQUEST_URI' => '/posts/add',
  1602. ),
  1603. ),
  1604. array(
  1605. 'url' => 'posts/add',
  1606. 'base' => '',
  1607. 'webroot' => '/',
  1608. 'urlParams' => array()
  1609. ),
  1610. ),
  1611. );
  1612. }
  1613. /**
  1614. * Test environment detection
  1615. *
  1616. * @dataProvider environmentGenerator
  1617. * @param $name
  1618. * @param $env
  1619. * @param $expected
  1620. * @return void
  1621. */
  1622. public function testEnvironmentDetection($name, $env, $expected) {
  1623. $_GET = array();
  1624. $this->_loadEnvironment($env);
  1625. $request = Request::createFromGlobals();
  1626. $this->assertEquals($expected['url'], $request->url, "url error");
  1627. $this->assertEquals($expected['base'], $request->base, "base error");
  1628. $this->assertEquals($expected['webroot'], $request->webroot, "webroot error");
  1629. if (isset($expected['urlParams'])) {
  1630. $this->assertEquals($expected['urlParams'], $request->query, "GET param mismatch");
  1631. }
  1632. }
  1633. /**
  1634. * Test the query() method
  1635. *
  1636. * @return void
  1637. */
  1638. public function testQuery() {
  1639. $request = new Request([
  1640. 'query' => ['foo' => 'bar']
  1641. ]);
  1642. $result = $request->query('foo');
  1643. $this->assertEquals('bar', $result);
  1644. $result = $request->query('imaginary');
  1645. $this->assertNull($result);
  1646. }
  1647. /**
  1648. * Test the query() method with arrays passed via $_GET
  1649. *
  1650. * @return void
  1651. */
  1652. public function testQueryWithArray() {
  1653. $get['test'] = array('foo', 'bar');
  1654. $request = new Request([
  1655. 'query' => $get
  1656. ]);
  1657. $result = $request->query('test');
  1658. $this->assertEquals(array('foo', 'bar'), $result);
  1659. $result = $request->query('test.1');
  1660. $this->assertEquals('bar', $result);
  1661. $result = $request->query('test.2');
  1662. $this->assertNull($result);
  1663. }
  1664. /**
  1665. * Test using param()
  1666. *
  1667. * @return void
  1668. */
  1669. public function testReadingParams() {
  1670. $request = new Request();
  1671. $request->addParams(array(
  1672. 'controller' => 'posts',
  1673. 'admin' => true,
  1674. 'truthy' => 1,
  1675. 'zero' => '0',
  1676. ));
  1677. $this->assertFalse($request->param('not_set'));
  1678. $this->assertTrue($request->param('admin'));
  1679. $this->assertEquals(1, $request->param('truthy'));
  1680. $this->assertEquals('posts', $request->param('controller'));
  1681. $this->assertEquals('0', $request->param('zero'));
  1682. }
  1683. /**
  1684. * Test the data() method reading
  1685. *
  1686. * @return void
  1687. */
  1688. public function testDataReading() {
  1689. $post = array(
  1690. 'Model' => array(
  1691. 'field' => 'value'
  1692. )
  1693. );
  1694. $request = new Request(compact('post'));
  1695. $result = $request->data('Model');
  1696. $this->assertEquals($post['Model'], $result);
  1697. $result = $request->data('Model.imaginary');
  1698. $this->assertNull($result);
  1699. }
  1700. /**
  1701. * Test writing with data()
  1702. *
  1703. * @return void
  1704. */
  1705. public function testDataWriting() {
  1706. $_POST['data'] = array(
  1707. 'Model' => array(
  1708. 'field' => 'value'
  1709. )
  1710. );
  1711. $request = new Request();
  1712. $result = $request->data('Model.new_value', 'new value');
  1713. $this->assertSame($result, $request, 'Return was not $this');
  1714. $this->assertEquals('new value', $request->data['Model']['new_value']);
  1715. $request->data('Post.title', 'New post')->data('Comment.1.author', 'Mark');
  1716. $this->assertEquals('New post', $request->data['Post']['title']);
  1717. $this->assertEquals('Mark', $request->data['Comment']['1']['author']);
  1718. }
  1719. /**
  1720. * Test writing falsey values.
  1721. *
  1722. * @return void
  1723. */
  1724. public function testDataWritingFalsey() {
  1725. $request = new Request();
  1726. $request->data('Post.null', null);
  1727. $this->assertNull($request->data['Post']['null']);
  1728. $request->data('Post.false', false);
  1729. $this->assertFalse($request->data['Post']['false']);
  1730. $request->data('Post.zero', 0);
  1731. $this->assertSame(0, $request->data['Post']['zero']);
  1732. $request->data('Post.empty', '');
  1733. $this->assertSame('', $request->data['Post']['empty']);
  1734. }
  1735. /**
  1736. * Test accept language
  1737. *
  1738. * @return void
  1739. */
  1740. public function testAcceptLanguage() {
  1741. // Weird language
  1742. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
  1743. $result = Request::acceptLanguage();
  1744. $this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
  1745. // No qualifier
  1746. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
  1747. $result = Request::acceptLanguage();
  1748. $this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
  1749. // With qualifier
  1750. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4';
  1751. $result = Request::acceptLanguage();
  1752. $this->assertEquals(array('en-us', 'en', 'pt-br', 'pt'), $result, 'Languages do not match');
  1753. // With spaces
  1754. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'da, en-gb;q=0.8, en;q=0.7';
  1755. $result = Request::acceptLanguage();
  1756. $this->assertEquals(array('da', 'en-gb', 'en'), $result, 'Languages do not match');
  1757. // Checking if requested
  1758. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
  1759. $result = Request::acceptLanguage();
  1760. $result = Request::acceptLanguage('en-ca');
  1761. $this->assertTrue($result);
  1762. $result = Request::acceptLanguage('en-CA');
  1763. $this->assertTrue($result);
  1764. $result = Request::acceptLanguage('en-us');
  1765. $this->assertFalse($result);
  1766. $result = Request::acceptLanguage('en-US');
  1767. $this->assertFalse($result);
  1768. }
  1769. /**
  1770. * Test the here() method
  1771. *
  1772. * @return void
  1773. */
  1774. public function testHere() {
  1775. Configure::write('App.base', '/base_path');
  1776. $q = array('test' => 'value');
  1777. $request = new Request(array(
  1778. 'query' => $q,
  1779. 'url' => '/posts/add/1/value',
  1780. 'base' => '/base_path'
  1781. ));
  1782. $result = $request->here();
  1783. $this->assertEquals('/base_path/posts/add/1/value?test=value', $result);
  1784. $result = $request->here(false);
  1785. $this->assertEquals('/posts/add/1/value?test=value', $result);
  1786. $request = new Request(array(
  1787. 'url' => '/posts/base_path/1/value',
  1788. 'query' => array('test' => 'value'),
  1789. 'base' => '/base_path'
  1790. ));
  1791. $result = $request->here();
  1792. $this->assertEquals('/base_path/posts/base_path/1/value?test=value', $result);
  1793. $result = $request->here(false);
  1794. $this->assertEquals('/posts/base_path/1/value?test=value', $result);
  1795. }
  1796. /**
  1797. * Test the input() method.
  1798. *
  1799. * @return void
  1800. */
  1801. public function testInput() {
  1802. $request = $this->getMock('Cake\Network\Request', array('_readInput'));
  1803. $request->expects($this->once())->method('_readInput')
  1804. ->will($this->returnValue('I came from stdin'));
  1805. $result = $request->input();
  1806. $this->assertEquals('I came from stdin', $result);
  1807. }
  1808. /**
  1809. * Test input() decoding.
  1810. *
  1811. * @return void
  1812. */
  1813. public function testInputDecode() {
  1814. $request = $this->getMock('Cake\Network\Request', array('_readInput'));
  1815. $request->expects($this->once())->method('_readInput')
  1816. ->will($this->returnValue('{"name":"value"}'));
  1817. $result = $request->input('json_decode');
  1818. $this->assertEquals(array('name' => 'value'), (array)$result);
  1819. }
  1820. /**
  1821. * Test input() decoding with additional arguments.
  1822. *
  1823. * @return void
  1824. */
  1825. public function testInputDecodeExtraParams() {
  1826. $xml = <<<XML
  1827. <?xml version="1.0" encoding="utf-8"?>
  1828. <post>
  1829. <title id="title">Test</title>
  1830. </post>
  1831. XML;
  1832. $request = $this->getMock('Cake\Network\Request', array('_readInput'));
  1833. $request->expects($this->once())->method('_readInput')
  1834. ->will($this->returnValue($xml));
  1835. $result = $request->input('Cake\Utility\Xml::build', array('return' => 'domdocument'));
  1836. $this->assertInstanceOf('DOMDocument', $result);
  1837. $this->assertEquals(
  1838. 'Test',
  1839. $result->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText
  1840. );
  1841. }
  1842. /**
  1843. * Test is('requested') and isRequested()
  1844. *
  1845. * @return void
  1846. */
  1847. public function testIsRequested() {
  1848. $request = new Request();
  1849. $request->addParams(array(
  1850. 'controller' => 'posts',
  1851. 'action' => 'index',
  1852. 'plugin' => null,
  1853. 'requested' => 1
  1854. ));
  1855. $this->assertTrue($request->is('requested'));
  1856. $this->assertTrue($request->isRequested());
  1857. $request = new Request();
  1858. $request->addParams(array(
  1859. 'controller' => 'posts',
  1860. 'action' => 'index',
  1861. 'plugin' => null,
  1862. ));
  1863. $this->assertFalse($request->is('requested'));
  1864. $this->assertFalse($request->isRequested());
  1865. }
  1866. /**
  1867. * Test the cookie() method.
  1868. *
  1869. * @return void
  1870. */
  1871. public function testReadCookie() {
  1872. $request = new Request(array(
  1873. 'cookies' => array(
  1874. 'testing' => 'A value in the cookie'
  1875. )
  1876. ));
  1877. $result = $request->cookie('testing');
  1878. $this->assertEquals('A value in the cookie', $result);
  1879. $result = $request->cookie('not there');
  1880. $this->assertNull($result);
  1881. }
  1882. /**
  1883. * TestOnlyAllow
  1884. *
  1885. * @return void
  1886. */
  1887. public function testOnlyAllow() {
  1888. $_SERVER['REQUEST_METHOD'] = 'PUT';
  1889. $request = new Request('/posts/edit/1');
  1890. $this->assertTrue($request->onlyAllow(array('put')));
  1891. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  1892. $this->assertTrue($request->onlyAllow('post', 'delete'));
  1893. }
  1894. /**
  1895. * Test onlyAllow throwing exception
  1896. *
  1897. * @return void
  1898. */
  1899. public function testOnlyAllowException() {
  1900. $_SERVER['REQUEST_METHOD'] = 'PUT';
  1901. $request = new Request('/posts/edit/1');
  1902. try {
  1903. $request->onlyAllow('POST', 'DELETE');
  1904. $this->fail('An expected exception has not been raised.');
  1905. } catch (Error\MethodNotAllowedException $e) {
  1906. $this->assertEquals(array('Allow' => 'POST, DELETE'), $e->responseHeader());
  1907. }
  1908. $this->setExpectedException('Cake\Error\MethodNotAllowedException');
  1909. $request->onlyAllow('POST');
  1910. }
  1911. /**
  1912. * loadEnvironment method
  1913. *
  1914. * @param array $env
  1915. * @return void
  1916. */
  1917. protected function _loadEnvironment($env) {
  1918. if (isset($env['App'])) {
  1919. Configure::write('App', $env['App']);
  1920. }
  1921. if (isset($env['GET'])) {
  1922. foreach ($env['GET'] as $key => $val) {
  1923. $_GET[$key] = $val;
  1924. }
  1925. }
  1926. if (isset($env['POST'])) {
  1927. foreach ($env['POST'] as $key => $val) {
  1928. $_POST[$key] = $val;
  1929. }
  1930. }
  1931. if (isset($env['SERVER'])) {
  1932. foreach ($env['SERVER'] as $key => $val) {
  1933. $_SERVER[$key] = $val;
  1934. }
  1935. }
  1936. }
  1937. }