ServerRequestTest.php 62 KB

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