RequestTest.php 64 KB

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