ServerRequestTest.php 62 KB

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