ServerRequestTest.php 67 KB

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