RequestTest.php 62 KB

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