RequestTest.php 62 KB

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