ServerRequestTest.php 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040
  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 testIsThrowsExceptionForInvalidType()
  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 the simple uses of asType()
  457. */
  458. public function testAsTypeThrowsExceptionForInvalidType()
  459. {
  460. $this->expectException(InvalidArgumentException::class);
  461. $this->expectExceptionMessage('Unable to convert request data to Elephants.');
  462. $request = new ServerRequest();
  463. $this->assertFalse($request->asType('Elephants', []));
  464. }
  465. /**
  466. * Test the magic methods on cookie data
  467. */
  468. public function testGetCookieAsDifferentTypes(): void
  469. {
  470. $request = new ServerRequest();
  471. $request = $request->withCookieParams([
  472. 'myint' => '123',
  473. 'mystring' => 'mushroom',
  474. 'mybool' => '1',
  475. ]);
  476. $this->assertSame(123, $request->getCookieAsInt('myint'));
  477. $this->assertSame('mushroom', $request->getCookieAsString('mystring'));
  478. $this->assertSame(true, $request->getCookieAsBool('mybool'));
  479. }
  480. /**
  481. * Test the magic methods on query parameters
  482. */
  483. public function testGetQueryAsDifferentTypes(): void
  484. {
  485. $request = new ServerRequest();
  486. $request = $request->withQueryParams([
  487. 'myint' => '123',
  488. 'mystring' => 'mushroom',
  489. 'mybool' => '1',
  490. ]);
  491. $this->assertSame(123, $request->getQueryAsInt('myint'));
  492. $this->assertSame('mushroom', $request->getQueryAsString('mystring'));
  493. $this->assertSame(true, $request->getQueryAsBool('mybool'));
  494. }
  495. /**
  496. * Test the magic methods on query parameters
  497. */
  498. public function testGetParamAsDifferentTypes(): void
  499. {
  500. $request = new ServerRequest();
  501. $request = $request->withParam('myint', '123');
  502. $request = $request->withParam('mystring', 'mushroom');
  503. $request = $request->withParam('mybool', '1');
  504. $this->assertSame(123, $request->getParamAsInt('myint'));
  505. $this->assertSame('mushroom', $request->getParamAsString('mystring'));
  506. $this->assertSame(true, $request->getParamAsBool('mybool'));
  507. }
  508. /**
  509. * Test the magic methods on query parameters
  510. */
  511. public function testGetDataAsDifferentTypes(): void
  512. {
  513. $request = new ServerRequest();
  514. $request = $request->withData('myint', '123');
  515. $request = $request->withData('mystring', 'mushroom');
  516. $request = $request->withData('mybool', '1');
  517. $this->assertSame(123, $request->getDataAsInt('myint'));
  518. $this->assertSame('mushroom', $request->getDataAsString('mystring'));
  519. $this->assertSame(true, $request->getDataAsBool('mybool'));
  520. }
  521. public function testGetRequestDataAsTypeString(): void
  522. {
  523. $request = new ServerRequest();
  524. $request = $request->withCookieParams(['mycookie' => 'correct']);
  525. $request = $request->withQueryParams(['myquery' => 'horse']);
  526. $request = $request->withParam('myparam', 'battery');
  527. $request = $request->withData('mydata', 'staple');
  528. $this->assertSame('correct', $request->asType('string', $request->getCookieParams(), 'mycookie'));
  529. $this->assertSame('horse', $request->asType('string', $request->getQueryParams(), 'myquery'));
  530. $this->assertSame('battery', $request->asType('string', $request->getAttribute('params'), 'myparam'));
  531. $this->assertSame('staple', $request->asType('string', $request->getData(), 'mydata'));
  532. }
  533. /**
  534. * Test asType() for integer datapoints
  535. */
  536. public function testGetRequestDataAsTypeInt(): void
  537. {
  538. $request = new ServerRequest();
  539. $request = $request->withCookieParams(['mycookie' => '123']);
  540. $request = $request->withQueryParams(['myquery' => 456]);
  541. $request = $request->withParam('myparam', '789');
  542. $request = $request->withData('mydata', 30 / 3);
  543. $this->assertSame(123, $request->asType('int', $request->getCookieParams(), 'mycookie'));
  544. $this->assertSame(456, $request->asType('int', $request->getQueryParams(), 'myquery'));
  545. $this->assertSame(789, $request->asType('int', $request->getAttribute('params'), 'myparam'));
  546. $this->assertSame(10, $request->asType('integer', $request->getData(), 'mydata'));
  547. }
  548. /**
  549. * Test asType() for boolean datapoints
  550. */
  551. public function testGetRequestDataAsTypeBool(): void
  552. {
  553. $request = new ServerRequest();
  554. $request = $request->withCookieParams(['mycookie' => true]);
  555. $request = $request->withQueryParams(['myquery' => false]);
  556. $request = $request->withParam('myparam', 1);
  557. $request = $request->withData('mydata', '0');
  558. $this->assertSame(true, $request->asType('bool', $request->getCookieParams(), 'mycookie'));
  559. $this->assertSame(false, $request->asType('bool', $request->getQueryParams(), 'myquery'));
  560. $this->assertSame(true, $request->asType('bool', $request->getAttribute('params'), 'myparam'));
  561. $this->assertSame(false, $request->asType('boolean', $request->getData(), 'mydata'));
  562. }
  563. /**
  564. * Test getMethod()
  565. */
  566. public function testGetMethod(): void
  567. {
  568. $request = new ServerRequest([
  569. 'environment' => ['REQUEST_METHOD' => 'delete'],
  570. ]);
  571. $this->assertSame('delete', $request->getMethod());
  572. }
  573. /**
  574. * Test withMethod()
  575. */
  576. public function testWithMethod(): void
  577. {
  578. $request = new ServerRequest([
  579. 'environment' => ['REQUEST_METHOD' => 'delete'],
  580. ]);
  581. $new = $request->withMethod('put');
  582. $this->assertNotSame($new, $request);
  583. $this->assertSame('delete', $request->getMethod());
  584. $this->assertSame('put', $new->getMethod());
  585. }
  586. /**
  587. * Test withMethod() and invalid data
  588. */
  589. public function testWithMethodInvalid(): void
  590. {
  591. $this->expectException(InvalidArgumentException::class);
  592. $this->expectExceptionMessage('Unsupported HTTP method `no good` provided');
  593. $request = new ServerRequest([
  594. 'environment' => ['REQUEST_METHOD' => 'delete'],
  595. ]);
  596. $request->withMethod('no good');
  597. }
  598. /**
  599. * Test getProtocolVersion()
  600. */
  601. public function testGetProtocolVersion(): void
  602. {
  603. $request = new ServerRequest();
  604. $this->assertSame('1.1', $request->getProtocolVersion());
  605. // SERVER var.
  606. $request = new ServerRequest([
  607. 'environment' => ['SERVER_PROTOCOL' => 'HTTP/1.0'],
  608. ]);
  609. $this->assertSame('1.0', $request->getProtocolVersion());
  610. }
  611. /**
  612. * Test withProtocolVersion()
  613. */
  614. public function testWithProtocolVersion(): void
  615. {
  616. $request = new ServerRequest();
  617. $new = $request->withProtocolVersion('1.0');
  618. $this->assertNotSame($new, $request);
  619. $this->assertSame('1.1', $request->getProtocolVersion());
  620. $this->assertSame('1.0', $new->getProtocolVersion());
  621. }
  622. /**
  623. * Test withProtocolVersion() and invalid data
  624. */
  625. public function testWithProtocolVersionInvalid(): void
  626. {
  627. $this->expectException(InvalidArgumentException::class);
  628. $this->expectExceptionMessage('Unsupported protocol version `no good` provided');
  629. $request = new ServerRequest();
  630. $request->withProtocolVersion('no good');
  631. }
  632. /**
  633. * Test host retrieval.
  634. */
  635. public function testHost(): void
  636. {
  637. $request = new ServerRequest(['environment' => [
  638. 'HTTP_HOST' => 'localhost',
  639. 'HTTP_X_FORWARDED_HOST' => 'cakephp.org',
  640. ]]);
  641. $this->assertSame('localhost', $request->host());
  642. $request->trustProxy = true;
  643. $this->assertSame('cakephp.org', $request->host());
  644. }
  645. /**
  646. * test port retrieval.
  647. */
  648. public function testPort(): void
  649. {
  650. $request = new ServerRequest(['environment' => ['SERVER_PORT' => '80']]);
  651. $this->assertSame('80', $request->port());
  652. $request = $request->withEnv('SERVER_PORT', '443');
  653. $request = $request->withEnv('HTTP_X_FORWARDED_PORT', '80');
  654. $this->assertSame('443', $request->port());
  655. $request->trustProxy = true;
  656. $this->assertSame('80', $request->port());
  657. }
  658. /**
  659. * test domain retrieval.
  660. */
  661. public function testDomain(): void
  662. {
  663. $request = new ServerRequest(['environment' => ['HTTP_HOST' => 'something.example.com']]);
  664. $this->assertSame('example.com', $request->domain());
  665. $request = $request->withEnv('HTTP_HOST', 'something.example.co.uk');
  666. $this->assertSame('example.co.uk', $request->domain(2));
  667. }
  668. /**
  669. * Test scheme() method.
  670. */
  671. public function testScheme(): void
  672. {
  673. $request = new ServerRequest(['environment' => ['HTTPS' => 'on']]);
  674. $this->assertSame('https', $request->scheme());
  675. $request = $request->withEnv('HTTPS', '');
  676. $this->assertSame('http', $request->scheme());
  677. $request = $request->withEnv('HTTP_X_FORWARDED_PROTO', 'https');
  678. $request->trustProxy = true;
  679. $this->assertSame('https', $request->scheme());
  680. }
  681. /**
  682. * test getting subdomains for a host.
  683. */
  684. public function testSubdomain(): void
  685. {
  686. $request = new ServerRequest(['environment' => ['HTTP_HOST' => 'something.example.com']]);
  687. $this->assertEquals(['something'], $request->subdomains());
  688. $request = $request->withEnv('HTTP_HOST', 'www.something.example.com');
  689. $this->assertEquals(['www', 'something'], $request->subdomains());
  690. $request = $request->withEnv('HTTP_HOST', 'www.something.example.co.uk');
  691. $this->assertEquals(['www', 'something'], $request->subdomains(2));
  692. $request = $request->withEnv('HTTP_HOST', 'example.co.uk');
  693. $this->assertEquals([], $request->subdomains(2));
  694. }
  695. /**
  696. * Test AJAX, flash and friends
  697. */
  698. public function testIsAjax(): void
  699. {
  700. $request = new ServerRequest();
  701. $request = $request->withEnv('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
  702. $this->assertTrue($request->is('ajax'));
  703. $request = $request->withEnv('HTTP_X_REQUESTED_WITH', 'XMLHTTPREQUEST');
  704. $this->assertFalse($request->is('ajax'));
  705. $this->assertFalse($request->isAjax());
  706. }
  707. /**
  708. * Test __call exceptions
  709. */
  710. public function testMagicCallExceptionOnUnknownMethod(): void
  711. {
  712. $this->expectException(BadMethodCallException::class);
  713. $request = new ServerRequest();
  714. $request->IamABanana();
  715. }
  716. /**
  717. * Test is(ssl)
  718. */
  719. public function testIsSsl(): void
  720. {
  721. $request = new ServerRequest();
  722. $request = $request->withEnv('HTTPS', 'on');
  723. $this->assertTrue($request->is('https'));
  724. $request = $request->withEnv('HTTPS', '1');
  725. $this->assertTrue($request->is('https'));
  726. $request = $request->withEnv('HTTPS', 'I am not empty');
  727. $this->assertFalse($request->is('https'));
  728. $request = $request->withEnv('HTTPS', 'off');
  729. $this->assertFalse($request->is('https'));
  730. $request = $request->withEnv('HTTPS', '');
  731. $this->assertFalse($request->is('https'));
  732. }
  733. /**
  734. * Test adding detectors and having them work.
  735. */
  736. public function testAddDetector(): void
  737. {
  738. $request = new ServerRequest();
  739. ServerRequest::addDetector('closure', function ($request) {
  740. return true;
  741. });
  742. $this->assertTrue($request->is('closure'));
  743. ServerRequest::addDetector('get', function ($request) {
  744. return $request->getEnv('REQUEST_METHOD') === 'GET';
  745. });
  746. $request = $request->withEnv('REQUEST_METHOD', 'GET');
  747. $this->assertTrue($request->is('get'));
  748. ServerRequest::addDetector('compare', ['env' => 'TEST_VAR', 'value' => 'something']);
  749. $request = $request->withEnv('TEST_VAR', 'something');
  750. $this->assertTrue($request->is('compare'), 'Value match failed.');
  751. $request = $request->withEnv('TEST_VAR', 'wrong');
  752. $this->assertFalse($request->is('compare'), 'Value mis-match failed.');
  753. ServerRequest::addDetector('compareCamelCase', ['env' => 'TEST_VAR', 'value' => 'foo']);
  754. $request = $request->withEnv('TEST_VAR', 'foo');
  755. $this->assertTrue($request->is('compareCamelCase'), 'Value match failed.');
  756. $this->assertTrue($request->is('comparecamelcase'), 'detectors should be case insensitive');
  757. $this->assertTrue($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
  758. $request = $request->withEnv('TEST_VAR', 'not foo');
  759. $this->assertFalse($request->is('compareCamelCase'), 'Value match failed.');
  760. $this->assertFalse($request->is('comparecamelcase'), 'detectors should be case insensitive');
  761. $this->assertFalse($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
  762. ServerRequest::addDetector('banana', ['env' => 'TEST_VAR', 'pattern' => '/^ban.*$/']);
  763. $request = $request->withEnv('TEST_VAR', 'banana');
  764. $this->assertTrue($request->isBanana());
  765. $request = $request->withEnv('TEST_VAR', 'wrong value');
  766. $this->assertFalse($request->isBanana());
  767. ServerRequest::addDetector('mobile', ['env' => 'HTTP_USER_AGENT', 'options' => ['Imagination']]);
  768. $request = $request->withEnv('HTTP_USER_AGENT', 'Imagination land');
  769. $this->assertTrue($request->isMobile());
  770. ServerRequest::addDetector('index', ['param' => 'action', 'value' => 'index']);
  771. $request = $request->withParam('action', 'index');
  772. $request->clearDetectorCache();
  773. $this->assertTrue($request->isIndex());
  774. $request = $request->withParam('action', 'add');
  775. $request->clearDetectorCache();
  776. $this->assertFalse($request->isIndex());
  777. ServerRequest::addDetector('withParams', function ($request, array $params) {
  778. foreach ($params as $name => $value) {
  779. if ($request->getParam($name) != $value) {
  780. return false;
  781. }
  782. }
  783. return true;
  784. });
  785. $request = $request->withParam('controller', 'Pages')->withParam('action', 'index');
  786. $request->clearDetectorCache();
  787. $this->assertTrue($request->isWithParams(['controller' => 'Pages', 'action' => 'index']));
  788. $request = $request->withParam('controller', 'Posts');
  789. $request->clearDetectorCache();
  790. $this->assertFalse($request->isWithParams(['controller' => 'Pages', 'action' => 'index']));
  791. ServerRequest::addDetector('callme', function ($request) {
  792. return $request->getAttribute('return');
  793. });
  794. $request = $request->withAttribute('return', true);
  795. $request->clearDetectorCache();
  796. $this->assertTrue($request->isCallMe());
  797. ServerRequest::addDetector('extension', ['param' => '_ext', 'options' => ['pdf', 'png', 'txt']]);
  798. $request = $request->withParam('_ext', 'pdf');
  799. $request->clearDetectorCache();
  800. $this->assertTrue($request->is('extension'));
  801. $request = $request->withParam('_ext', 'exe');
  802. $request->clearDetectorCache();
  803. $this->assertFalse($request->isExtension());
  804. }
  805. /**
  806. * Test getting headers
  807. */
  808. public function testHeader(): void
  809. {
  810. $request = new ServerRequest(['environment' => [
  811. 'HTTP_HOST' => 'localhost',
  812. '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',
  813. 'CONTENT_TYPE' => 'application/json',
  814. 'CONTENT_LENGTH' => '1337',
  815. 'HTTP_CONTENT_MD5' => 'abc123',
  816. ]]);
  817. $this->assertEquals($request->getEnv('HTTP_HOST'), $request->getHeaderLine('host'));
  818. $this->assertEquals($request->getEnv('HTTP_USER_AGENT'), $request->getHeaderLine('User-Agent'));
  819. $this->assertEquals($request->getEnv('CONTENT_LENGTH'), $request->getHeaderLine('content-length'));
  820. $this->assertEquals($request->getEnv('CONTENT_TYPE'), $request->getHeaderLine('content-type'));
  821. $this->assertEquals($request->getEnv('HTTP_CONTENT_MD5'), $request->getHeaderLine('content-md5'));
  822. }
  823. /**
  824. * Test getting headers with psr7 methods
  825. */
  826. public function testGetHeaders(): void
  827. {
  828. $request = new ServerRequest(['environment' => [
  829. 'HTTP_HOST' => 'localhost',
  830. 'CONTENT_TYPE' => 'application/json',
  831. 'CONTENT_LENGTH' => 1337,
  832. 'HTTP_CONTENT_MD5' => 'abc123',
  833. 'HTTP_DOUBLE' => ['a', 'b'],
  834. ]]);
  835. $headers = $request->getHeaders();
  836. $expected = [
  837. 'Host' => ['localhost'],
  838. 'Content-Type' => ['application/json'],
  839. 'Content-Length' => [1337],
  840. 'Content-Md5' => ['abc123'],
  841. 'Double' => ['a', 'b'],
  842. ];
  843. $this->assertEquals($expected, $headers);
  844. }
  845. /**
  846. * Test hasHeader
  847. */
  848. public function testHasHeader(): void
  849. {
  850. $request = new ServerRequest(['environment' => [
  851. 'HTTP_HOST' => 'localhost',
  852. 'CONTENT_TYPE' => 'application/json',
  853. 'CONTENT_LENGTH' => 1337,
  854. 'HTTP_CONTENT_MD5' => 'abc123',
  855. 'HTTP_DOUBLE' => ['a', 'b'],
  856. ]]);
  857. $this->assertTrue($request->hasHeader('Host'));
  858. $this->assertTrue($request->hasHeader('Content-Type'));
  859. $this->assertTrue($request->hasHeader('Content-MD5'));
  860. $this->assertTrue($request->hasHeader('Double'));
  861. $this->assertFalse($request->hasHeader('Authorization'));
  862. }
  863. /**
  864. * Test getting headers with psr7 methods
  865. */
  866. public function testGetHeader(): void
  867. {
  868. $request = new ServerRequest(['environment' => [
  869. 'HTTP_HOST' => 'localhost',
  870. 'CONTENT_TYPE' => 'application/json',
  871. 'CONTENT_LENGTH' => 1337,
  872. 'HTTP_CONTENT_MD5' => 'abc123',
  873. 'HTTP_DOUBLE' => ['a', 'b'],
  874. ]]);
  875. $this->assertEquals([], $request->getHeader('Not-there'));
  876. $expected = [$request->getEnv('HTTP_HOST')];
  877. $this->assertEquals($expected, $request->getHeader('Host'));
  878. $this->assertEquals($expected, $request->getHeader('host'));
  879. $this->assertEquals($expected, $request->getHeader('HOST'));
  880. $this->assertEquals(['a', 'b'], $request->getHeader('Double'));
  881. }
  882. /**
  883. * Test getting headers with psr7 methods
  884. */
  885. public function testGetHeaderLine(): void
  886. {
  887. $request = new ServerRequest(['environment' => [
  888. 'HTTP_HOST' => 'localhost',
  889. 'CONTENT_TYPE' => 'application/json',
  890. 'CONTENT_LENGTH' => '1337',
  891. 'HTTP_CONTENT_MD5' => 'abc123',
  892. 'HTTP_DOUBLE' => ['a', 'b'],
  893. ]]);
  894. $this->assertSame('', $request->getHeaderLine('Authorization'));
  895. $expected = $request->getEnv('CONTENT_LENGTH');
  896. $this->assertEquals($expected, $request->getHeaderLine('Content-Length'));
  897. $this->assertEquals($expected, $request->getHeaderLine('content-Length'));
  898. $this->assertEquals($expected, $request->getHeaderLine('ConTent-LenGth'));
  899. $this->assertSame('a, b', $request->getHeaderLine('Double'));
  900. }
  901. /**
  902. * Test setting a header.
  903. */
  904. public function testWithHeader(): void
  905. {
  906. $request = new ServerRequest(['environment' => [
  907. 'HTTP_HOST' => 'localhost',
  908. 'CONTENT_TYPE' => 'application/json',
  909. 'CONTENT_LENGTH' => '1337',
  910. 'HTTP_CONTENT_MD5' => 'abc123',
  911. 'HTTP_DOUBLE' => ['a', 'b'],
  912. ]]);
  913. $new = $request->withHeader('Content-Length', '999');
  914. $this->assertNotSame($new, $request);
  915. $this->assertSame('1337', $request->getHeaderLine('Content-length'), 'old request is unchanged');
  916. $this->assertSame('999', $new->getHeaderLine('Content-length'), 'new request is correct');
  917. $new = $request->withHeader('Double', ['a']);
  918. $this->assertEquals(['a'], $new->getHeader('Double'), 'List values are overwritten');
  919. }
  920. /**
  921. * Test adding a header.
  922. */
  923. public function testWithAddedHeader(): void
  924. {
  925. $request = new ServerRequest(['environment' => [
  926. 'HTTP_HOST' => 'localhost',
  927. 'CONTENT_TYPE' => 'application/json',
  928. 'CONTENT_LENGTH' => 1337,
  929. 'HTTP_CONTENT_MD5' => 'abc123',
  930. 'HTTP_DOUBLE' => ['a', 'b'],
  931. ]]);
  932. $new = $request->withAddedHeader('Double', 'c');
  933. $this->assertNotSame($new, $request);
  934. $this->assertSame('a, b', $request->getHeaderLine('Double'), 'old request is unchanged');
  935. $this->assertSame('a, b, c', $new->getHeaderLine('Double'), 'new request is correct');
  936. $new = $request->withAddedHeader('Content-Length', 777);
  937. $this->assertEquals([1337, 777], $new->getHeader('Content-Length'), 'scalar values are appended');
  938. $new = $request->withAddedHeader('Content-Length', [123, 456]);
  939. $this->assertEquals([1337, 123, 456], $new->getHeader('Content-Length'), 'List values are merged');
  940. }
  941. /**
  942. * Test removing a header.
  943. */
  944. public function testWithoutHeader(): void
  945. {
  946. $request = new ServerRequest(['environment' => [
  947. 'HTTP_HOST' => 'localhost',
  948. 'CONTENT_TYPE' => 'application/json',
  949. 'CONTENT_LENGTH' => 1337,
  950. 'HTTP_CONTENT_MD5' => 'abc123',
  951. 'HTTP_DOUBLE' => ['a', 'b'],
  952. ]]);
  953. $new = $request->withoutHeader('Content-Length');
  954. $this->assertNotSame($new, $request);
  955. $this->assertSame('1337', $request->getHeaderLine('Content-length'), 'old request is unchanged');
  956. $this->assertSame('', $new->getHeaderLine('Content-length'), 'new request is correct');
  957. }
  958. /**
  959. * Test accepts() with and without parameters
  960. */
  961. public function testAccepts(): void
  962. {
  963. $request = new ServerRequest(['environment' => [
  964. 'HTTP_ACCEPT' => 'text/xml,application/xml;q=0.9,application/xhtml+xml,text/html,text/plain,image/png',
  965. ]]);
  966. $result = $request->accepts();
  967. $expected = [
  968. 'text/xml', 'application/xhtml+xml', 'text/html', 'text/plain', 'image/png', 'application/xml',
  969. ];
  970. $this->assertEquals($expected, $result, 'Content types differ.');
  971. $result = $request->accepts('text/html');
  972. $this->assertTrue($result);
  973. $result = $request->accepts('image/gif');
  974. $this->assertFalse($result);
  975. }
  976. /**
  977. * Test that accept header types are trimmed for comparisons.
  978. */
  979. public function testAcceptWithWhitespace(): void
  980. {
  981. $request = new ServerRequest(['environment' => [
  982. 'HTTP_ACCEPT' => 'text/xml , text/html , text/plain,image/png',
  983. ]]);
  984. $result = $request->accepts();
  985. $expected = [
  986. 'text/xml', 'text/html', 'text/plain', 'image/png',
  987. ];
  988. $this->assertEquals($expected, $result, 'Content types differ.');
  989. $this->assertTrue($request->accepts('text/html'));
  990. }
  991. /**
  992. * Content types from accepts() should respect the client's q preference values.
  993. */
  994. public function testAcceptWithQvalueSorting(): void
  995. {
  996. $request = new ServerRequest(['environment' => [
  997. 'HTTP_ACCEPT' => 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0',
  998. ]]);
  999. $result = $request->accepts();
  1000. $expected = ['application/xml', 'text/html', 'application/json'];
  1001. $this->assertEquals($expected, $result);
  1002. }
  1003. /**
  1004. * Test the getQuery() method
  1005. */
  1006. public function testGetQuery(): void
  1007. {
  1008. $array = [
  1009. 'query' => [
  1010. 'foo' => 'bar',
  1011. 'zero' => '0',
  1012. 'test' => [
  1013. 'foo', 'bar',
  1014. ],
  1015. ],
  1016. ];
  1017. $request = new ServerRequest($array);
  1018. $this->assertEquals([
  1019. 'foo' => 'bar',
  1020. 'zero' => '0',
  1021. 'test' => [
  1022. 'foo', 'bar',
  1023. ],
  1024. ], $request->getQuery());
  1025. $this->assertSame('bar', $request->getQuery('foo'));
  1026. $this->assertSame('0', $request->getQuery('zero'));
  1027. $this->assertNull($request->getQuery('imaginary'));
  1028. $this->assertSame('default', $request->getQuery('imaginary', 'default'));
  1029. $this->assertFalse($request->getQuery('imaginary', false));
  1030. $this->assertSame(['foo', 'bar'], $request->getQuery('test'));
  1031. $this->assertSame('bar', $request->getQuery('test.1'));
  1032. $this->assertNull($request->getQuery('test.2'));
  1033. $this->assertSame('default', $request->getQuery('test.2', 'default'));
  1034. }
  1035. /**
  1036. * Test getQueryParams
  1037. */
  1038. public function testGetQueryParams(): void
  1039. {
  1040. $get = [
  1041. 'test' => ['foo', 'bar'],
  1042. 'key' => 'value',
  1043. ];
  1044. $request = new ServerRequest([
  1045. 'query' => $get,
  1046. ]);
  1047. $this->assertSame($get, $request->getQueryParams());
  1048. }
  1049. /**
  1050. * Test withQueryParams and immutability
  1051. */
  1052. public function testWithQueryParams(): void
  1053. {
  1054. $get = [
  1055. 'test' => ['foo', 'bar'],
  1056. 'key' => 'value',
  1057. ];
  1058. $request = new ServerRequest([
  1059. 'query' => $get,
  1060. ]);
  1061. $new = $request->withQueryParams(['new' => 'data']);
  1062. $this->assertSame($get, $request->getQueryParams());
  1063. $this->assertSame(['new' => 'data'], $new->getQueryParams());
  1064. }
  1065. /**
  1066. * Test using param()
  1067. */
  1068. public function testReadingParams(): void
  1069. {
  1070. $request = new ServerRequest([
  1071. 'params' => [
  1072. 'controller' => 'Posts',
  1073. 'admin' => true,
  1074. 'truthy' => 1,
  1075. 'zero' => '0',
  1076. ],
  1077. ]);
  1078. $this->assertNull($request->getParam('not_set'));
  1079. $this->assertTrue($request->getParam('admin'));
  1080. $this->assertSame(1, $request->getParam('truthy'));
  1081. $this->assertSame('Posts', $request->getParam('controller'));
  1082. $this->assertSame('0', $request->getParam('zero'));
  1083. }
  1084. /**
  1085. * Test the data() method reading
  1086. */
  1087. public function testGetData(): void
  1088. {
  1089. $post = [
  1090. 'Model' => [
  1091. 'field' => 'value',
  1092. ],
  1093. ];
  1094. $request = new ServerRequest(compact('post'));
  1095. $this->assertEquals($post['Model'], $request->getData('Model'));
  1096. $this->assertEquals($post, $request->getData());
  1097. $this->assertNull($request->getData('Model.imaginary'));
  1098. $this->assertSame('value', $request->getData('Model.field', 'default'));
  1099. $this->assertSame('default', $request->getData('Model.imaginary', 'default'));
  1100. }
  1101. /**
  1102. * Test setting post data to a string throws exception.
  1103. */
  1104. public function testInvalidStringData(): void
  1105. {
  1106. $this->expectException(InvalidArgumentException::class);
  1107. $this->expectExceptionMessage('`post` key must be an array, object or null. Got `string` instead.');
  1108. $post = 'strange, but could happen';
  1109. new ServerRequest(compact('post'));
  1110. }
  1111. /**
  1112. * Test writing falsey values.
  1113. */
  1114. public function testDataWritingFalsey(): void
  1115. {
  1116. $request = new ServerRequest();
  1117. $request = $request->withData('Post.null', null);
  1118. $this->assertNull($request->getData('Post.null'));
  1119. $request = $request->withData('Post.false', false);
  1120. $this->assertFalse($request->getData('Post.false'));
  1121. $request = $request->withData('Post.zero', 0);
  1122. $this->assertSame(0, $request->getData('Post.zero'));
  1123. $request = $request->withData('Post.empty', '');
  1124. $this->assertSame('', $request->getData('Post.empty'));
  1125. }
  1126. /**
  1127. * Test reading params
  1128. *
  1129. * @dataProvider paramReadingDataProvider
  1130. * @param mixed $expected
  1131. */
  1132. public function testGetParam(string $toRead, $expected): void
  1133. {
  1134. $request = new ServerRequest([
  1135. 'url' => '/',
  1136. 'params' => [
  1137. 'action' => 'index',
  1138. 'foo' => 'bar',
  1139. 'baz' => [
  1140. 'a' => [
  1141. 'b' => 'c',
  1142. ],
  1143. ],
  1144. 'admin' => true,
  1145. 'truthy' => 1,
  1146. 'zero' => '0',
  1147. ],
  1148. ]);
  1149. $this->assertSame($expected, $request->getParam($toRead));
  1150. }
  1151. /**
  1152. * Test getParam returning a default value.
  1153. */
  1154. public function testGetParamDefault(): void
  1155. {
  1156. $request = new ServerRequest([
  1157. 'params' => [
  1158. 'controller' => 'Articles',
  1159. 'null' => null,
  1160. ],
  1161. ]);
  1162. $this->assertSame('Articles', $request->getParam('controller', 'default'));
  1163. $this->assertSame('default', $request->getParam('null', 'default'));
  1164. $this->assertFalse($request->getParam('unset', false));
  1165. $this->assertNull($request->getParam('unset'));
  1166. }
  1167. /**
  1168. * Data provider for testing reading values with ServerRequest::getParam()
  1169. *
  1170. * @return array
  1171. */
  1172. public static function paramReadingDataProvider(): array
  1173. {
  1174. return [
  1175. [
  1176. 'action',
  1177. 'index',
  1178. ],
  1179. [
  1180. 'baz',
  1181. [
  1182. 'a' => [
  1183. 'b' => 'c',
  1184. ],
  1185. ],
  1186. ],
  1187. [
  1188. 'baz.a.b',
  1189. 'c',
  1190. ],
  1191. [
  1192. 'does_not_exist',
  1193. null,
  1194. ],
  1195. [
  1196. 'admin',
  1197. true,
  1198. ],
  1199. [
  1200. 'truthy',
  1201. 1,
  1202. ],
  1203. [
  1204. 'zero',
  1205. '0',
  1206. ],
  1207. ];
  1208. }
  1209. /**
  1210. * test writing request params with param()
  1211. */
  1212. public function testParamWriting(): void
  1213. {
  1214. $request = new ServerRequest(['url' => '/']);
  1215. $request = $request->withParam('action', 'index');
  1216. $this->assertInstanceOf(
  1217. 'Cake\Http\ServerRequest',
  1218. $request->withParam('some', 'thing'),
  1219. 'Method has not returned $this'
  1220. );
  1221. $request = $request->withParam('Post.null', null);
  1222. $this->assertNull($request->getParam('Post.null'), 'default value should be used.');
  1223. $request = $request->withParam('Post.false', false);
  1224. $this->assertFalse($request->getParam('Post.false'));
  1225. $request = $request->withParam('Post.zero', 0);
  1226. $this->assertSame(0, $request->getParam('Post.zero'));
  1227. $request = $request->withParam('Post.empty', '');
  1228. $this->assertSame('', $request->getParam('Post.empty'));
  1229. $this->assertSame('index', $request->getParam('action'));
  1230. $request = $request->withParam('action', 'edit');
  1231. $this->assertSame('edit', $request->getParam('action'));
  1232. }
  1233. /**
  1234. * Test accept language
  1235. */
  1236. public function testAcceptLanguage(): void
  1237. {
  1238. $request = new ServerRequest();
  1239. // Weird language
  1240. $request = $request->withEnv('HTTP_ACCEPT_LANGUAGE', 'inexistent,en-ca');
  1241. $result = $request->acceptLanguage();
  1242. $this->assertEquals(['inexistent', 'en-ca'], $result, 'Languages do not match');
  1243. // No qualifier
  1244. $request = $request->withEnv('HTTP_ACCEPT_LANGUAGE', 'es_mx,en_ca');
  1245. $result = $request->acceptLanguage();
  1246. $this->assertEquals(['es-mx', 'en-ca'], $result, 'Languages do not match');
  1247. // With qualifier
  1248. $request = $request->withEnv('HTTP_ACCEPT_LANGUAGE', 'en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4');
  1249. $result = $request->acceptLanguage();
  1250. $this->assertEquals(['en-us', 'en', 'pt-br', 'pt'], $result, 'Languages do not match');
  1251. // With spaces
  1252. $request = $request->withEnv('HTTP_ACCEPT_LANGUAGE', 'da, en-gb;q=0.8, en;q=0.7');
  1253. $result = $request->acceptLanguage();
  1254. $this->assertEquals(['da', 'en-gb', 'en'], $result, 'Languages do not match');
  1255. // Checking if requested
  1256. $request = $request->withEnv('HTTP_ACCEPT_LANGUAGE', 'es_mx,en_ca');
  1257. $result = $request->acceptLanguage('en-ca');
  1258. $this->assertTrue($result);
  1259. $result = $request->acceptLanguage('en-CA');
  1260. $this->assertTrue($result);
  1261. $result = $request->acceptLanguage('en-us');
  1262. $this->assertFalse($result);
  1263. $result = $request->acceptLanguage('en-US');
  1264. $this->assertFalse($result);
  1265. }
  1266. /**
  1267. * Test getBody
  1268. */
  1269. public function testGetBody(): void
  1270. {
  1271. $request = new ServerRequest([
  1272. 'input' => 'key=val&some=data',
  1273. ]);
  1274. $result = $request->getBody();
  1275. $this->assertInstanceOf('Psr\Http\Message\StreamInterface', $result);
  1276. $this->assertSame('key=val&some=data', $result->getContents());
  1277. }
  1278. /**
  1279. * Test withBody
  1280. */
  1281. public function testWithBody(): void
  1282. {
  1283. $request = new ServerRequest([
  1284. 'input' => 'key=val&some=data',
  1285. ]);
  1286. $body = $this->getMockBuilder('Psr\Http\Message\StreamInterface')->getMock();
  1287. $new = $request->withBody($body);
  1288. $this->assertNotSame($new, $request);
  1289. $this->assertNotSame($body, $request->getBody());
  1290. $this->assertSame($body, $new->getBody());
  1291. }
  1292. /**
  1293. * Test getUri
  1294. */
  1295. public function testGetUri(): void
  1296. {
  1297. $request = new ServerRequest(['url' => 'articles/view/3']);
  1298. $result = $request->getUri();
  1299. $this->assertInstanceOf('Psr\Http\Message\UriInterface', $result);
  1300. $this->assertSame('/articles/view/3', $result->getPath());
  1301. }
  1302. /**
  1303. * Test withUri
  1304. */
  1305. public function testWithUri(): void
  1306. {
  1307. $request = new ServerRequest([
  1308. 'environment' => [
  1309. 'HTTP_HOST' => 'example.com',
  1310. ],
  1311. 'url' => 'articles/view/3',
  1312. ]);
  1313. $uri = $this->getMockBuilder('Psr\Http\Message\UriInterface')->getMock();
  1314. $new = $request->withUri($uri);
  1315. $this->assertNotSame($new, $request);
  1316. $this->assertNotSame($uri, $request->getUri());
  1317. $this->assertSame($uri, $new->getUri());
  1318. }
  1319. /**
  1320. * Test withUri() and preserveHost
  1321. */
  1322. public function testWithUriPreserveHost(): void
  1323. {
  1324. $request = new ServerRequest([
  1325. 'environment' => [
  1326. 'HTTP_HOST' => 'localhost',
  1327. ],
  1328. 'url' => 'articles/view/3',
  1329. ]);
  1330. $uri = new Uri();
  1331. $uri = $uri->withHost('example.com')
  1332. ->withPort(123)
  1333. ->withPath('articles/view/3');
  1334. $new = $request->withUri($uri, false);
  1335. $this->assertNotSame($new, $request);
  1336. $this->assertSame('example.com:123', $new->getHeaderLine('Host'));
  1337. }
  1338. /**
  1339. * Test withUri() and preserveHost missing the host header
  1340. */
  1341. public function testWithUriPreserveHostNoHostHeader(): void
  1342. {
  1343. $request = new ServerRequest([
  1344. 'url' => 'articles/view/3',
  1345. ]);
  1346. $uri = new Uri();
  1347. $uri = $uri->withHost('example.com')
  1348. ->withPort(123)
  1349. ->withPath('articles/view/3');
  1350. $new = $request->withUri($uri, false);
  1351. $this->assertSame('example.com:123', $new->getHeaderLine('Host'));
  1352. }
  1353. /**
  1354. * Test the cookie() method.
  1355. */
  1356. public function testGetCookie(): void
  1357. {
  1358. $request = new ServerRequest([
  1359. 'cookies' => [
  1360. 'testing' => 'A value in the cookie',
  1361. 'user' => [
  1362. 'remember' => '1',
  1363. ],
  1364. '123' => 'a integer key cookie',
  1365. ],
  1366. ]);
  1367. $this->assertSame('A value in the cookie', $request->getCookie('testing'));
  1368. $this->assertSame('a integer key cookie', $request->getCookie('123'));
  1369. $this->assertNull($request->getCookie('not there'));
  1370. $this->assertSame('default', $request->getCookie('not there', 'default'));
  1371. $this->assertSame('1', $request->getCookie('user.remember'));
  1372. $this->assertSame('1', $request->getCookie('user.remember', 'default'));
  1373. $this->assertSame('default', $request->getCookie('user.not there', 'default'));
  1374. }
  1375. /**
  1376. * Test getCookieParams()
  1377. */
  1378. public function testGetCookieParams(): void
  1379. {
  1380. $cookies = [
  1381. 'testing' => 'A value in the cookie',
  1382. ];
  1383. $request = new ServerRequest(['cookies' => $cookies]);
  1384. $this->assertSame($cookies, $request->getCookieParams());
  1385. }
  1386. /**
  1387. * Test withCookieParams()
  1388. */
  1389. public function testWithCookieParams(): void
  1390. {
  1391. $cookies = [
  1392. 'testing' => 'A value in the cookie',
  1393. ];
  1394. $request = new ServerRequest(['cookies' => $cookies]);
  1395. $new = $request->withCookieParams(['remember_me' => 1]);
  1396. $this->assertNotSame($new, $request);
  1397. $this->assertSame($cookies, $request->getCookieParams());
  1398. $this->assertSame(['remember_me' => 1], $new->getCookieParams());
  1399. }
  1400. /**
  1401. * Test getting a cookie collection from a request.
  1402. */
  1403. public function testGetCookieCollection(): void
  1404. {
  1405. $cookies = [
  1406. 'remember_me' => 1,
  1407. 'color' => 'blue',
  1408. ];
  1409. $request = new ServerRequest(['cookies' => $cookies]);
  1410. $cookies = $request->getCookieCollection();
  1411. $this->assertInstanceOf(CookieCollection::class, $cookies);
  1412. $this->assertCount(2, $cookies);
  1413. $this->assertSame('1', $cookies->get('remember_me')->getValue());
  1414. $this->assertSame('blue', $cookies->get('color')->getValue());
  1415. }
  1416. /**
  1417. * Test replacing cookies from a collection
  1418. */
  1419. public function testWithCookieCollection(): void
  1420. {
  1421. $cookies = new CookieCollection([new Cookie('remember_me', 1), new Cookie('color', 'red')]);
  1422. $request = new ServerRequest(['cookies' => ['bad' => 'goaway']]);
  1423. $new = $request->withCookieCollection($cookies);
  1424. $this->assertNotSame($new, $request, 'Should clone');
  1425. $this->assertSame(['bad' => 'goaway'], $request->getCookieParams());
  1426. $this->assertSame(['remember_me' => '1', 'color' => 'red'], $new->getCookieParams());
  1427. $cookies = $new->getCookieCollection();
  1428. $this->assertCount(2, $cookies);
  1429. $this->assertSame('red', $cookies->get('color')->getValue());
  1430. }
  1431. /**
  1432. * TestAllowMethod
  1433. */
  1434. public function testAllowMethod(): void
  1435. {
  1436. $request = new ServerRequest(['environment' => [
  1437. 'url' => '/posts/edit/1',
  1438. 'REQUEST_METHOD' => 'PUT',
  1439. ]]);
  1440. $this->assertTrue($request->allowMethod('put'));
  1441. $request = $request->withEnv('REQUEST_METHOD', 'DELETE');
  1442. $this->assertTrue($request->allowMethod(['post', 'delete']));
  1443. }
  1444. /**
  1445. * Test allowMethod throwing exception
  1446. */
  1447. public function testAllowMethodException(): void
  1448. {
  1449. $request = new ServerRequest([
  1450. 'url' => '/posts/edit/1',
  1451. 'environment' => ['REQUEST_METHOD' => 'PUT'],
  1452. ]);
  1453. try {
  1454. $request->allowMethod(['POST', 'DELETE']);
  1455. $this->fail('An expected exception has not been raised.');
  1456. } catch (MethodNotAllowedException $e) {
  1457. $this->assertEquals(['Allow' => 'POST, DELETE'], $e->getHeaders());
  1458. }
  1459. $this->expectException(MethodNotAllowedException::class);
  1460. $request->allowMethod('POST');
  1461. }
  1462. /**
  1463. * Tests getting the session from the request
  1464. */
  1465. public function testGetSession(): void
  1466. {
  1467. $session = new Session();
  1468. $request = new ServerRequest(['session' => $session]);
  1469. $this->assertSame($session, $request->getSession());
  1470. $request = new ServerRequest();
  1471. $this->assertEquals($session, $request->getSession());
  1472. }
  1473. public function testGetFlash(): void
  1474. {
  1475. $request = new ServerRequest();
  1476. $this->assertInstanceOf(FlashMessage::class, $request->getFlash());
  1477. }
  1478. /**
  1479. * Test the content type method.
  1480. */
  1481. public function testContentType(): void
  1482. {
  1483. $request = new ServerRequest([
  1484. 'environment' => ['HTTP_CONTENT_TYPE' => 'application/json'],
  1485. ]);
  1486. $this->assertSame('application/json', $request->contentType());
  1487. $request = new ServerRequest([
  1488. 'environment' => ['HTTP_CONTENT_TYPE' => 'application/xml'],
  1489. ]);
  1490. $this->assertSame('application/xml', $request->contentType(), 'prefer non http header.');
  1491. }
  1492. /**
  1493. * Test updating params in a psr7 fashion.
  1494. */
  1495. public function testWithParam(): void
  1496. {
  1497. $request = new ServerRequest([
  1498. 'params' => ['controller' => 'Articles'],
  1499. ]);
  1500. $result = $request->withParam('action', 'view');
  1501. $this->assertNotSame($result, $request, 'New instance should be made');
  1502. $this->assertNull($request->getParam('action'), 'No side-effect on original');
  1503. $this->assertSame('view', $result->getParam('action'));
  1504. $result = $request->withParam('action', 'index')
  1505. ->withParam('plugin', 'DebugKit')
  1506. ->withParam('prefix', 'Admin');
  1507. $this->assertNotSame($result, $request, 'New instance should be made');
  1508. $this->assertNull($request->getParam('action'), 'No side-effect on original');
  1509. $this->assertSame('index', $result->getParam('action'));
  1510. $this->assertSame('DebugKit', $result->getParam('plugin'));
  1511. $this->assertSame('Admin', $result->getParam('prefix'));
  1512. }
  1513. /**
  1514. * Test getting the parsed body parameters.
  1515. */
  1516. public function testGetParsedBody(): void
  1517. {
  1518. $data = ['title' => 'First', 'body' => 'Best Article!'];
  1519. $request = new ServerRequest(['post' => $data]);
  1520. $this->assertSame($data, $request->getParsedBody());
  1521. $request = new ServerRequest();
  1522. $this->assertSame([], $request->getParsedBody());
  1523. }
  1524. /**
  1525. * Test replacing the parsed body parameters.
  1526. */
  1527. public function testWithParsedBody(): void
  1528. {
  1529. $data = ['title' => 'First', 'body' => 'Best Article!'];
  1530. $request = new ServerRequest([]);
  1531. $new = $request->withParsedBody($data);
  1532. $this->assertNotSame($request, $new);
  1533. $this->assertSame([], $request->getParsedBody());
  1534. $this->assertSame($data, $new->getParsedBody());
  1535. }
  1536. /**
  1537. * Test updating POST data in a psr7 fashion.
  1538. */
  1539. public function testWithData(): void
  1540. {
  1541. $request = new ServerRequest([
  1542. 'post' => [
  1543. 'Model' => [
  1544. 'field' => 'value',
  1545. ],
  1546. ],
  1547. ]);
  1548. $result = $request->withData('Model.new_value', 'new value');
  1549. $this->assertNull($request->getData('Model.new_value'), 'Original request should not change.');
  1550. $this->assertNotSame($result, $request);
  1551. $this->assertSame('new value', $result->getData('Model.new_value'));
  1552. $this->assertSame('new value', $result->getData()['Model']['new_value']);
  1553. $this->assertSame('value', $result->getData('Model.field'));
  1554. }
  1555. /**
  1556. * Test removing data from a request
  1557. */
  1558. public function testWithoutData(): void
  1559. {
  1560. $request = new ServerRequest([
  1561. 'post' => [
  1562. 'Model' => [
  1563. 'id' => 1,
  1564. 'field' => 'value',
  1565. ],
  1566. ],
  1567. ]);
  1568. $updated = $request->withoutData('Model.field');
  1569. $this->assertNotSame($updated, $request);
  1570. $this->assertSame('value', $request->getData('Model.field'), 'Original request should not change.');
  1571. $this->assertNull($updated->getData('Model.field'), 'data removed from updated request');
  1572. $this->assertFalse(isset($updated->getData()['Model']['field']), 'data removed from updated request');
  1573. }
  1574. /**
  1575. * Test updating POST data when keys don't exist
  1576. */
  1577. public function testWithDataMissingIntermediaryKeys(): void
  1578. {
  1579. $request = new ServerRequest([
  1580. 'post' => [
  1581. 'Model' => [
  1582. 'field' => 'value',
  1583. ],
  1584. ],
  1585. ]);
  1586. $result = $request->withData('Model.field.new_value', 'new value');
  1587. $this->assertSame(
  1588. 'new value',
  1589. $result->getData('Model.field.new_value')
  1590. );
  1591. $this->assertSame(
  1592. 'new value',
  1593. $result->getData()['Model']['field']['new_value']
  1594. );
  1595. }
  1596. /**
  1597. * Test updating POST data with falsey values
  1598. */
  1599. public function testWithDataFalseyValues(): void
  1600. {
  1601. $request = new ServerRequest([
  1602. 'post' => [],
  1603. ]);
  1604. $result = $request->withData('false', false)
  1605. ->withData('null', null)
  1606. ->withData('empty_string', '')
  1607. ->withData('zero', 0)
  1608. ->withData('zero_string', '0');
  1609. $expected = [
  1610. 'false' => false,
  1611. 'null' => null,
  1612. 'empty_string' => '',
  1613. 'zero' => 0,
  1614. 'zero_string' => '0',
  1615. ];
  1616. $this->assertSame($expected, $result->getData());
  1617. }
  1618. /**
  1619. * Test setting attributes.
  1620. */
  1621. public function testWithAttribute(): void
  1622. {
  1623. $request = new ServerRequest([]);
  1624. $this->assertNull($request->getAttribute('key'));
  1625. $this->assertSame('default', $request->getAttribute('key', 'default'));
  1626. $new = $request->withAttribute('key', 'value');
  1627. $this->assertNotEquals($new, $request, 'Should be different');
  1628. $this->assertNull($request->getAttribute('key'), 'Old instance not modified');
  1629. $this->assertSame('value', $new->getAttribute('key'));
  1630. $update = $new->withAttribute('key', ['complex']);
  1631. $this->assertNotEquals($update, $new, 'Should be different');
  1632. $this->assertSame(['complex'], $update->getAttribute('key'));
  1633. }
  1634. /**
  1635. * Test that replacing the session can be done via withAttribute()
  1636. */
  1637. public function testWithAttributeSession(): void
  1638. {
  1639. $request = new ServerRequest([]);
  1640. $request->getSession()->write('attrKey', 'session-value');
  1641. $update = $request->withAttribute('session', Session::create());
  1642. $this->assertSame('session-value', $request->getAttribute('session')->read('attrKey'));
  1643. $this->assertNotSame($request->getAttribute('session'), $update->getAttribute('session'));
  1644. $this->assertNotSame($request->getSession()->read('attrKey'), $update->getSession()->read('attrKey'));
  1645. }
  1646. /**
  1647. * Test getting all attributes.
  1648. */
  1649. public function testGetAttributes(): void
  1650. {
  1651. $request = new ServerRequest([]);
  1652. $new = $request->withAttribute('key', 'value')
  1653. ->withAttribute('nully', null)
  1654. ->withAttribute('falsey', false);
  1655. $this->assertFalse($new->getAttribute('falsey'));
  1656. $this->assertNull($new->getAttribute('nully'));
  1657. $expected = [
  1658. 'key' => 'value',
  1659. 'nully' => null,
  1660. 'falsey' => false,
  1661. 'params' => [
  1662. 'plugin' => null,
  1663. 'controller' => null,
  1664. 'action' => null,
  1665. '_ext' => null,
  1666. 'pass' => [],
  1667. ],
  1668. 'webroot' => '',
  1669. 'base' => '',
  1670. 'here' => '/',
  1671. ];
  1672. $this->assertEquals($expected, $new->getAttributes());
  1673. }
  1674. /**
  1675. * Test unsetting attributes.
  1676. */
  1677. public function testWithoutAttribute(): void
  1678. {
  1679. $request = new ServerRequest([]);
  1680. $new = $request->withAttribute('key', 'value');
  1681. $update = $request->withoutAttribute('key');
  1682. $this->assertNotEquals($update, $new, 'Should be different');
  1683. $this->assertNull($update->getAttribute('key'));
  1684. }
  1685. /**
  1686. * Test that withoutAttribute() cannot remove emulatedAttributes properties.
  1687. *
  1688. * @dataProvider emulatedPropertyProvider
  1689. */
  1690. public function testWithoutAttributesDenyEmulatedProperties(string $prop): void
  1691. {
  1692. $this->expectException(InvalidArgumentException::class);
  1693. $request = new ServerRequest([]);
  1694. $request->withoutAttribute($prop);
  1695. }
  1696. /**
  1697. * Test the requestTarget methods.
  1698. */
  1699. public function testWithRequestTarget(): void
  1700. {
  1701. $request = new ServerRequest([
  1702. 'environment' => [
  1703. 'REQUEST_URI' => '/articles/view/1',
  1704. 'QUERY_STRING' => 'comments=1&open=0',
  1705. ],
  1706. 'base' => '/basedir',
  1707. ]);
  1708. $this->assertSame(
  1709. '/articles/view/1?comments=1&open=0',
  1710. $request->getRequestTarget(),
  1711. 'Should not include basedir.'
  1712. );
  1713. $new = $request->withRequestTarget('/articles/view/3');
  1714. $this->assertNotSame($new, $request);
  1715. $this->assertSame(
  1716. '/articles/view/1?comments=1&open=0',
  1717. $request->getRequestTarget(),
  1718. 'should be unchanged.'
  1719. );
  1720. $this->assertSame('/articles/view/3', $new->getRequestTarget(), 'reflects method call');
  1721. }
  1722. /**
  1723. * Test withEnv()
  1724. */
  1725. public function testWithEnv(): void
  1726. {
  1727. $request = new ServerRequest();
  1728. $newRequest = $request->withEnv('HTTP_HOST', 'cakephp.org');
  1729. $this->assertNotSame($request, $newRequest);
  1730. $this->assertSame('cakephp.org', $newRequest->getEnv('HTTP_HOST'));
  1731. }
  1732. /**
  1733. * Test getEnv()
  1734. */
  1735. public function testGetEnv(): void
  1736. {
  1737. $request = new ServerRequest([
  1738. 'environment' => ['TEST' => 'ing'],
  1739. ]);
  1740. //Test default null
  1741. $this->assertNull($request->getEnv('Foo'));
  1742. //Test default set
  1743. $this->assertSame('Bar', $request->getEnv('Foo', 'Bar'));
  1744. //Test env() fallback
  1745. $this->assertSame('ing', $request->getEnv('test'));
  1746. }
  1747. /**
  1748. * Data provider for emulated property tests.
  1749. *
  1750. * @return array
  1751. */
  1752. public static function emulatedPropertyProvider(): array
  1753. {
  1754. return [
  1755. ['here'],
  1756. ['params'],
  1757. ['base'],
  1758. ['webroot'],
  1759. ['session'],
  1760. ];
  1761. }
  1762. }