ServerRequestTest.php 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 2.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Http;
  17. use Cake\Core\Configure;
  18. use Cake\Http\Cookie\Cookie;
  19. use Cake\Http\Cookie\CookieCollection;
  20. use Cake\Http\Exception\MethodNotAllowedException;
  21. use Cake\Http\ServerRequest;
  22. use Cake\Http\Session;
  23. use Cake\TestSuite\TestCase;
  24. use Laminas\Diactoros\UploadedFile;
  25. use Laminas\Diactoros\Uri;
  26. /**
  27. * ServerRequest Test
  28. */
  29. class ServerRequestTest extends TestCase
  30. {
  31. /**
  32. * Test custom detector with extra arguments.
  33. *
  34. * @return void
  35. */
  36. public function testCustomArgsDetector()
  37. {
  38. $request = new ServerRequest();
  39. $request->addDetector('controller', function ($request, $name) {
  40. return $request->getParam('controller') === $name;
  41. });
  42. $request = $request->withParam('controller', 'cake');
  43. $this->assertTrue($request->is('controller', 'cake'));
  44. $this->assertFalse($request->is('controller', 'nonExistingController'));
  45. $this->assertTrue($request->isController('cake'));
  46. $this->assertFalse($request->isController('nonExistingController'));
  47. }
  48. /**
  49. * Test the header detector.
  50. *
  51. * @return void
  52. */
  53. public function testHeaderDetector()
  54. {
  55. $request = new ServerRequest();
  56. $request->addDetector('host', ['header' => ['host' => 'cakephp.org']]);
  57. $request = $request->withEnv('HTTP_HOST', 'cakephp.org');
  58. $this->assertTrue($request->is('host'));
  59. $request = $request->withEnv('HTTP_HOST', 'php.net');
  60. $this->assertFalse($request->is('host'));
  61. }
  62. /**
  63. * Test the accept header detector.
  64. *
  65. * @return void
  66. */
  67. public function testExtensionDetector()
  68. {
  69. $request = new ServerRequest();
  70. $request = $request->withParam('_ext', 'json');
  71. $this->assertTrue($request->is('json'));
  72. $request = new ServerRequest();
  73. $request = $request->withParam('_ext', 'xml');
  74. $this->assertFalse($request->is('json'));
  75. }
  76. /**
  77. * Test the accept header detector.
  78. *
  79. * @return void
  80. */
  81. public function testAcceptHeaderDetector()
  82. {
  83. $request = new ServerRequest();
  84. $request = $request->withEnv('HTTP_ACCEPT', 'application/json, text/plain, */*');
  85. $this->assertTrue($request->is('json'));
  86. $request = new ServerRequest();
  87. $request = $request->withEnv('HTTP_ACCEPT', 'text/plain, */*');
  88. $this->assertFalse($request->is('json'));
  89. }
  90. /**
  91. * Test construction
  92. *
  93. * @return void
  94. */
  95. public function testConstructionQueryData()
  96. {
  97. $data = [
  98. 'query' => [
  99. 'one' => 'param',
  100. 'two' => 'banana',
  101. ],
  102. 'url' => 'some/path',
  103. ];
  104. $request = new ServerRequest($data);
  105. $this->assertSame('param', $request->getQuery('one'));
  106. $this->assertEquals($data['query'], $request->getQueryParams());
  107. $this->assertSame('/some/path', $request->getRequestTarget());
  108. }
  109. /**
  110. * Test constructing with a string url.
  111. *
  112. * @return void
  113. */
  114. public function testConstructStringUrlIgnoreServer()
  115. {
  116. $request = new ServerRequest([
  117. 'url' => '/articles/view/1',
  118. 'environment' => ['REQUEST_URI' => '/some/other/path'],
  119. ]);
  120. $this->assertSame('/articles/view/1', $request->getUri()->getPath());
  121. $request = new ServerRequest(['url' => '/']);
  122. $this->assertSame('/', $request->getUri()->getPath());
  123. }
  124. /**
  125. * Test that querystring args provided in the URL string are parsed.
  126. *
  127. * @return void
  128. */
  129. public function testQueryStringParsingFromInputUrl()
  130. {
  131. $request = new ServerRequest(['url' => 'some/path?one=something&two=else']);
  132. $expected = ['one' => 'something', 'two' => 'else'];
  133. $this->assertEquals($expected, $request->getQueryParams());
  134. $this->assertSame('/some/path', $request->getUri()->getPath());
  135. $this->assertSame('one=something&two=else', $request->getUri()->getQuery());
  136. }
  137. /**
  138. * Test that querystrings are handled correctly.
  139. *
  140. * @return void
  141. */
  142. public function testQueryStringAndNamedParams()
  143. {
  144. $config = ['environment' => ['REQUEST_URI' => '/tasks/index?ts=123456']];
  145. $request = new ServerRequest($config);
  146. $this->assertSame('/tasks/index', $request->getRequestTarget());
  147. $config = ['environment' => ['REQUEST_URI' => '/some/path?url=http://cakephp.org']];
  148. $request = new ServerRequest($config);
  149. $this->assertSame('/some/path', $request->getRequestTarget());
  150. $config = ['environment' => [
  151. 'REQUEST_URI' => Configure::read('App.fullBaseUrl') . '/other/path?url=http://cakephp.org',
  152. ]];
  153. $request = new ServerRequest($config);
  154. $this->assertSame('/other/path', $request->getRequestTarget());
  155. }
  156. /**
  157. * Test that URL in path is handled correctly.
  158. */
  159. public function testUrlInPath()
  160. {
  161. $config = ['environment' => ['REQUEST_URI' => '/jump/http://cakephp.org']];
  162. $request = new ServerRequest($config);
  163. $this->assertSame('/jump/http://cakephp.org', $request->getRequestTarget());
  164. $config = ['environment' => [
  165. 'REQUEST_URI' => Configure::read('App.fullBaseUrl') . '/jump/http://cakephp.org',
  166. ]];
  167. $request = new ServerRequest($config);
  168. $this->assertSame('/jump/http://cakephp.org', $request->getRequestTarget());
  169. }
  170. /**
  171. * Test getPath().
  172. *
  173. * @return void
  174. */
  175. public function testGetPath()
  176. {
  177. $request = new ServerRequest(['url' => '']);
  178. $this->assertSame('/', $request->getPath());
  179. $request = new ServerRequest(['url' => 'some/path?one=something&two=else']);
  180. $this->assertSame('/some/path', $request->getPath());
  181. $request = $request->withRequestTarget('/foo/bar?x=y');
  182. $this->assertSame('/foo/bar', $request->getPath());
  183. }
  184. /**
  185. * Test parsing POST data into the object.
  186. *
  187. * @return void
  188. */
  189. public function testPostParsing()
  190. {
  191. $post = [
  192. 'Article' => ['title'],
  193. ];
  194. $request = new ServerRequest(compact('post'));
  195. $this->assertEquals($post, $request->getData());
  196. $post = ['one' => 1, 'two' => 'three'];
  197. $request = new ServerRequest(compact('post'));
  198. $this->assertEquals($post, $request->getData());
  199. $post = [
  200. 'Article' => ['title' => 'Testing'],
  201. 'action' => 'update',
  202. ];
  203. $request = new ServerRequest(compact('post'));
  204. $this->assertEquals($post, $request->getData());
  205. }
  206. /**
  207. * Test parsing JSON PUT data into the object.
  208. *
  209. * @return void
  210. * @group deprecated
  211. */
  212. public function testPutParsingJSON()
  213. {
  214. $data = '{"Article":["title"]}';
  215. $request = new ServerRequest([
  216. 'input' => $data,
  217. 'environment' => [
  218. 'REQUEST_METHOD' => 'PUT',
  219. 'CONTENT_TYPE' => 'application/json',
  220. ],
  221. ]);
  222. $this->assertEquals([], $request->getData());
  223. $this->deprecated(function () use ($request) {
  224. $result = $request->input('json_decode', true);
  225. $this->assertEquals(['title'], $result['Article']);
  226. });
  227. }
  228. /**
  229. * Test that the constructor uses uploaded file objects
  230. * if they are present. This could happen in test scenarios.
  231. *
  232. * @return void
  233. */
  234. public function testFilesObject()
  235. {
  236. $file = new UploadedFile(
  237. __FILE__,
  238. 123,
  239. UPLOAD_ERR_OK,
  240. 'test.php',
  241. 'text/plain'
  242. );
  243. $request = new ServerRequest(['files' => ['avatar' => $file]]);
  244. $this->assertSame(['avatar' => $file], $request->getUploadedFiles());
  245. }
  246. /**
  247. * Test passing an empty files list.
  248. *
  249. * @return void
  250. */
  251. public function testFilesWithEmptyList()
  252. {
  253. $request = new ServerRequest([
  254. 'files' => [],
  255. ]);
  256. $this->assertEmpty($request->getData());
  257. $this->assertEmpty($request->getUploadedFiles());
  258. }
  259. /**
  260. * Test replacing files.
  261. *
  262. * @return void
  263. */
  264. public function testWithUploadedFiles()
  265. {
  266. $file = new UploadedFile(
  267. __FILE__,
  268. 123,
  269. UPLOAD_ERR_OK,
  270. 'test.php',
  271. 'text/plain'
  272. );
  273. $request = new ServerRequest();
  274. $new = $request->withUploadedFiles(['picture' => $file]);
  275. $this->assertSame([], $request->getUploadedFiles());
  276. $this->assertNotSame($new, $request);
  277. $this->assertSame(['picture' => $file], $new->getUploadedFiles());
  278. }
  279. /**
  280. * Test getting a single file
  281. *
  282. * @return void
  283. */
  284. public function testGetUploadedFile()
  285. {
  286. $file = new UploadedFile(
  287. __FILE__,
  288. 123,
  289. UPLOAD_ERR_OK,
  290. 'test.php',
  291. 'text/plain'
  292. );
  293. $request = new ServerRequest();
  294. $new = $request->withUploadedFiles(['picture' => $file]);
  295. $this->assertNull($new->getUploadedFile(''));
  296. $this->assertSame($file, $new->getUploadedFile('picture'));
  297. $new = $request->withUploadedFiles([
  298. 'pictures' => [
  299. [
  300. 'image' => $file,
  301. ],
  302. ],
  303. ]);
  304. $this->assertNull($new->getUploadedFile('pictures'));
  305. $this->assertNull($new->getUploadedFile('pictures.0'));
  306. $this->assertNull($new->getUploadedFile('pictures.1'));
  307. $this->assertSame($file, $new->getUploadedFile('pictures.0.image'));
  308. }
  309. /**
  310. * Test replacing files with an invalid file
  311. *
  312. * @return void
  313. */
  314. public function testWithUploadedFilesInvalidFile()
  315. {
  316. $this->expectException(\InvalidArgumentException::class);
  317. $this->expectExceptionMessage('Invalid file at \'avatar\'');
  318. $request = new ServerRequest();
  319. $request->withUploadedFiles(['avatar' => 'not a file']);
  320. }
  321. /**
  322. * Test replacing files with an invalid file
  323. *
  324. * @return void
  325. */
  326. public function testWithUploadedFilesInvalidFileNested()
  327. {
  328. $this->expectException(\InvalidArgumentException::class);
  329. $this->expectExceptionMessage('Invalid file at \'user.avatar\'');
  330. $request = new ServerRequest();
  331. $request->withUploadedFiles(['user' => ['avatar' => 'not a file']]);
  332. }
  333. /**
  334. * Test the clientIp method.
  335. *
  336. * @return void
  337. */
  338. public function testClientIp()
  339. {
  340. $request = new ServerRequest(['environment' => [
  341. 'HTTP_X_FORWARDED_FOR' => '192.168.1.5, 10.0.1.1, proxy.com, real.ip',
  342. 'HTTP_X_REAL_IP' => '192.168.1.1',
  343. 'HTTP_CLIENT_IP' => '192.168.1.2',
  344. 'REMOTE_ADDR' => '192.168.1.3',
  345. ]]);
  346. $request->trustProxy = true;
  347. $this->assertSame('real.ip', $request->clientIp());
  348. $request = $request->withEnv('HTTP_X_FORWARDED_FOR', '');
  349. $this->assertSame('192.168.1.1', $request->clientIp());
  350. $request = $request->withEnv('HTTP_X_REAL_IP', '');
  351. $this->assertSame('192.168.1.2', $request->clientIp());
  352. $request->trustProxy = false;
  353. $this->assertSame('192.168.1.3', $request->clientIp());
  354. $request = $request->withEnv('HTTP_X_FORWARDED_FOR', '');
  355. $this->assertSame('192.168.1.3', $request->clientIp());
  356. $request = $request->withEnv('HTTP_CLIENT_IP', '');
  357. $this->assertSame('192.168.1.3', $request->clientIp());
  358. }
  359. /**
  360. * test clientIp method with trusted proxies
  361. *
  362. * @return void
  363. */
  364. public function testClientIpWithTrustedProxies()
  365. {
  366. $request = new ServerRequest(['environment' => [
  367. 'HTTP_X_FORWARDED_FOR' => 'real.ip, 192.168.1.0, 192.168.1.2, 192.168.1.3',
  368. 'HTTP_X_REAL_IP' => '192.168.1.1',
  369. 'HTTP_CLIENT_IP' => '192.168.1.2',
  370. 'REMOTE_ADDR' => '192.168.1.4',
  371. ]]);
  372. $request->setTrustedProxies([
  373. '192.168.1.0',
  374. '192.168.1.1',
  375. '192.168.1.2',
  376. '192.168.1.3',
  377. ]);
  378. $this->assertSame('real.ip', $request->clientIp());
  379. $request = $request->withEnv(
  380. 'HTTP_X_FORWARDED_FOR',
  381. 'spoof.fake.ip, real.ip, 192.168.1.0, 192.168.1.2, 192.168.1.3'
  382. );
  383. $this->assertSame('192.168.1.3', $request->clientIp());
  384. $request = $request->withEnv('HTTP_X_FORWARDED_FOR', '');
  385. $this->assertSame('192.168.1.1', $request->clientIp());
  386. $request->trustProxy = false;
  387. $this->assertSame('192.168.1.4', $request->clientIp());
  388. }
  389. /**
  390. * Test the referrer function.
  391. *
  392. * @return void
  393. */
  394. public function testReferer()
  395. {
  396. $request = new ServerRequest(['webroot' => '/']);
  397. $request = $request->withEnv('HTTP_REFERER', 'http://cakephp.org');
  398. $result = $request->referer(false);
  399. $this->assertSame('http://cakephp.org', $result);
  400. $request = $request->withEnv('HTTP_REFERER', '');
  401. $result = $request->referer(true);
  402. $this->assertNull($result);
  403. $result = $request->referer(false);
  404. $this->assertNull($result);
  405. $request = $request->withEnv('HTTP_REFERER', Configure::read('App.fullBaseUrl') . '/some/path');
  406. $result = $request->referer();
  407. $this->assertSame('/some/path', $result);
  408. $request = $request->withEnv('HTTP_REFERER', Configure::read('App.fullBaseUrl') . '///cakephp.org/');
  409. $result = $request->referer();
  410. $this->assertSame('/', $result); // Avoid returning scheme-relative URLs.
  411. $request = $request->withEnv('HTTP_REFERER', Configure::read('App.fullBaseUrl') . '/0');
  412. $result = $request->referer();
  413. $this->assertSame('/0', $result);
  414. $request = $request->withEnv('HTTP_REFERER', Configure::read('App.fullBaseUrl') . '/');
  415. $result = $request->referer();
  416. $this->assertSame('/', $result);
  417. $request = $request->withEnv('HTTP_REFERER', Configure::read('App.fullBaseUrl') . '/some/path');
  418. $result = $request->referer(false);
  419. $this->assertSame(Configure::read('App.fullBaseUrl') . '/some/path', $result);
  420. }
  421. /**
  422. * Test referer() with a base path that duplicates the
  423. * first segment.
  424. *
  425. * @return void
  426. */
  427. public function testRefererBasePath()
  428. {
  429. $request = new ServerRequest([
  430. 'url' => '/waves/users/login',
  431. 'webroot' => '/waves/',
  432. 'base' => '/waves',
  433. ]);
  434. $request = $request->withEnv('HTTP_REFERER', Configure::read('App.fullBaseUrl') . '/waves/waves/add');
  435. $result = $request->referer();
  436. $this->assertSame('/waves/add', $result);
  437. }
  438. /**
  439. * test the simple uses of is()
  440. *
  441. * @return void
  442. */
  443. public function testIsHttpMethods()
  444. {
  445. $request = new ServerRequest();
  446. $this->assertFalse($request->is('undefined-behavior'));
  447. $request = $request->withEnv('REQUEST_METHOD', 'GET');
  448. $this->assertTrue($request->is('get'));
  449. $request = $request->withEnv('REQUEST_METHOD', 'POST');
  450. $this->assertTrue($request->is('POST'));
  451. $request = $request->withEnv('REQUEST_METHOD', 'PUT');
  452. $this->assertTrue($request->is('put'));
  453. $this->assertFalse($request->is('get'));
  454. $request = $request->withEnv('REQUEST_METHOD', 'DELETE');
  455. $this->assertTrue($request->is('delete'));
  456. $this->assertTrue($request->isDelete());
  457. $request = $request->withEnv('REQUEST_METHOD', 'delete');
  458. $this->assertFalse($request->is('delete'));
  459. }
  460. /**
  461. * Test is() with JSON and XML.
  462. *
  463. * @return void
  464. */
  465. public function testIsJsonAndXml()
  466. {
  467. $request = new ServerRequest();
  468. $request = $request->withEnv('HTTP_ACCEPT', 'application/json, text/plain, */*');
  469. $this->assertTrue($request->is('json'));
  470. $request = new ServerRequest();
  471. $request = $request->withEnv('HTTP_ACCEPT', 'application/xml, text/plain, */*');
  472. $this->assertTrue($request->is('xml'));
  473. $request = new ServerRequest();
  474. $request = $request->withEnv('HTTP_ACCEPT', 'text/xml, */*');
  475. $this->assertTrue($request->is('xml'));
  476. }
  477. /**
  478. * Test is() with multiple types.
  479. *
  480. * @return void
  481. */
  482. public function testIsMultiple()
  483. {
  484. $request = new ServerRequest();
  485. $request = $request->withEnv('REQUEST_METHOD', 'GET');
  486. $this->assertTrue($request->is(['get', 'post']));
  487. $request = $request->withEnv('REQUEST_METHOD', 'POST');
  488. $this->assertTrue($request->is(['get', 'post']));
  489. $request = $request->withEnv('REQUEST_METHOD', 'PUT');
  490. $this->assertFalse($request->is(['get', 'post']));
  491. }
  492. /**
  493. * Test isAll()
  494. *
  495. * @return void
  496. */
  497. public function testIsAll()
  498. {
  499. $request = new ServerRequest();
  500. $request = $request->withEnv('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
  501. $request = $request->withEnv('REQUEST_METHOD', 'GET');
  502. $this->assertTrue($request->isAll(['ajax', 'get']));
  503. $this->assertFalse($request->isAll(['post', 'get']));
  504. $this->assertFalse($request->isAll(['ajax', 'post']));
  505. }
  506. /**
  507. * Test getMethod()
  508. *
  509. * @return void
  510. */
  511. public function testGetMethod()
  512. {
  513. $request = new ServerRequest([
  514. 'environment' => ['REQUEST_METHOD' => 'delete'],
  515. ]);
  516. $this->assertSame('delete', $request->getMethod());
  517. }
  518. /**
  519. * Test withMethod()
  520. *
  521. * @return void
  522. */
  523. public function testWithMethod()
  524. {
  525. $request = new ServerRequest([
  526. 'environment' => ['REQUEST_METHOD' => 'delete'],
  527. ]);
  528. $new = $request->withMethod('put');
  529. $this->assertNotSame($new, $request);
  530. $this->assertSame('delete', $request->getMethod());
  531. $this->assertSame('put', $new->getMethod());
  532. }
  533. /**
  534. * Test withMethod() and invalid data
  535. *
  536. * @return void
  537. */
  538. public function testWithMethodInvalid()
  539. {
  540. $this->expectException(\InvalidArgumentException::class);
  541. $this->expectExceptionMessage('Unsupported HTTP method "no good" provided');
  542. $request = new ServerRequest([
  543. 'environment' => ['REQUEST_METHOD' => 'delete'],
  544. ]);
  545. $request->withMethod('no good');
  546. }
  547. /**
  548. * Test getProtocolVersion()
  549. *
  550. * @return void
  551. */
  552. public function testGetProtocolVersion()
  553. {
  554. $request = new ServerRequest();
  555. $this->assertSame('1.1', $request->getProtocolVersion());
  556. // SERVER var.
  557. $request = new ServerRequest([
  558. 'environment' => ['SERVER_PROTOCOL' => 'HTTP/1.0'],
  559. ]);
  560. $this->assertSame('1.0', $request->getProtocolVersion());
  561. }
  562. /**
  563. * Test withProtocolVersion()
  564. *
  565. * @return void
  566. */
  567. public function testWithProtocolVersion()
  568. {
  569. $request = new ServerRequest();
  570. $new = $request->withProtocolVersion('1.0');
  571. $this->assertNotSame($new, $request);
  572. $this->assertSame('1.1', $request->getProtocolVersion());
  573. $this->assertSame('1.0', $new->getProtocolVersion());
  574. }
  575. /**
  576. * Test withProtocolVersion() and invalid data
  577. *
  578. * @return void
  579. */
  580. public function testWithProtocolVersionInvalid()
  581. {
  582. $this->expectException(\InvalidArgumentException::class);
  583. $this->expectExceptionMessage('Unsupported protocol version \'no good\' provided');
  584. $request = new ServerRequest();
  585. $request->withProtocolVersion('no good');
  586. }
  587. /**
  588. * Test host retrieval.
  589. *
  590. * @return void
  591. */
  592. public function testHost()
  593. {
  594. $request = new ServerRequest(['environment' => [
  595. 'HTTP_HOST' => 'localhost',
  596. 'HTTP_X_FORWARDED_HOST' => 'cakephp.org',
  597. ]]);
  598. $this->assertSame('localhost', $request->host());
  599. $request->trustProxy = true;
  600. $this->assertSame('cakephp.org', $request->host());
  601. }
  602. /**
  603. * test port retrieval.
  604. *
  605. * @return void
  606. */
  607. public function testPort()
  608. {
  609. $request = new ServerRequest(['environment' => ['SERVER_PORT' => '80']]);
  610. $this->assertSame('80', $request->port());
  611. $request = $request->withEnv('SERVER_PORT', '443');
  612. $request = $request->withEnv('HTTP_X_FORWARDED_PORT', '80');
  613. $this->assertSame('443', $request->port());
  614. $request->trustProxy = true;
  615. $this->assertSame('80', $request->port());
  616. }
  617. /**
  618. * test domain retrieval.
  619. *
  620. * @return void
  621. */
  622. public function testDomain()
  623. {
  624. $request = new ServerRequest(['environment' => ['HTTP_HOST' => 'something.example.com']]);
  625. $this->assertSame('example.com', $request->domain());
  626. $request = $request->withEnv('HTTP_HOST', 'something.example.co.uk');
  627. $this->assertSame('example.co.uk', $request->domain(2));
  628. }
  629. /**
  630. * Test scheme() method.
  631. *
  632. * @return void
  633. */
  634. public function testScheme()
  635. {
  636. $request = new ServerRequest(['environment' => ['HTTPS' => 'on']]);
  637. $this->assertSame('https', $request->scheme());
  638. $request = $request->withEnv('HTTPS', '');
  639. $this->assertSame('http', $request->scheme());
  640. $request = $request->withEnv('HTTP_X_FORWARDED_PROTO', 'https');
  641. $request->trustProxy = true;
  642. $this->assertSame('https', $request->scheme());
  643. }
  644. /**
  645. * test getting subdomains for a host.
  646. *
  647. * @return void
  648. */
  649. public function testSubdomain()
  650. {
  651. $request = new ServerRequest(['environment' => ['HTTP_HOST' => 'something.example.com']]);
  652. $this->assertEquals(['something'], $request->subdomains());
  653. $request = $request->withEnv('HTTP_HOST', 'www.something.example.com');
  654. $this->assertEquals(['www', 'something'], $request->subdomains());
  655. $request = $request->withEnv('HTTP_HOST', 'www.something.example.co.uk');
  656. $this->assertEquals(['www', 'something'], $request->subdomains(2));
  657. $request = $request->withEnv('HTTP_HOST', 'example.co.uk');
  658. $this->assertEquals([], $request->subdomains(2));
  659. }
  660. /**
  661. * Test AJAX, flash and friends
  662. *
  663. * @return void
  664. */
  665. public function testisAjax()
  666. {
  667. $request = new ServerRequest();
  668. $request = $request->withEnv('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
  669. $this->assertTrue($request->is('ajax'));
  670. $request = $request->withEnv('HTTP_X_REQUESTED_WITH', 'XMLHTTPREQUEST');
  671. $this->assertFalse($request->is('ajax'));
  672. $this->assertFalse($request->isAjax());
  673. }
  674. /**
  675. * Test __call exceptions
  676. *
  677. * @return void
  678. */
  679. public function testMagicCallExceptionOnUnknownMethod()
  680. {
  681. $this->expectException(\BadMethodCallException::class);
  682. $request = new ServerRequest();
  683. $request->IamABanana();
  684. }
  685. /**
  686. * Test is(ssl)
  687. *
  688. * @return void
  689. */
  690. public function testIsSsl()
  691. {
  692. $request = new ServerRequest();
  693. $request = $request->withEnv('HTTPS', 'on');
  694. $this->assertTrue($request->is('ssl'));
  695. $request = $request->withEnv('HTTPS', '1');
  696. $this->assertTrue($request->is('ssl'));
  697. $request = $request->withEnv('HTTPS', 'I am not empty');
  698. $this->assertFalse($request->is('ssl'));
  699. $request = $request->withEnv('HTTPS', 'off');
  700. $this->assertFalse($request->is('ssl'));
  701. $request = $request->withEnv('HTTPS', '');
  702. $this->assertFalse($request->is('ssl'));
  703. }
  704. /**
  705. * Test adding detectors and having them work.
  706. *
  707. * @return void
  708. */
  709. public function testAddDetector()
  710. {
  711. $request = new ServerRequest();
  712. ServerRequest::addDetector('closure', function ($request) {
  713. return true;
  714. });
  715. $this->assertTrue($request->is('closure'));
  716. ServerRequest::addDetector('get', function ($request) {
  717. return $request->getEnv('REQUEST_METHOD') === 'GET';
  718. });
  719. $request = $request->withEnv('REQUEST_METHOD', 'GET');
  720. $this->assertTrue($request->is('get'));
  721. ServerRequest::addDetector('compare', ['env' => 'TEST_VAR', 'value' => 'something']);
  722. $request = $request->withEnv('TEST_VAR', 'something');
  723. $this->assertTrue($request->is('compare'), 'Value match failed.');
  724. $request = $request->withEnv('TEST_VAR', 'wrong');
  725. $this->assertFalse($request->is('compare'), 'Value mis-match failed.');
  726. ServerRequest::addDetector('compareCamelCase', ['env' => 'TEST_VAR', 'value' => 'foo']);
  727. $request = $request->withEnv('TEST_VAR', 'foo');
  728. $this->assertTrue($request->is('compareCamelCase'), 'Value match failed.');
  729. $this->assertTrue($request->is('comparecamelcase'), 'detectors should be case insensitive');
  730. $this->assertTrue($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
  731. $request = $request->withEnv('TEST_VAR', 'not foo');
  732. $this->assertFalse($request->is('compareCamelCase'), 'Value match failed.');
  733. $this->assertFalse($request->is('comparecamelcase'), 'detectors should be case insensitive');
  734. $this->assertFalse($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
  735. ServerRequest::addDetector('banana', ['env' => 'TEST_VAR', 'pattern' => '/^ban.*$/']);
  736. $request = $request->withEnv('TEST_VAR', 'banana');
  737. $this->assertTrue($request->isBanana());
  738. $request = $request->withEnv('TEST_VAR', 'wrong value');
  739. $this->assertFalse($request->isBanana());
  740. ServerRequest::addDetector('mobile', ['env' => 'HTTP_USER_AGENT', 'options' => ['Imagination']]);
  741. $request = $request->withEnv('HTTP_USER_AGENT', 'Imagination land');
  742. $this->assertTrue($request->isMobile());
  743. ServerRequest::addDetector('index', ['param' => 'action', 'value' => 'index']);
  744. $request = $request->withParam('action', 'index');
  745. $request->clearDetectorCache();
  746. $this->assertTrue($request->isIndex());
  747. $request = $request->withParam('action', 'add');
  748. $request->clearDetectorCache();
  749. $this->assertFalse($request->isIndex());
  750. ServerRequest::addDetector('callme', function ($request) {
  751. return $request->getAttribute('return');
  752. });
  753. $request = $request->withAttribute('return', true);
  754. $request->clearDetectorCache();
  755. $this->assertTrue($request->isCallMe());
  756. ServerRequest::addDetector('extension', ['param' => '_ext', 'options' => ['pdf', 'png', 'txt']]);
  757. $request = $request->withParam('_ext', 'pdf');
  758. $request->clearDetectorCache();
  759. $this->assertTrue($request->is('extension'));
  760. $request = $request->withParam('_ext', 'exe');
  761. $request->clearDetectorCache();
  762. $this->assertFalse($request->isExtension());
  763. }
  764. /**
  765. * Test getting headers
  766. *
  767. * @return void
  768. */
  769. public function testHeader()
  770. {
  771. $request = new ServerRequest(['environment' => [
  772. 'HTTP_HOST' => 'localhost',
  773. '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',
  774. 'CONTENT_TYPE' => 'application/json',
  775. 'CONTENT_LENGTH' => '1337',
  776. 'HTTP_CONTENT_MD5' => 'abc123',
  777. ]]);
  778. $this->assertEquals($request->getEnv('HTTP_HOST'), $request->getHeaderLine('host'));
  779. $this->assertEquals($request->getEnv('HTTP_USER_AGENT'), $request->getHeaderLine('User-Agent'));
  780. $this->assertEquals($request->getEnv('CONTENT_LENGTH'), $request->getHeaderLine('content-length'));
  781. $this->assertEquals($request->getEnv('CONTENT_TYPE'), $request->getHeaderLine('content-type'));
  782. $this->assertEquals($request->getEnv('HTTP_CONTENT_MD5'), $request->getHeaderLine('content-md5'));
  783. }
  784. /**
  785. * Test getting headers with psr7 methods
  786. *
  787. * @return void
  788. */
  789. public function testGetHeaders()
  790. {
  791. $request = new ServerRequest(['environment' => [
  792. 'HTTP_HOST' => 'localhost',
  793. 'CONTENT_TYPE' => 'application/json',
  794. 'CONTENT_LENGTH' => 1337,
  795. 'HTTP_CONTENT_MD5' => 'abc123',
  796. 'HTTP_DOUBLE' => ['a', 'b'],
  797. ]]);
  798. $headers = $request->getHeaders();
  799. $expected = [
  800. 'Host' => ['localhost'],
  801. 'Content-Type' => ['application/json'],
  802. 'Content-Length' => [1337],
  803. 'Content-Md5' => ['abc123'],
  804. 'Double' => ['a', 'b'],
  805. ];
  806. $this->assertEquals($expected, $headers);
  807. }
  808. /**
  809. * Test hasHeader
  810. *
  811. * @return void
  812. */
  813. public function testHasHeader()
  814. {
  815. $request = new ServerRequest(['environment' => [
  816. 'HTTP_HOST' => 'localhost',
  817. 'CONTENT_TYPE' => 'application/json',
  818. 'CONTENT_LENGTH' => 1337,
  819. 'HTTP_CONTENT_MD5' => 'abc123',
  820. 'HTTP_DOUBLE' => ['a', 'b'],
  821. ]]);
  822. $this->assertTrue($request->hasHeader('Host'));
  823. $this->assertTrue($request->hasHeader('Content-Type'));
  824. $this->assertTrue($request->hasHeader('Content-MD5'));
  825. $this->assertTrue($request->hasHeader('Double'));
  826. $this->assertFalse($request->hasHeader('Authorization'));
  827. }
  828. /**
  829. * Test getting headers with psr7 methods
  830. *
  831. * @return void
  832. */
  833. public function testGetHeader()
  834. {
  835. $request = new ServerRequest(['environment' => [
  836. 'HTTP_HOST' => 'localhost',
  837. 'CONTENT_TYPE' => 'application/json',
  838. 'CONTENT_LENGTH' => 1337,
  839. 'HTTP_CONTENT_MD5' => 'abc123',
  840. 'HTTP_DOUBLE' => ['a', 'b'],
  841. ]]);
  842. $this->assertEquals([], $request->getHeader('Not-there'));
  843. $expected = [$request->getEnv('HTTP_HOST')];
  844. $this->assertEquals($expected, $request->getHeader('Host'));
  845. $this->assertEquals($expected, $request->getHeader('host'));
  846. $this->assertEquals($expected, $request->getHeader('HOST'));
  847. $this->assertEquals(['a', 'b'], $request->getHeader('Double'));
  848. }
  849. /**
  850. * Test getting headers with psr7 methods
  851. *
  852. * @return void
  853. */
  854. public function testGetHeaderLine()
  855. {
  856. $request = new ServerRequest(['environment' => [
  857. 'HTTP_HOST' => 'localhost',
  858. 'CONTENT_TYPE' => 'application/json',
  859. 'CONTENT_LENGTH' => '1337',
  860. 'HTTP_CONTENT_MD5' => 'abc123',
  861. 'HTTP_DOUBLE' => ['a', 'b'],
  862. ]]);
  863. $this->assertSame('', $request->getHeaderLine('Authorization'));
  864. $expected = $request->getEnv('CONTENT_LENGTH');
  865. $this->assertEquals($expected, $request->getHeaderLine('Content-Length'));
  866. $this->assertEquals($expected, $request->getHeaderLine('content-Length'));
  867. $this->assertEquals($expected, $request->getHeaderLine('ConTent-LenGth'));
  868. $this->assertSame('a, b', $request->getHeaderLine('Double'));
  869. }
  870. /**
  871. * Test setting a header.
  872. *
  873. * @return void
  874. */
  875. public function testWithHeader()
  876. {
  877. $request = new ServerRequest(['environment' => [
  878. 'HTTP_HOST' => 'localhost',
  879. 'CONTENT_TYPE' => 'application/json',
  880. 'CONTENT_LENGTH' => '1337',
  881. 'HTTP_CONTENT_MD5' => 'abc123',
  882. 'HTTP_DOUBLE' => ['a', 'b'],
  883. ]]);
  884. $new = $request->withHeader('Content-Length', '999');
  885. $this->assertNotSame($new, $request);
  886. $this->assertSame('1337', $request->getHeaderLine('Content-length'), 'old request is unchanged');
  887. $this->assertSame('999', $new->getHeaderLine('Content-length'), 'new request is correct');
  888. $new = $request->withHeader('Double', ['a']);
  889. $this->assertEquals(['a'], $new->getHeader('Double'), 'List values are overwritten');
  890. }
  891. /**
  892. * Test adding a header.
  893. *
  894. * @return void
  895. */
  896. public function testWithAddedHeader()
  897. {
  898. $request = new ServerRequest(['environment' => [
  899. 'HTTP_HOST' => 'localhost',
  900. 'CONTENT_TYPE' => 'application/json',
  901. 'CONTENT_LENGTH' => 1337,
  902. 'HTTP_CONTENT_MD5' => 'abc123',
  903. 'HTTP_DOUBLE' => ['a', 'b'],
  904. ]]);
  905. $new = $request->withAddedHeader('Double', 'c');
  906. $this->assertNotSame($new, $request);
  907. $this->assertSame('a, b', $request->getHeaderLine('Double'), 'old request is unchanged');
  908. $this->assertSame('a, b, c', $new->getHeaderLine('Double'), 'new request is correct');
  909. $new = $request->withAddedHeader('Content-Length', 777);
  910. $this->assertEquals([1337, 777], $new->getHeader('Content-Length'), 'scalar values are appended');
  911. $new = $request->withAddedHeader('Content-Length', [123, 456]);
  912. $this->assertEquals([1337, 123, 456], $new->getHeader('Content-Length'), 'List values are merged');
  913. }
  914. /**
  915. * Test removing a header.
  916. *
  917. * @return void
  918. */
  919. public function testWithoutHeader()
  920. {
  921. $request = new ServerRequest(['environment' => [
  922. 'HTTP_HOST' => 'localhost',
  923. 'CONTENT_TYPE' => 'application/json',
  924. 'CONTENT_LENGTH' => 1337,
  925. 'HTTP_CONTENT_MD5' => 'abc123',
  926. 'HTTP_DOUBLE' => ['a', 'b'],
  927. ]]);
  928. $new = $request->withoutHeader('Content-Length');
  929. $this->assertNotSame($new, $request);
  930. $this->assertSame('1337', $request->getHeaderLine('Content-length'), 'old request is unchanged');
  931. $this->assertSame('', $new->getHeaderLine('Content-length'), 'new request is correct');
  932. }
  933. /**
  934. * Test accepts() with and without parameters
  935. *
  936. * @return void
  937. */
  938. public function testAccepts()
  939. {
  940. $request = new ServerRequest(['environment' => [
  941. 'HTTP_ACCEPT' => 'text/xml,application/xml;q=0.9,application/xhtml+xml,text/html,text/plain,image/png',
  942. ]]);
  943. $result = $request->accepts();
  944. $expected = [
  945. 'text/xml', 'application/xhtml+xml', 'text/html', 'text/plain', 'image/png', 'application/xml',
  946. ];
  947. $this->assertEquals($expected, $result, 'Content types differ.');
  948. $result = $request->accepts('text/html');
  949. $this->assertTrue($result);
  950. $result = $request->accepts('image/gif');
  951. $this->assertFalse($result);
  952. }
  953. /**
  954. * Test that accept header types are trimmed for comparisons.
  955. *
  956. * @return void
  957. */
  958. public function testAcceptWithWhitespace()
  959. {
  960. $request = new ServerRequest(['environment' => [
  961. 'HTTP_ACCEPT' => 'text/xml , text/html , text/plain,image/png',
  962. ]]);
  963. $result = $request->accepts();
  964. $expected = [
  965. 'text/xml', 'text/html', 'text/plain', 'image/png',
  966. ];
  967. $this->assertEquals($expected, $result, 'Content types differ.');
  968. $this->assertTrue($request->accepts('text/html'));
  969. }
  970. /**
  971. * Content types from accepts() should respect the client's q preference values.
  972. *
  973. * @return void
  974. */
  975. public function testAcceptWithQvalueSorting()
  976. {
  977. $request = new ServerRequest(['environment' => [
  978. 'HTTP_ACCEPT' => 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0',
  979. ]]);
  980. $result = $request->accepts();
  981. $expected = ['application/xml', 'text/html', 'application/json'];
  982. $this->assertEquals($expected, $result);
  983. }
  984. /**
  985. * Test the raw parsing of accept headers into the q value formatting.
  986. *
  987. * @return void
  988. */
  989. public function testParseAcceptWithQValue()
  990. {
  991. $request = new ServerRequest(['environment' => [
  992. 'HTTP_ACCEPT' => 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0,image/png',
  993. ]]);
  994. $result = $request->parseAccept();
  995. $expected = [
  996. '1.0' => ['application/xml', 'image/png'],
  997. '0.8' => ['text/html'],
  998. '0.7' => ['application/json'],
  999. ];
  1000. $this->assertEquals($expected, $result);
  1001. }
  1002. /**
  1003. * Test parsing accept with a confusing accept value.
  1004. *
  1005. * @return void
  1006. */
  1007. public function testParseAcceptNoQValues()
  1008. {
  1009. $request = new ServerRequest(['environment' => [
  1010. 'HTTP_ACCEPT' => 'application/json, text/plain, */*',
  1011. ]]);
  1012. $result = $request->parseAccept();
  1013. $expected = [
  1014. '1.0' => ['application/json', 'text/plain', '*/*'],
  1015. ];
  1016. $this->assertEquals($expected, $result);
  1017. }
  1018. /**
  1019. * Test parsing accept ignores index param
  1020. *
  1021. * @return void
  1022. */
  1023. public function testParseAcceptIgnoreAcceptExtensions()
  1024. {
  1025. $request = new ServerRequest(['environment' => [
  1026. 'url' => '/',
  1027. 'HTTP_ACCEPT' => 'application/json;level=1, text/plain, */*',
  1028. ]]);
  1029. $result = $request->parseAccept();
  1030. $expected = [
  1031. '1.0' => ['application/json', 'text/plain', '*/*'],
  1032. ];
  1033. $this->assertEquals($expected, $result);
  1034. }
  1035. /**
  1036. * Test that parsing accept headers with invalid syntax works.
  1037. *
  1038. * The header used is missing a q value for application/xml.
  1039. *
  1040. * @return void
  1041. */
  1042. public function testParseAcceptInvalidSyntax()
  1043. {
  1044. $request = new ServerRequest(['environment' => [
  1045. 'url' => '/',
  1046. 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;image/png,image/jpeg,image/*;q=0.9,*/*;q=0.8',
  1047. ]]);
  1048. $result = $request->parseAccept();
  1049. $expected = [
  1050. '1.0' => ['text/html', 'application/xhtml+xml', 'application/xml', 'image/jpeg'],
  1051. '0.9' => ['image/*'],
  1052. '0.8' => ['*/*'],
  1053. ];
  1054. $this->assertEquals($expected, $result);
  1055. }
  1056. /**
  1057. * Test the getQuery() method
  1058. *
  1059. * @return void
  1060. */
  1061. public function testGetQuery()
  1062. {
  1063. $array = [
  1064. 'query' => [
  1065. 'foo' => 'bar',
  1066. 'zero' => '0',
  1067. 'test' => [
  1068. 'foo', 'bar',
  1069. ],
  1070. ],
  1071. ];
  1072. $request = new ServerRequest($array);
  1073. $this->assertEquals([
  1074. 'foo' => 'bar',
  1075. 'zero' => '0',
  1076. 'test' => [
  1077. 'foo', 'bar',
  1078. ],
  1079. ], $request->getQuery());
  1080. $this->assertSame('bar', $request->getQuery('foo'));
  1081. $this->assertSame('0', $request->getQuery('zero'));
  1082. $this->assertNull($request->getQuery('imaginary'));
  1083. $this->assertSame('default', $request->getQuery('imaginary', 'default'));
  1084. $this->assertFalse($request->getQuery('imaginary', false));
  1085. $this->assertSame(['foo', 'bar'], $request->getQuery('test'));
  1086. $this->assertSame('bar', $request->getQuery('test.1'));
  1087. $this->assertNull($request->getQuery('test.2'));
  1088. $this->assertSame('default', $request->getQuery('test.2', 'default'));
  1089. }
  1090. /**
  1091. * Test getQueryParams
  1092. *
  1093. * @return void
  1094. */
  1095. public function testGetQueryParams()
  1096. {
  1097. $get = [
  1098. 'test' => ['foo', 'bar'],
  1099. 'key' => 'value',
  1100. ];
  1101. $request = new ServerRequest([
  1102. 'query' => $get,
  1103. ]);
  1104. $this->assertSame($get, $request->getQueryParams());
  1105. }
  1106. /**
  1107. * Test withQueryParams and immutability
  1108. *
  1109. * @return void
  1110. */
  1111. public function testWithQueryParams()
  1112. {
  1113. $get = [
  1114. 'test' => ['foo', 'bar'],
  1115. 'key' => 'value',
  1116. ];
  1117. $request = new ServerRequest([
  1118. 'query' => $get,
  1119. ]);
  1120. $new = $request->withQueryParams(['new' => 'data']);
  1121. $this->assertSame($get, $request->getQueryParams());
  1122. $this->assertSame(['new' => 'data'], $new->getQueryParams());
  1123. }
  1124. /**
  1125. * Test using param()
  1126. *
  1127. * @return void
  1128. */
  1129. public function testReadingParams()
  1130. {
  1131. $request = new ServerRequest([
  1132. 'params' => [
  1133. 'controller' => 'posts',
  1134. 'admin' => true,
  1135. 'truthy' => 1,
  1136. 'zero' => '0',
  1137. ],
  1138. ]);
  1139. $this->assertNull($request->getParam('not_set'));
  1140. $this->assertTrue($request->getParam('admin'));
  1141. $this->assertSame(1, $request->getParam('truthy'));
  1142. $this->assertSame('posts', $request->getParam('controller'));
  1143. $this->assertSame('0', $request->getParam('zero'));
  1144. }
  1145. /**
  1146. * Test the data() method reading
  1147. *
  1148. * @return void
  1149. */
  1150. public function testGetData()
  1151. {
  1152. $post = [
  1153. 'Model' => [
  1154. 'field' => 'value',
  1155. ],
  1156. ];
  1157. $request = new ServerRequest(compact('post'));
  1158. $this->assertEquals($post['Model'], $request->getData('Model'));
  1159. $this->assertEquals($post, $request->getData());
  1160. $this->assertNull($request->getData('Model.imaginary'));
  1161. $this->assertSame('value', $request->getData('Model.field', 'default'));
  1162. $this->assertSame('default', $request->getData('Model.imaginary', 'default'));
  1163. }
  1164. /**
  1165. * Test that getData() doesn't fail on scalar data.
  1166. *
  1167. * @return void
  1168. */
  1169. public function testGetDataOnStringData()
  1170. {
  1171. $post = 'strange, but could happen';
  1172. $request = new ServerRequest(compact('post'));
  1173. $this->assertNull($request->getData('Model'));
  1174. $this->assertNull($request->getData('Model.field'));
  1175. }
  1176. /**
  1177. * Test writing falsey values.
  1178. *
  1179. * @return void
  1180. */
  1181. public function testDataWritingFalsey()
  1182. {
  1183. $request = new ServerRequest();
  1184. $request = $request->withData('Post.null', null);
  1185. $this->assertNull($request->getData('Post.null'));
  1186. $request = $request->withData('Post.false', false);
  1187. $this->assertFalse($request->getData('Post.false'));
  1188. $request = $request->withData('Post.zero', 0);
  1189. $this->assertSame(0, $request->getData('Post.zero'));
  1190. $request = $request->withData('Post.empty', '');
  1191. $this->assertSame('', $request->getData('Post.empty'));
  1192. }
  1193. /**
  1194. * Test reading params
  1195. *
  1196. * @dataProvider paramReadingDataProvider
  1197. */
  1198. public function testGetParam($toRead, $expected)
  1199. {
  1200. $request = new ServerRequest([
  1201. 'url' => '/',
  1202. 'params' => [
  1203. 'action' => 'index',
  1204. 'foo' => 'bar',
  1205. 'baz' => [
  1206. 'a' => [
  1207. 'b' => 'c',
  1208. ],
  1209. ],
  1210. 'admin' => true,
  1211. 'truthy' => 1,
  1212. 'zero' => '0',
  1213. ],
  1214. ]);
  1215. $this->assertSame($expected, $request->getParam($toRead));
  1216. }
  1217. /**
  1218. * Test getParam returning a default value.
  1219. *
  1220. * @return void
  1221. */
  1222. public function testGetParamDefault()
  1223. {
  1224. $request = new ServerRequest([
  1225. 'params' => [
  1226. 'controller' => 'Articles',
  1227. 'null' => null,
  1228. ],
  1229. ]);
  1230. $this->assertSame('Articles', $request->getParam('controller', 'default'));
  1231. $this->assertSame('default', $request->getParam('null', 'default'));
  1232. $this->assertFalse($request->getParam('unset', false));
  1233. $this->assertNull($request->getParam('unset'));
  1234. }
  1235. /**
  1236. * Data provider for testing reading values with ServerRequest::getParam()
  1237. *
  1238. * @return array
  1239. */
  1240. public function paramReadingDataProvider()
  1241. {
  1242. return [
  1243. [
  1244. 'action',
  1245. 'index',
  1246. ],
  1247. [
  1248. 'baz',
  1249. [
  1250. 'a' => [
  1251. 'b' => 'c',
  1252. ],
  1253. ],
  1254. ],
  1255. [
  1256. 'baz.a.b',
  1257. 'c',
  1258. ],
  1259. [
  1260. 'does_not_exist',
  1261. null,
  1262. ],
  1263. [
  1264. 'admin',
  1265. true,
  1266. ],
  1267. [
  1268. 'truthy',
  1269. 1,
  1270. ],
  1271. [
  1272. 'zero',
  1273. '0',
  1274. ],
  1275. ];
  1276. }
  1277. /**
  1278. * test writing request params with param()
  1279. *
  1280. * @return void
  1281. */
  1282. public function testParamWriting()
  1283. {
  1284. $request = new ServerRequest(['url' => '/']);
  1285. $request = $request->withParam('action', 'index');
  1286. $this->assertInstanceOf(
  1287. 'Cake\Http\ServerRequest',
  1288. $request->withParam('some', 'thing'),
  1289. 'Method has not returned $this'
  1290. );
  1291. $request = $request->withParam('Post.null', null);
  1292. $this->assertNull($request->getParam('Post.null'), 'default value should be used.');
  1293. $request = $request->withParam('Post.false', false);
  1294. $this->assertFalse($request->getParam('Post.false'));
  1295. $request = $request->withParam('Post.zero', 0);
  1296. $this->assertSame(0, $request->getParam('Post.zero'));
  1297. $request = $request->withParam('Post.empty', '');
  1298. $this->assertSame('', $request->getParam('Post.empty'));
  1299. $this->assertSame('index', $request->getParam('action'));
  1300. $request = $request->withParam('action', 'edit');
  1301. $this->assertSame('edit', $request->getParam('action'));
  1302. }
  1303. /**
  1304. * Test accept language
  1305. *
  1306. * @return void
  1307. */
  1308. public function testAcceptLanguage()
  1309. {
  1310. $request = new ServerRequest();
  1311. // Weird language
  1312. $request = $request->withEnv('HTTP_ACCEPT_LANGUAGE', 'inexistent,en-ca');
  1313. $result = $request->acceptLanguage();
  1314. $this->assertEquals(['inexistent', 'en-ca'], $result, 'Languages do not match');
  1315. // No qualifier
  1316. $request = $request->withEnv('HTTP_ACCEPT_LANGUAGE', 'es_mx,en_ca');
  1317. $result = $request->acceptLanguage();
  1318. $this->assertEquals(['es-mx', 'en-ca'], $result, 'Languages do not match');
  1319. // With qualifier
  1320. $request = $request->withEnv('HTTP_ACCEPT_LANGUAGE', 'en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4');
  1321. $result = $request->acceptLanguage();
  1322. $this->assertEquals(['en-us', 'en', 'pt-br', 'pt'], $result, 'Languages do not match');
  1323. // With spaces
  1324. $request = $request->withEnv('HTTP_ACCEPT_LANGUAGE', 'da, en-gb;q=0.8, en;q=0.7');
  1325. $result = $request->acceptLanguage();
  1326. $this->assertEquals(['da', 'en-gb', 'en'], $result, 'Languages do not match');
  1327. // Checking if requested
  1328. $request = $request->withEnv('HTTP_ACCEPT_LANGUAGE', 'es_mx,en_ca');
  1329. $result = $request->acceptLanguage('en-ca');
  1330. $this->assertTrue($result);
  1331. $result = $request->acceptLanguage('en-CA');
  1332. $this->assertTrue($result);
  1333. $result = $request->acceptLanguage('en-us');
  1334. $this->assertFalse($result);
  1335. $result = $request->acceptLanguage('en-US');
  1336. $this->assertFalse($result);
  1337. }
  1338. /**
  1339. * Test the input() method.
  1340. *
  1341. * @return void
  1342. */
  1343. public function testInput()
  1344. {
  1345. $request = new ServerRequest([
  1346. 'input' => 'I came from stdin',
  1347. ]);
  1348. $this->deprecated(function () use ($request) {
  1349. $result = $request->input();
  1350. $this->assertSame('I came from stdin', $result);
  1351. });
  1352. }
  1353. /**
  1354. * Test input() decoding.
  1355. *
  1356. * @return void
  1357. * @group deprecated
  1358. */
  1359. public function testInputDecode()
  1360. {
  1361. $request = new ServerRequest([
  1362. 'input' => '{"name":"value"}',
  1363. ]);
  1364. $this->deprecated(function () use ($request) {
  1365. $result = $request->input('json_decode');
  1366. $this->assertEquals(['name' => 'value'], (array)$result);
  1367. });
  1368. }
  1369. /**
  1370. * Test input() decoding with additional arguments.
  1371. *
  1372. * @return void
  1373. * @group deprecated
  1374. */
  1375. public function testInputDecodeExtraParams()
  1376. {
  1377. $xml = <<<XML
  1378. <?xml version="1.0" encoding="utf-8"?>
  1379. <post>
  1380. <title id="title">Test</title>
  1381. </post>
  1382. XML;
  1383. $request = new ServerRequest([
  1384. 'input' => $xml,
  1385. ]);
  1386. $this->deprecated(function () use ($request) {
  1387. $result = $request->input('Cake\Utility\Xml::build', ['return' => 'domdocument']);
  1388. $this->assertInstanceOf('DOMDocument', $result);
  1389. $this->assertSame(
  1390. 'Test',
  1391. $result->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText
  1392. );
  1393. });
  1394. }
  1395. /**
  1396. * Test getBody
  1397. *
  1398. * @return void
  1399. */
  1400. public function testGetBody()
  1401. {
  1402. $request = new ServerRequest([
  1403. 'input' => 'key=val&some=data',
  1404. ]);
  1405. $result = $request->getBody();
  1406. $this->assertInstanceOf('Psr\Http\Message\StreamInterface', $result);
  1407. $this->assertSame('key=val&some=data', $result->getContents());
  1408. }
  1409. /**
  1410. * Test withBody
  1411. *
  1412. * @return void
  1413. */
  1414. public function testWithBody()
  1415. {
  1416. $request = new ServerRequest([
  1417. 'input' => 'key=val&some=data',
  1418. ]);
  1419. $body = $this->getMockBuilder('Psr\Http\Message\StreamInterface')->getMock();
  1420. $new = $request->withBody($body);
  1421. $this->assertNotSame($new, $request);
  1422. $this->assertNotSame($body, $request->getBody());
  1423. $this->assertSame($body, $new->getBody());
  1424. }
  1425. /**
  1426. * Test getUri
  1427. *
  1428. * @return void
  1429. */
  1430. public function testGetUri()
  1431. {
  1432. $request = new ServerRequest(['url' => 'articles/view/3']);
  1433. $result = $request->getUri();
  1434. $this->assertInstanceOf('Psr\Http\Message\UriInterface', $result);
  1435. $this->assertSame('/articles/view/3', $result->getPath());
  1436. }
  1437. /**
  1438. * Test withUri
  1439. *
  1440. * @return void
  1441. */
  1442. public function testWithUri()
  1443. {
  1444. $request = new ServerRequest([
  1445. 'environment' => [
  1446. 'HTTP_HOST' => 'example.com',
  1447. ],
  1448. 'url' => 'articles/view/3',
  1449. ]);
  1450. $uri = $this->getMockBuilder('Psr\Http\Message\UriInterface')->getMock();
  1451. $new = $request->withUri($uri);
  1452. $this->assertNotSame($new, $request);
  1453. $this->assertNotSame($uri, $request->getUri());
  1454. $this->assertSame($uri, $new->getUri());
  1455. }
  1456. /**
  1457. * Test withUri() and preserveHost
  1458. *
  1459. * @return void
  1460. */
  1461. public function testWithUriPreserveHost()
  1462. {
  1463. $request = new ServerRequest([
  1464. 'environment' => [
  1465. 'HTTP_HOST' => 'localhost',
  1466. ],
  1467. 'url' => 'articles/view/3',
  1468. ]);
  1469. $uri = new Uri();
  1470. $uri = $uri->withHost('example.com')
  1471. ->withPort(123)
  1472. ->withPath('articles/view/3');
  1473. $new = $request->withUri($uri, false);
  1474. $this->assertNotSame($new, $request);
  1475. $this->assertSame('example.com:123', $new->getHeaderLine('Host'));
  1476. }
  1477. /**
  1478. * Test withUri() and preserveHost missing the host header
  1479. *
  1480. * @return void
  1481. */
  1482. public function testWithUriPreserveHostNoHostHeader()
  1483. {
  1484. $request = new ServerRequest([
  1485. 'url' => 'articles/view/3',
  1486. ]);
  1487. $uri = new Uri();
  1488. $uri = $uri->withHost('example.com')
  1489. ->withPort(123)
  1490. ->withPath('articles/view/3');
  1491. $new = $request->withUri($uri, false);
  1492. $this->assertSame('example.com:123', $new->getHeaderLine('Host'));
  1493. }
  1494. /**
  1495. * Test the cookie() method.
  1496. *
  1497. * @return void
  1498. */
  1499. public function testGetCookie()
  1500. {
  1501. $request = new ServerRequest([
  1502. 'cookies' => [
  1503. 'testing' => 'A value in the cookie',
  1504. 'user' => [
  1505. 'remember' => '1',
  1506. ],
  1507. ],
  1508. ]);
  1509. $this->assertSame('A value in the cookie', $request->getCookie('testing'));
  1510. $this->assertNull($request->getCookie('not there'));
  1511. $this->assertSame('default', $request->getCookie('not there', 'default'));
  1512. $this->assertSame('1', $request->getCookie('user.remember'));
  1513. $this->assertSame('1', $request->getCookie('user.remember', 'default'));
  1514. $this->assertSame('default', $request->getCookie('user.not there', 'default'));
  1515. }
  1516. /**
  1517. * Test getCookieParams()
  1518. *
  1519. * @return void
  1520. */
  1521. public function testGetCookieParams()
  1522. {
  1523. $cookies = [
  1524. 'testing' => 'A value in the cookie',
  1525. ];
  1526. $request = new ServerRequest(['cookies' => $cookies]);
  1527. $this->assertSame($cookies, $request->getCookieParams());
  1528. }
  1529. /**
  1530. * Test withCookieParams()
  1531. *
  1532. * @return void
  1533. */
  1534. public function testWithCookieParams()
  1535. {
  1536. $cookies = [
  1537. 'testing' => 'A value in the cookie',
  1538. ];
  1539. $request = new ServerRequest(['cookies' => $cookies]);
  1540. $new = $request->withCookieParams(['remember_me' => 1]);
  1541. $this->assertNotSame($new, $request);
  1542. $this->assertSame($cookies, $request->getCookieParams());
  1543. $this->assertSame(['remember_me' => 1], $new->getCookieParams());
  1544. }
  1545. /**
  1546. * Test getting a cookie collection from a request.
  1547. *
  1548. * @return void
  1549. */
  1550. public function testGetCookieCollection()
  1551. {
  1552. $cookies = [
  1553. 'remember_me' => '1',
  1554. 'color' => 'blue',
  1555. ];
  1556. $request = new ServerRequest(['cookies' => $cookies]);
  1557. $cookies = $request->getCookieCollection();
  1558. $this->assertInstanceOf(CookieCollection::class, $cookies);
  1559. $this->assertCount(2, $cookies);
  1560. $this->assertSame('1', $cookies->get('remember_me')->getValue());
  1561. $this->assertSame('blue', $cookies->get('color')->getValue());
  1562. }
  1563. /**
  1564. * Test replacing cookies from a collection
  1565. *
  1566. * @return void
  1567. */
  1568. public function testWithCookieCollection()
  1569. {
  1570. $cookies = new CookieCollection([new Cookie('remember_me', 1), new Cookie('color', 'red')]);
  1571. $request = new ServerRequest(['cookies' => ['bad' => 'goaway']]);
  1572. $new = $request->withCookieCollection($cookies);
  1573. $this->assertNotSame($new, $request, 'Should clone');
  1574. $this->assertSame(['bad' => 'goaway'], $request->getCookieParams());
  1575. $this->assertSame(['remember_me' => 1, 'color' => 'red'], $new->getCookieParams());
  1576. $cookies = $new->getCookieCollection();
  1577. $this->assertCount(2, $cookies);
  1578. $this->assertSame('red', $cookies->get('color')->getValue());
  1579. }
  1580. /**
  1581. * TestAllowMethod
  1582. *
  1583. * @return void
  1584. */
  1585. public function testAllowMethod()
  1586. {
  1587. $request = new ServerRequest(['environment' => [
  1588. 'url' => '/posts/edit/1',
  1589. 'REQUEST_METHOD' => 'PUT',
  1590. ]]);
  1591. $this->assertTrue($request->allowMethod('put'));
  1592. $request = $request->withEnv('REQUEST_METHOD', 'DELETE');
  1593. $this->assertTrue($request->allowMethod(['post', 'delete']));
  1594. }
  1595. /**
  1596. * Test allowMethod throwing exception
  1597. *
  1598. * @return void
  1599. */
  1600. public function testAllowMethodException()
  1601. {
  1602. $request = new ServerRequest([
  1603. 'url' => '/posts/edit/1',
  1604. 'environment' => ['REQUEST_METHOD' => 'PUT'],
  1605. ]);
  1606. try {
  1607. $request->allowMethod(['POST', 'DELETE']);
  1608. $this->fail('An expected exception has not been raised.');
  1609. } catch (MethodNotAllowedException $e) {
  1610. $this->assertEquals(['Allow' => 'POST, DELETE'], $e->getHeaders());
  1611. }
  1612. $this->expectException(MethodNotAllowedException::class);
  1613. $request->allowMethod('POST');
  1614. }
  1615. /**
  1616. * Tests getting the session from the request
  1617. *
  1618. * @return void
  1619. */
  1620. public function testGetSession()
  1621. {
  1622. $session = new Session();
  1623. $request = new ServerRequest(['session' => $session]);
  1624. $this->assertSame($session, $request->getSession());
  1625. $request = new ServerRequest();
  1626. $this->assertEquals($session, $request->getSession());
  1627. }
  1628. /**
  1629. * Test the content type method.
  1630. *
  1631. * @return void
  1632. */
  1633. public function testContentType()
  1634. {
  1635. $request = new ServerRequest([
  1636. 'environment' => ['HTTP_CONTENT_TYPE' => 'application/json'],
  1637. ]);
  1638. $this->assertSame('application/json', $request->contentType());
  1639. $request = new ServerRequest([
  1640. 'environment' => ['HTTP_CONTENT_TYPE' => 'application/xml'],
  1641. ]);
  1642. $this->assertSame('application/xml', $request->contentType(), 'prefer non http header.');
  1643. }
  1644. /**
  1645. * Test updating params in a psr7 fashion.
  1646. *
  1647. * @return void
  1648. */
  1649. public function testWithParam()
  1650. {
  1651. $request = new ServerRequest([
  1652. 'params' => ['controller' => 'Articles'],
  1653. ]);
  1654. $result = $request->withParam('action', 'view');
  1655. $this->assertNotSame($result, $request, 'New instance should be made');
  1656. $this->assertNull($request->getParam('action'), 'No side-effect on original');
  1657. $this->assertSame('view', $result->getParam('action'));
  1658. $result = $request->withParam('action', 'index')
  1659. ->withParam('plugin', 'DebugKit')
  1660. ->withParam('prefix', 'Admin');
  1661. $this->assertNotSame($result, $request, 'New instance should be made');
  1662. $this->assertNull($request->getParam('action'), 'No side-effect on original');
  1663. $this->assertSame('index', $result->getParam('action'));
  1664. $this->assertSame('DebugKit', $result->getParam('plugin'));
  1665. $this->assertSame('Admin', $result->getParam('prefix'));
  1666. }
  1667. /**
  1668. * Test getting the parsed body parameters.
  1669. *
  1670. * @return void
  1671. */
  1672. public function testGetParsedBody()
  1673. {
  1674. $data = ['title' => 'First', 'body' => 'Best Article!'];
  1675. $request = new ServerRequest(['post' => $data]);
  1676. $this->assertSame($data, $request->getParsedBody());
  1677. $request = new ServerRequest();
  1678. $this->assertSame([], $request->getParsedBody());
  1679. }
  1680. /**
  1681. * Test replacing the parsed body parameters.
  1682. *
  1683. * @return void
  1684. */
  1685. public function testWithParsedBody()
  1686. {
  1687. $data = ['title' => 'First', 'body' => 'Best Article!'];
  1688. $request = new ServerRequest([]);
  1689. $new = $request->withParsedBody($data);
  1690. $this->assertNotSame($request, $new);
  1691. $this->assertSame([], $request->getParsedBody());
  1692. $this->assertSame($data, $new->getParsedBody());
  1693. }
  1694. /**
  1695. * Test updating POST data in a psr7 fashion.
  1696. *
  1697. * @return void
  1698. */
  1699. public function testWithData()
  1700. {
  1701. $request = new ServerRequest([
  1702. 'post' => [
  1703. 'Model' => [
  1704. 'field' => 'value',
  1705. ],
  1706. ],
  1707. ]);
  1708. $result = $request->withData('Model.new_value', 'new value');
  1709. $this->assertNull($request->getData('Model.new_value'), 'Original request should not change.');
  1710. $this->assertNotSame($result, $request);
  1711. $this->assertSame('new value', $result->getData('Model.new_value'));
  1712. $this->assertSame('new value', $result->getData()['Model']['new_value']);
  1713. $this->assertSame('value', $result->getData('Model.field'));
  1714. }
  1715. /**
  1716. * Test removing data from a request
  1717. *
  1718. * @return void
  1719. */
  1720. public function testWithoutData()
  1721. {
  1722. $request = new ServerRequest([
  1723. 'post' => [
  1724. 'Model' => [
  1725. 'id' => 1,
  1726. 'field' => 'value',
  1727. ],
  1728. ],
  1729. ]);
  1730. $updated = $request->withoutData('Model.field');
  1731. $this->assertNotSame($updated, $request);
  1732. $this->assertSame('value', $request->getData('Model.field'), 'Original request should not change.');
  1733. $this->assertNull($updated->getData('Model.field'), 'data removed from updated request');
  1734. $this->assertFalse(isset($updated->getData()['Model']['field']), 'data removed from updated request');
  1735. }
  1736. /**
  1737. * Test updating POST data when keys don't exist
  1738. *
  1739. * @return void
  1740. */
  1741. public function testWithDataMissingIntermediaryKeys()
  1742. {
  1743. $request = new ServerRequest([
  1744. 'post' => [
  1745. 'Model' => [
  1746. 'field' => 'value',
  1747. ],
  1748. ],
  1749. ]);
  1750. $result = $request->withData('Model.field.new_value', 'new value');
  1751. $this->assertSame(
  1752. 'new value',
  1753. $result->getData('Model.field.new_value')
  1754. );
  1755. $this->assertSame(
  1756. 'new value',
  1757. $result->getData()['Model']['field']['new_value']
  1758. );
  1759. }
  1760. /**
  1761. * Test updating POST data with falsey values
  1762. *
  1763. * @return void
  1764. */
  1765. public function testWithDataFalseyValues()
  1766. {
  1767. $request = new ServerRequest([
  1768. 'post' => [],
  1769. ]);
  1770. $result = $request->withData('false', false)
  1771. ->withData('null', null)
  1772. ->withData('empty_string', '')
  1773. ->withData('zero', 0)
  1774. ->withData('zero_string', '0');
  1775. $expected = [
  1776. 'false' => false,
  1777. 'null' => null,
  1778. 'empty_string' => '',
  1779. 'zero' => 0,
  1780. 'zero_string' => '0',
  1781. ];
  1782. $this->assertSame($expected, $result->getData());
  1783. }
  1784. /**
  1785. * Test setting attributes.
  1786. *
  1787. * @return void
  1788. */
  1789. public function testWithAttribute()
  1790. {
  1791. $request = new ServerRequest([]);
  1792. $this->assertNull($request->getAttribute('key'));
  1793. $this->assertSame('default', $request->getAttribute('key', 'default'));
  1794. $new = $request->withAttribute('key', 'value');
  1795. $this->assertNotEquals($new, $request, 'Should be different');
  1796. $this->assertNull($request->getAttribute('key'), 'Old instance not modified');
  1797. $this->assertSame('value', $new->getAttribute('key'));
  1798. $update = $new->withAttribute('key', ['complex']);
  1799. $this->assertNotEquals($update, $new, 'Should be different');
  1800. $this->assertSame(['complex'], $update->getAttribute('key'));
  1801. }
  1802. /**
  1803. * Test that replacing the session can be done via withAttribute()
  1804. *
  1805. * @return void
  1806. */
  1807. public function testWithAttributeSession()
  1808. {
  1809. $request = new ServerRequest([]);
  1810. $request->getSession()->write('attrKey', 'session-value');
  1811. $update = $request->withAttribute('session', Session::create());
  1812. $this->assertSame('session-value', $request->getAttribute('session')->read('attrKey'));
  1813. $this->assertNotSame($request->getAttribute('session'), $update->getAttribute('session'));
  1814. $this->assertNotSame($request->getSession()->read('attrKey'), $update->getSession()->read('attrKey'));
  1815. }
  1816. /**
  1817. * Test getting all attributes.
  1818. *
  1819. * @return void
  1820. */
  1821. public function testGetAttributes()
  1822. {
  1823. $request = new ServerRequest([]);
  1824. $new = $request->withAttribute('key', 'value')
  1825. ->withAttribute('nully', null)
  1826. ->withAttribute('falsey', false);
  1827. $this->assertFalse($new->getAttribute('falsey'));
  1828. $this->assertNull($new->getAttribute('nully'));
  1829. $expected = [
  1830. 'key' => 'value',
  1831. 'nully' => null,
  1832. 'falsey' => false,
  1833. 'params' => [
  1834. 'plugin' => null,
  1835. 'controller' => null,
  1836. 'action' => null,
  1837. '_ext' => null,
  1838. 'pass' => [],
  1839. ],
  1840. 'webroot' => '',
  1841. 'base' => '',
  1842. 'here' => '/',
  1843. ];
  1844. $this->assertEquals($expected, $new->getAttributes());
  1845. }
  1846. /**
  1847. * Test unsetting attributes.
  1848. *
  1849. * @return void
  1850. */
  1851. public function testWithoutAttribute()
  1852. {
  1853. $request = new ServerRequest([]);
  1854. $new = $request->withAttribute('key', 'value');
  1855. $update = $request->withoutAttribute('key');
  1856. $this->assertNotEquals($update, $new, 'Should be different');
  1857. $this->assertNull($update->getAttribute('key'));
  1858. }
  1859. /**
  1860. * Test that withoutAttribute() cannot remove emulatedAttributes properties.
  1861. *
  1862. * @dataProvider emulatedPropertyProvider
  1863. * @return void
  1864. */
  1865. public function testWithoutAttributesDenyEmulatedProperties($prop)
  1866. {
  1867. $this->expectException(\InvalidArgumentException::class);
  1868. $request = new ServerRequest([]);
  1869. $request->withoutAttribute($prop);
  1870. }
  1871. /**
  1872. * Test the requestTarget methods.
  1873. *
  1874. * @return void
  1875. */
  1876. public function testWithRequestTarget()
  1877. {
  1878. $request = new ServerRequest([
  1879. 'environment' => [
  1880. 'REQUEST_URI' => '/articles/view/1',
  1881. 'QUERY_STRING' => 'comments=1&open=0',
  1882. ],
  1883. 'base' => '/basedir',
  1884. ]);
  1885. $this->assertSame(
  1886. '/articles/view/1?comments=1&open=0',
  1887. $request->getRequestTarget(),
  1888. 'Should not include basedir.'
  1889. );
  1890. $new = $request->withRequestTarget('/articles/view/3');
  1891. $this->assertNotSame($new, $request);
  1892. $this->assertSame(
  1893. '/articles/view/1?comments=1&open=0',
  1894. $request->getRequestTarget(),
  1895. 'should be unchanged.'
  1896. );
  1897. $this->assertSame('/articles/view/3', $new->getRequestTarget(), 'reflects method call');
  1898. }
  1899. /**
  1900. * Test withEnv()
  1901. *
  1902. * @return void
  1903. */
  1904. public function testWithEnv()
  1905. {
  1906. $request = new ServerRequest();
  1907. $newRequest = $request->withEnv('HTTP_HOST', 'cakephp.org');
  1908. $this->assertNotSame($request, $newRequest);
  1909. $this->assertSame('cakephp.org', $newRequest->getEnv('HTTP_HOST'));
  1910. }
  1911. /**
  1912. * Test getEnv()
  1913. *
  1914. * @return void
  1915. */
  1916. public function testGetEnv()
  1917. {
  1918. $request = new ServerRequest([
  1919. 'environment' => ['TEST' => 'ing'],
  1920. ]);
  1921. //Test default null
  1922. $this->assertNull($request->getEnv('Foo'));
  1923. //Test default set
  1924. $this->assertSame('Bar', $request->getEnv('Foo', 'Bar'));
  1925. //Test env() fallback
  1926. $this->assertSame('ing', $request->getEnv('test'));
  1927. }
  1928. /**
  1929. * Data provider for emulated property tests.
  1930. *
  1931. * @return array
  1932. */
  1933. public function emulatedPropertyProvider()
  1934. {
  1935. return [
  1936. ['here'],
  1937. ['params'],
  1938. ['base'],
  1939. ['webroot'],
  1940. ['session'],
  1941. ];
  1942. }
  1943. }