| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911 |
- <?php
- declare(strict_types=1);
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 2.0.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Http;
- use BadMethodCallException;
- use Cake\Core\Configure;
- use Cake\Http\Cookie\Cookie;
- use Cake\Http\Cookie\CookieCollection;
- use Cake\Http\Exception\MethodNotAllowedException;
- use Cake\Http\FlashMessage;
- use Cake\Http\ServerRequest;
- use Cake\Http\Session;
- use Cake\TestSuite\TestCase;
- use InvalidArgumentException;
- use Laminas\Diactoros\UploadedFile;
- use Laminas\Diactoros\Uri;
- /**
- * ServerRequest Test
- */
- class ServerRequestTest extends TestCase
- {
- /**
- * Test custom detector with extra arguments.
- */
- public function testCustomArgsDetector(): void
- {
- $request = new ServerRequest();
- $request->addDetector('controller', function ($request, $name) {
- return $request->getParam('controller') === $name;
- });
- $request = $request->withParam('controller', 'cake');
- $this->assertTrue($request->is('controller', 'cake'));
- $this->assertFalse($request->is('controller', 'nonExistingController'));
- $this->assertTrue($request->isController('cake'));
- $this->assertFalse($request->isController('nonExistingController'));
- }
- /**
- * Test the header detector.
- */
- public function testHeaderDetector(): void
- {
- $request = new ServerRequest();
- $request->addDetector('host', ['header' => ['host' => 'cakephp.org']]);
- $request = $request->withEnv('HTTP_HOST', 'cakephp.org');
- $this->assertTrue($request->is('host'));
- $request = $request->withEnv('HTTP_HOST', 'php.net');
- $this->assertFalse($request->is('host'));
- }
- /**
- * Test the accept header detector.
- */
- public function testExtensionDetector(): void
- {
- $request = new ServerRequest();
- $request = $request->withParam('_ext', 'json');
- $this->assertTrue($request->is('json'));
- $request = new ServerRequest();
- $request = $request->withParam('_ext', 'xml');
- $this->assertFalse($request->is('json'));
- }
- /**
- * Test the accept header detector.
- */
- public function testAcceptHeaderDetector(): void
- {
- $request = new ServerRequest();
- $request = $request->withEnv('HTTP_ACCEPT', 'application/json, text/plain, */*');
- $this->assertTrue($request->is('json'));
- $request = new ServerRequest();
- $request = $request->withEnv('HTTP_ACCEPT', 'text/plain, */*');
- $this->assertFalse($request->is('json'));
- $request = new ServerRequest();
- $request = $request->withEnv('HTTP_ACCEPT', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8');
- $this->assertFalse($request->is('json'));
- $this->assertFalse($request->is('xml'));
- $this->assertFalse($request->is('xml'));
- }
- public function testConstructor(): void
- {
- $request = new ServerRequest();
- $this->assertInstanceOf(FlashMessage::class, $request->getAttribute('flash'));
- }
- /**
- * Test construction with query data
- */
- public function testConstructionQueryData(): void
- {
- $data = [
- 'query' => [
- 'one' => 'param',
- 'two' => 'banana',
- ],
- 'url' => 'some/path',
- ];
- $request = new ServerRequest($data);
- $this->assertSame('param', $request->getQuery('one'));
- $this->assertEquals($data['query'], $request->getQueryParams());
- $this->assertSame('/some/path', $request->getRequestTarget());
- }
- /**
- * Test constructing with a string url.
- */
- public function testConstructStringUrlIgnoreServer(): void
- {
- $request = new ServerRequest([
- 'url' => '/articles/view/1',
- 'environment' => ['REQUEST_URI' => '/some/other/path'],
- ]);
- $this->assertSame('/articles/view/1', $request->getUri()->getPath());
- $request = new ServerRequest(['url' => '/']);
- $this->assertSame('/', $request->getUri()->getPath());
- }
- /**
- * Test that querystring args provided in the URL string are parsed.
- */
- public function testQueryStringParsingFromInputUrl(): void
- {
- $request = new ServerRequest(['url' => 'some/path?one=something&two=else']);
- $expected = ['one' => 'something', 'two' => 'else'];
- $this->assertEquals($expected, $request->getQueryParams());
- $this->assertSame('/some/path', $request->getUri()->getPath());
- $this->assertSame('one=something&two=else', $request->getUri()->getQuery());
- }
- /**
- * Test that querystrings are handled correctly.
- */
- public function testQueryStringAndNamedParams(): void
- {
- $config = ['environment' => ['REQUEST_URI' => '/tasks/index?ts=123456']];
- $request = new ServerRequest($config);
- $this->assertSame('/tasks/index', $request->getRequestTarget());
- $config = ['environment' => ['REQUEST_URI' => '/some/path?url=http://cakephp.org']];
- $request = new ServerRequest($config);
- $this->assertSame('/some/path', $request->getRequestTarget());
- $config = ['environment' => [
- 'REQUEST_URI' => Configure::read('App.fullBaseUrl') . '/other/path?url=http://cakephp.org',
- ]];
- $request = new ServerRequest($config);
- $this->assertSame('/other/path', $request->getRequestTarget());
- }
- /**
- * Test that URL in path is handled correctly.
- */
- public function testUrlInPath(): void
- {
- $config = ['environment' => ['REQUEST_URI' => '/jump/http://cakephp.org']];
- $request = new ServerRequest($config);
- $this->assertSame('/jump/http://cakephp.org', $request->getRequestTarget());
- $config = ['environment' => [
- 'REQUEST_URI' => Configure::read('App.fullBaseUrl') . '/jump/http://cakephp.org',
- ]];
- $request = new ServerRequest($config);
- $this->assertSame('/jump/http://cakephp.org', $request->getRequestTarget());
- }
- /**
- * Test getPath().
- */
- public function testGetPath(): void
- {
- $request = new ServerRequest(['url' => '']);
- $this->assertSame('/', $request->getPath());
- $request = new ServerRequest(['url' => 'some/path?one=something&two=else']);
- $this->assertSame('/some/path', $request->getPath());
- $request = $request->withRequestTarget('/foo/bar?x=y');
- $this->assertSame('/foo/bar', $request->getPath());
- }
- /**
- * Test parsing POST data into the object.
- */
- public function testPostParsing(): void
- {
- $post = [
- 'Article' => ['title'],
- ];
- $request = new ServerRequest(compact('post'));
- $this->assertEquals($post, $request->getData());
- $post = ['one' => 1, 'two' => 'three'];
- $request = new ServerRequest(compact('post'));
- $this->assertEquals($post, $request->getData());
- $post = [
- 'Article' => ['title' => 'Testing'],
- 'action' => 'update',
- ];
- $request = new ServerRequest(compact('post'));
- $this->assertEquals($post, $request->getData());
- }
- /**
- * Test that the constructor uses uploaded file objects
- * if they are present. This could happen in test scenarios.
- */
- public function testFilesObject(): void
- {
- $file = new UploadedFile(
- __FILE__,
- 123,
- UPLOAD_ERR_OK,
- 'test.php',
- 'text/plain'
- );
- $request = new ServerRequest(['files' => ['avatar' => $file]]);
- $this->assertSame(['avatar' => $file], $request->getUploadedFiles());
- }
- /**
- * Test passing an empty files list.
- */
- public function testFilesWithEmptyList(): void
- {
- $request = new ServerRequest([
- 'files' => [],
- ]);
- $this->assertEmpty($request->getData());
- $this->assertEmpty($request->getUploadedFiles());
- }
- /**
- * Test replacing files.
- */
- public function testWithUploadedFiles(): void
- {
- $file = new UploadedFile(
- __FILE__,
- 123,
- UPLOAD_ERR_OK,
- 'test.php',
- 'text/plain'
- );
- $request = new ServerRequest();
- $new = $request->withUploadedFiles(['picture' => $file]);
- $this->assertSame([], $request->getUploadedFiles());
- $this->assertNotSame($new, $request);
- $this->assertSame(['picture' => $file], $new->getUploadedFiles());
- }
- /**
- * Test getting a single file
- */
- public function testGetUploadedFile(): void
- {
- $file = new UploadedFile(
- __FILE__,
- 123,
- UPLOAD_ERR_OK,
- 'test.php',
- 'text/plain'
- );
- $request = new ServerRequest();
- $new = $request->withUploadedFiles(['picture' => $file]);
- $this->assertNull($new->getUploadedFile(''));
- $this->assertSame($file, $new->getUploadedFile('picture'));
- $new = $request->withUploadedFiles([
- 'pictures' => [
- [
- 'image' => $file,
- ],
- ],
- ]);
- $this->assertNull($new->getUploadedFile('pictures'));
- $this->assertNull($new->getUploadedFile('pictures.0'));
- $this->assertNull($new->getUploadedFile('pictures.1'));
- $this->assertSame($file, $new->getUploadedFile('pictures.0.image'));
- }
- /**
- * Test replacing files with an invalid file
- */
- public function testWithUploadedFilesInvalidFile(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Invalid file at `avatar`');
- $request = new ServerRequest();
- $request->withUploadedFiles(['avatar' => 'not a file']);
- }
- /**
- * Test replacing files with an invalid file
- */
- public function testWithUploadedFilesInvalidFileNested(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Invalid file at `user.avatar`');
- $request = new ServerRequest();
- $request->withUploadedFiles(['user' => ['avatar' => 'not a file']]);
- }
- /**
- * Test the clientIp method.
- */
- public function testClientIp(): void
- {
- $request = new ServerRequest(['environment' => [
- 'HTTP_X_FORWARDED_FOR' => '192.168.1.5, 10.0.1.1, proxy.com, real.ip',
- 'HTTP_X_REAL_IP' => '192.168.1.1',
- 'HTTP_CLIENT_IP' => '192.168.1.2',
- 'REMOTE_ADDR' => '192.168.1.3',
- ]]);
- $request->trustProxy = true;
- $this->assertSame('real.ip', $request->clientIp());
- $request = $request->withEnv('HTTP_X_FORWARDED_FOR', '');
- $this->assertSame('192.168.1.1', $request->clientIp());
- $request = $request->withEnv('HTTP_X_REAL_IP', '');
- $this->assertSame('192.168.1.2', $request->clientIp());
- $request->trustProxy = false;
- $this->assertSame('192.168.1.3', $request->clientIp());
- $request = $request->withEnv('HTTP_X_FORWARDED_FOR', '');
- $this->assertSame('192.168.1.3', $request->clientIp());
- $request = $request->withEnv('HTTP_CLIENT_IP', '');
- $this->assertSame('192.168.1.3', $request->clientIp());
- }
- /**
- * test clientIp method with trusted proxies
- */
- public function testClientIpWithTrustedProxies(): void
- {
- $request = new ServerRequest(['environment' => [
- 'HTTP_X_FORWARDED_FOR' => 'real.ip, 192.168.1.0, 192.168.1.2, 192.168.1.3',
- 'HTTP_X_REAL_IP' => '192.168.1.1',
- 'HTTP_CLIENT_IP' => '192.168.1.2',
- 'REMOTE_ADDR' => '192.168.1.4',
- ]]);
- $request->setTrustedProxies([
- '192.168.1.0',
- '192.168.1.1',
- '192.168.1.2',
- '192.168.1.3',
- ]);
- $this->assertSame('real.ip', $request->clientIp());
- $request = $request->withEnv(
- 'HTTP_X_FORWARDED_FOR',
- 'spoof.fake.ip, real.ip, 192.168.1.0, 192.168.1.2, 192.168.1.3'
- );
- $this->assertSame('192.168.1.3', $request->clientIp());
- $request = $request->withEnv('HTTP_X_FORWARDED_FOR', '');
- $this->assertSame('192.168.1.1', $request->clientIp());
- $request->trustProxy = false;
- $this->assertSame('192.168.1.4', $request->clientIp());
- }
- /**
- * Test the referrer function.
- */
- public function testReferer(): void
- {
- $request = new ServerRequest(['webroot' => '/']);
- $request = $request->withEnv('HTTP_REFERER', 'http://cakephp.org');
- $result = $request->referer(false);
- $this->assertSame('http://cakephp.org', $result);
- $request = $request->withEnv('HTTP_REFERER', '');
- $result = $request->referer(true);
- $this->assertNull($result);
- $result = $request->referer(false);
- $this->assertNull($result);
- $request = $request->withEnv('HTTP_REFERER', Configure::read('App.fullBaseUrl') . '/some/path');
- $result = $request->referer();
- $this->assertSame('/some/path', $result);
- $request = $request->withEnv('HTTP_REFERER', Configure::read('App.fullBaseUrl') . '///cakephp.org/');
- $result = $request->referer();
- $this->assertSame('/', $result); // Avoid returning scheme-relative URLs.
- $request = $request->withEnv('HTTP_REFERER', Configure::read('App.fullBaseUrl') . '/0');
- $result = $request->referer();
- $this->assertSame('/0', $result);
- $request = $request->withEnv('HTTP_REFERER', Configure::read('App.fullBaseUrl') . '/');
- $result = $request->referer();
- $this->assertSame('/', $result);
- $request = $request->withEnv('HTTP_REFERER', Configure::read('App.fullBaseUrl') . '/some/path');
- $result = $request->referer(false);
- $this->assertSame(Configure::read('App.fullBaseUrl') . '/some/path', $result);
- }
- /**
- * Test referer() with a base path that duplicates the
- * first segment.
- */
- public function testRefererBasePath(): void
- {
- $request = new ServerRequest([
- 'url' => '/waves/users/login',
- 'webroot' => '/waves/',
- 'base' => '/waves',
- ]);
- $request = $request->withEnv('HTTP_REFERER', Configure::read('App.fullBaseUrl') . '/waves/waves/add');
- $result = $request->referer();
- $this->assertSame('/waves/add', $result);
- }
- /**
- * test the simple uses of is()
- */
- public function testIsHttpMethods(): void
- {
- $request = new ServerRequest();
- $request = $request->withEnv('REQUEST_METHOD', 'GET');
- $this->assertTrue($request->is('get'));
- $request = $request->withEnv('REQUEST_METHOD', 'POST');
- $this->assertTrue($request->is('POST'));
- $request = $request->withEnv('REQUEST_METHOD', 'PUT');
- $this->assertTrue($request->is('put'));
- $this->assertFalse($request->is('get'));
- $request = $request->withEnv('REQUEST_METHOD', 'DELETE');
- $this->assertTrue($request->is('delete'));
- $this->assertTrue($request->isDelete());
- $request = $request->withEnv('REQUEST_METHOD', 'delete');
- $this->assertFalse($request->is('delete'));
- }
- public function testExceptionForInvalidType()
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('No detector set for type `nonexistent`');
- $request = new ServerRequest();
- $this->assertFalse($request->is('nonexistent'));
- }
- /**
- * Test is() with JSON and XML.
- */
- public function testIsJsonAndXml(): void
- {
- $request = new ServerRequest();
- $request = $request->withEnv('HTTP_ACCEPT', 'application/json, text/plain, */*');
- $this->assertTrue($request->is('json'));
- $request = new ServerRequest();
- $request = $request->withEnv('HTTP_ACCEPT', 'application/xml, text/plain, */*');
- $this->assertTrue($request->is('xml'));
- $request = new ServerRequest();
- $request = $request->withEnv('HTTP_ACCEPT', 'text/xml, */*');
- $this->assertTrue($request->is('xml'));
- }
- /**
- * Test is() with multiple types.
- */
- public function testIsMultiple(): void
- {
- $request = new ServerRequest();
- $request = $request->withEnv('REQUEST_METHOD', 'GET');
- $this->assertTrue($request->is(['get', 'post']));
- $request = $request->withEnv('REQUEST_METHOD', 'POST');
- $this->assertTrue($request->is(['get', 'post']));
- $request = $request->withEnv('REQUEST_METHOD', 'PUT');
- $this->assertFalse($request->is(['get', 'post']));
- }
- /**
- * Test isAll()
- */
- public function testIsAll(): void
- {
- $request = new ServerRequest();
- $request = $request->withEnv('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
- $request = $request->withEnv('REQUEST_METHOD', 'GET');
- $this->assertTrue($request->isAll(['ajax', 'get']));
- $this->assertFalse($request->isAll(['post', 'get']));
- $this->assertFalse($request->isAll(['ajax', 'post']));
- }
- /**
- * Test getMethod()
- */
- public function testGetMethod(): void
- {
- $request = new ServerRequest([
- 'environment' => ['REQUEST_METHOD' => 'delete'],
- ]);
- $this->assertSame('delete', $request->getMethod());
- }
- /**
- * Test withMethod()
- */
- public function testWithMethod(): void
- {
- $request = new ServerRequest([
- 'environment' => ['REQUEST_METHOD' => 'delete'],
- ]);
- $new = $request->withMethod('put');
- $this->assertNotSame($new, $request);
- $this->assertSame('delete', $request->getMethod());
- $this->assertSame('put', $new->getMethod());
- }
- /**
- * Test withMethod() and invalid data
- */
- public function testWithMethodInvalid(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Unsupported HTTP method `no good` provided');
- $request = new ServerRequest([
- 'environment' => ['REQUEST_METHOD' => 'delete'],
- ]);
- $request->withMethod('no good');
- }
- /**
- * Test getProtocolVersion()
- */
- public function testGetProtocolVersion(): void
- {
- $request = new ServerRequest();
- $this->assertSame('1.1', $request->getProtocolVersion());
- // SERVER var.
- $request = new ServerRequest([
- 'environment' => ['SERVER_PROTOCOL' => 'HTTP/1.0'],
- ]);
- $this->assertSame('1.0', $request->getProtocolVersion());
- }
- /**
- * Test withProtocolVersion()
- */
- public function testWithProtocolVersion(): void
- {
- $request = new ServerRequest();
- $new = $request->withProtocolVersion('1.0');
- $this->assertNotSame($new, $request);
- $this->assertSame('1.1', $request->getProtocolVersion());
- $this->assertSame('1.0', $new->getProtocolVersion());
- }
- /**
- * Test withProtocolVersion() and invalid data
- */
- public function testWithProtocolVersionInvalid(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Unsupported protocol version `no good` provided');
- $request = new ServerRequest();
- $request->withProtocolVersion('no good');
- }
- /**
- * Test host retrieval.
- */
- public function testHost(): void
- {
- $request = new ServerRequest(['environment' => [
- 'HTTP_HOST' => 'localhost',
- 'HTTP_X_FORWARDED_HOST' => 'cakephp.org',
- ]]);
- $this->assertSame('localhost', $request->host());
- $request->trustProxy = true;
- $this->assertSame('cakephp.org', $request->host());
- }
- /**
- * test port retrieval.
- */
- public function testPort(): void
- {
- $request = new ServerRequest(['environment' => ['SERVER_PORT' => '80']]);
- $this->assertSame('80', $request->port());
- $request = $request->withEnv('SERVER_PORT', '443');
- $request = $request->withEnv('HTTP_X_FORWARDED_PORT', '80');
- $this->assertSame('443', $request->port());
- $request->trustProxy = true;
- $this->assertSame('80', $request->port());
- }
- /**
- * test domain retrieval.
- */
- public function testDomain(): void
- {
- $request = new ServerRequest(['environment' => ['HTTP_HOST' => 'something.example.com']]);
- $this->assertSame('example.com', $request->domain());
- $request = $request->withEnv('HTTP_HOST', 'something.example.co.uk');
- $this->assertSame('example.co.uk', $request->domain(2));
- }
- /**
- * Test scheme() method.
- */
- public function testScheme(): void
- {
- $request = new ServerRequest(['environment' => ['HTTPS' => 'on']]);
- $this->assertSame('https', $request->scheme());
- $request = $request->withEnv('HTTPS', '');
- $this->assertSame('http', $request->scheme());
- $request = $request->withEnv('HTTP_X_FORWARDED_PROTO', 'https');
- $request->trustProxy = true;
- $this->assertSame('https', $request->scheme());
- }
- /**
- * test getting subdomains for a host.
- */
- public function testSubdomain(): void
- {
- $request = new ServerRequest(['environment' => ['HTTP_HOST' => 'something.example.com']]);
- $this->assertEquals(['something'], $request->subdomains());
- $request = $request->withEnv('HTTP_HOST', 'www.something.example.com');
- $this->assertEquals(['www', 'something'], $request->subdomains());
- $request = $request->withEnv('HTTP_HOST', 'www.something.example.co.uk');
- $this->assertEquals(['www', 'something'], $request->subdomains(2));
- $request = $request->withEnv('HTTP_HOST', 'example.co.uk');
- $this->assertEquals([], $request->subdomains(2));
- }
- /**
- * Test AJAX, flash and friends
- */
- public function testIsAjax(): void
- {
- $request = new ServerRequest();
- $request = $request->withEnv('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
- $this->assertTrue($request->is('ajax'));
- $request = $request->withEnv('HTTP_X_REQUESTED_WITH', 'XMLHTTPREQUEST');
- $this->assertFalse($request->is('ajax'));
- $this->assertFalse($request->isAjax());
- }
- /**
- * Test __call exceptions
- */
- public function testMagicCallExceptionOnUnknownMethod(): void
- {
- $this->expectException(BadMethodCallException::class);
- $request = new ServerRequest();
- $request->IamABanana();
- }
- /**
- * Test is(ssl)
- */
- public function testIsSsl(): void
- {
- $request = new ServerRequest();
- $request = $request->withEnv('HTTPS', 'on');
- $this->assertTrue($request->is('https'));
- $request = $request->withEnv('HTTPS', '1');
- $this->assertTrue($request->is('https'));
- $request = $request->withEnv('HTTPS', 'I am not empty');
- $this->assertFalse($request->is('https'));
- $request = $request->withEnv('HTTPS', 'off');
- $this->assertFalse($request->is('https'));
- $request = $request->withEnv('HTTPS', '');
- $this->assertFalse($request->is('https'));
- }
- /**
- * Test adding detectors and having them work.
- */
- public function testAddDetector(): void
- {
- $request = new ServerRequest();
- ServerRequest::addDetector('closure', function ($request) {
- return true;
- });
- $this->assertTrue($request->is('closure'));
- ServerRequest::addDetector('get', function ($request) {
- return $request->getEnv('REQUEST_METHOD') === 'GET';
- });
- $request = $request->withEnv('REQUEST_METHOD', 'GET');
- $this->assertTrue($request->is('get'));
- ServerRequest::addDetector('compare', ['env' => 'TEST_VAR', 'value' => 'something']);
- $request = $request->withEnv('TEST_VAR', 'something');
- $this->assertTrue($request->is('compare'), 'Value match failed.');
- $request = $request->withEnv('TEST_VAR', 'wrong');
- $this->assertFalse($request->is('compare'), 'Value mis-match failed.');
- ServerRequest::addDetector('compareCamelCase', ['env' => 'TEST_VAR', 'value' => 'foo']);
- $request = $request->withEnv('TEST_VAR', 'foo');
- $this->assertTrue($request->is('compareCamelCase'), 'Value match failed.');
- $this->assertTrue($request->is('comparecamelcase'), 'detectors should be case insensitive');
- $this->assertTrue($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
- $request = $request->withEnv('TEST_VAR', 'not foo');
- $this->assertFalse($request->is('compareCamelCase'), 'Value match failed.');
- $this->assertFalse($request->is('comparecamelcase'), 'detectors should be case insensitive');
- $this->assertFalse($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
- ServerRequest::addDetector('banana', ['env' => 'TEST_VAR', 'pattern' => '/^ban.*$/']);
- $request = $request->withEnv('TEST_VAR', 'banana');
- $this->assertTrue($request->isBanana());
- $request = $request->withEnv('TEST_VAR', 'wrong value');
- $this->assertFalse($request->isBanana());
- ServerRequest::addDetector('mobile', ['env' => 'HTTP_USER_AGENT', 'options' => ['Imagination']]);
- $request = $request->withEnv('HTTP_USER_AGENT', 'Imagination land');
- $this->assertTrue($request->isMobile());
- ServerRequest::addDetector('index', ['param' => 'action', 'value' => 'index']);
- $request = $request->withParam('action', 'index');
- $request->clearDetectorCache();
- $this->assertTrue($request->isIndex());
- $request = $request->withParam('action', 'add');
- $request->clearDetectorCache();
- $this->assertFalse($request->isIndex());
- ServerRequest::addDetector('withParams', function ($request, array $params) {
- foreach ($params as $name => $value) {
- if ($request->getParam($name) != $value) {
- return false;
- }
- }
- return true;
- });
- $request = $request->withParam('controller', 'Pages')->withParam('action', 'index');
- $request->clearDetectorCache();
- $this->assertTrue($request->isWithParams(['controller' => 'Pages', 'action' => 'index']));
- $request = $request->withParam('controller', 'Posts');
- $request->clearDetectorCache();
- $this->assertFalse($request->isWithParams(['controller' => 'Pages', 'action' => 'index']));
- ServerRequest::addDetector('callme', function ($request) {
- return $request->getAttribute('return');
- });
- $request = $request->withAttribute('return', true);
- $request->clearDetectorCache();
- $this->assertTrue($request->isCallMe());
- ServerRequest::addDetector('extension', ['param' => '_ext', 'options' => ['pdf', 'png', 'txt']]);
- $request = $request->withParam('_ext', 'pdf');
- $request->clearDetectorCache();
- $this->assertTrue($request->is('extension'));
- $request = $request->withParam('_ext', 'exe');
- $request->clearDetectorCache();
- $this->assertFalse($request->isExtension());
- }
- /**
- * Test getting headers
- */
- public function testHeader(): void
- {
- $request = new ServerRequest(['environment' => [
- 'HTTP_HOST' => 'localhost',
- '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',
- 'CONTENT_TYPE' => 'application/json',
- 'CONTENT_LENGTH' => '1337',
- 'HTTP_CONTENT_MD5' => 'abc123',
- ]]);
- $this->assertEquals($request->getEnv('HTTP_HOST'), $request->getHeaderLine('host'));
- $this->assertEquals($request->getEnv('HTTP_USER_AGENT'), $request->getHeaderLine('User-Agent'));
- $this->assertEquals($request->getEnv('CONTENT_LENGTH'), $request->getHeaderLine('content-length'));
- $this->assertEquals($request->getEnv('CONTENT_TYPE'), $request->getHeaderLine('content-type'));
- $this->assertEquals($request->getEnv('HTTP_CONTENT_MD5'), $request->getHeaderLine('content-md5'));
- }
- /**
- * Test getting headers with psr7 methods
- */
- public function testGetHeaders(): void
- {
- $request = new ServerRequest(['environment' => [
- 'HTTP_HOST' => 'localhost',
- 'CONTENT_TYPE' => 'application/json',
- 'CONTENT_LENGTH' => 1337,
- 'HTTP_CONTENT_MD5' => 'abc123',
- 'HTTP_DOUBLE' => ['a', 'b'],
- ]]);
- $headers = $request->getHeaders();
- $expected = [
- 'Host' => ['localhost'],
- 'Content-Type' => ['application/json'],
- 'Content-Length' => [1337],
- 'Content-Md5' => ['abc123'],
- 'Double' => ['a', 'b'],
- ];
- $this->assertEquals($expected, $headers);
- }
- /**
- * Test hasHeader
- */
- public function testHasHeader(): void
- {
- $request = new ServerRequest(['environment' => [
- 'HTTP_HOST' => 'localhost',
- 'CONTENT_TYPE' => 'application/json',
- 'CONTENT_LENGTH' => 1337,
- 'HTTP_CONTENT_MD5' => 'abc123',
- 'HTTP_DOUBLE' => ['a', 'b'],
- ]]);
- $this->assertTrue($request->hasHeader('Host'));
- $this->assertTrue($request->hasHeader('Content-Type'));
- $this->assertTrue($request->hasHeader('Content-MD5'));
- $this->assertTrue($request->hasHeader('Double'));
- $this->assertFalse($request->hasHeader('Authorization'));
- }
- /**
- * Test getting headers with psr7 methods
- */
- public function testGetHeader(): void
- {
- $request = new ServerRequest(['environment' => [
- 'HTTP_HOST' => 'localhost',
- 'CONTENT_TYPE' => 'application/json',
- 'CONTENT_LENGTH' => 1337,
- 'HTTP_CONTENT_MD5' => 'abc123',
- 'HTTP_DOUBLE' => ['a', 'b'],
- ]]);
- $this->assertEquals([], $request->getHeader('Not-there'));
- $expected = [$request->getEnv('HTTP_HOST')];
- $this->assertEquals($expected, $request->getHeader('Host'));
- $this->assertEquals($expected, $request->getHeader('host'));
- $this->assertEquals($expected, $request->getHeader('HOST'));
- $this->assertEquals(['a', 'b'], $request->getHeader('Double'));
- }
- /**
- * Test getting headers with psr7 methods
- */
- public function testGetHeaderLine(): void
- {
- $request = new ServerRequest(['environment' => [
- 'HTTP_HOST' => 'localhost',
- 'CONTENT_TYPE' => 'application/json',
- 'CONTENT_LENGTH' => '1337',
- 'HTTP_CONTENT_MD5' => 'abc123',
- 'HTTP_DOUBLE' => ['a', 'b'],
- ]]);
- $this->assertSame('', $request->getHeaderLine('Authorization'));
- $expected = $request->getEnv('CONTENT_LENGTH');
- $this->assertEquals($expected, $request->getHeaderLine('Content-Length'));
- $this->assertEquals($expected, $request->getHeaderLine('content-Length'));
- $this->assertEquals($expected, $request->getHeaderLine('ConTent-LenGth'));
- $this->assertSame('a, b', $request->getHeaderLine('Double'));
- }
- /**
- * Test setting a header.
- */
- public function testWithHeader(): void
- {
- $request = new ServerRequest(['environment' => [
- 'HTTP_HOST' => 'localhost',
- 'CONTENT_TYPE' => 'application/json',
- 'CONTENT_LENGTH' => '1337',
- 'HTTP_CONTENT_MD5' => 'abc123',
- 'HTTP_DOUBLE' => ['a', 'b'],
- ]]);
- $new = $request->withHeader('Content-Length', '999');
- $this->assertNotSame($new, $request);
- $this->assertSame('1337', $request->getHeaderLine('Content-length'), 'old request is unchanged');
- $this->assertSame('999', $new->getHeaderLine('Content-length'), 'new request is correct');
- $new = $request->withHeader('Double', ['a']);
- $this->assertEquals(['a'], $new->getHeader('Double'), 'List values are overwritten');
- }
- /**
- * Test adding a header.
- */
- public function testWithAddedHeader(): void
- {
- $request = new ServerRequest(['environment' => [
- 'HTTP_HOST' => 'localhost',
- 'CONTENT_TYPE' => 'application/json',
- 'CONTENT_LENGTH' => 1337,
- 'HTTP_CONTENT_MD5' => 'abc123',
- 'HTTP_DOUBLE' => ['a', 'b'],
- ]]);
- $new = $request->withAddedHeader('Double', 'c');
- $this->assertNotSame($new, $request);
- $this->assertSame('a, b', $request->getHeaderLine('Double'), 'old request is unchanged');
- $this->assertSame('a, b, c', $new->getHeaderLine('Double'), 'new request is correct');
- $new = $request->withAddedHeader('Content-Length', 777);
- $this->assertEquals([1337, 777], $new->getHeader('Content-Length'), 'scalar values are appended');
- $new = $request->withAddedHeader('Content-Length', [123, 456]);
- $this->assertEquals([1337, 123, 456], $new->getHeader('Content-Length'), 'List values are merged');
- }
- /**
- * Test removing a header.
- */
- public function testWithoutHeader(): void
- {
- $request = new ServerRequest(['environment' => [
- 'HTTP_HOST' => 'localhost',
- 'CONTENT_TYPE' => 'application/json',
- 'CONTENT_LENGTH' => 1337,
- 'HTTP_CONTENT_MD5' => 'abc123',
- 'HTTP_DOUBLE' => ['a', 'b'],
- ]]);
- $new = $request->withoutHeader('Content-Length');
- $this->assertNotSame($new, $request);
- $this->assertSame('1337', $request->getHeaderLine('Content-length'), 'old request is unchanged');
- $this->assertSame('', $new->getHeaderLine('Content-length'), 'new request is correct');
- }
- /**
- * Test accepts() with and without parameters
- */
- public function testAccepts(): void
- {
- $request = new ServerRequest(['environment' => [
- 'HTTP_ACCEPT' => 'text/xml,application/xml;q=0.9,application/xhtml+xml,text/html,text/plain,image/png',
- ]]);
- $result = $request->accepts();
- $expected = [
- 'text/xml', 'application/xhtml+xml', 'text/html', 'text/plain', 'image/png', 'application/xml',
- ];
- $this->assertEquals($expected, $result, 'Content types differ.');
- $result = $request->accepts('text/html');
- $this->assertTrue($result);
- $result = $request->accepts('image/gif');
- $this->assertFalse($result);
- }
- /**
- * Test that accept header types are trimmed for comparisons.
- */
- public function testAcceptWithWhitespace(): void
- {
- $request = new ServerRequest(['environment' => [
- 'HTTP_ACCEPT' => 'text/xml , text/html , text/plain,image/png',
- ]]);
- $result = $request->accepts();
- $expected = [
- 'text/xml', 'text/html', 'text/plain', 'image/png',
- ];
- $this->assertEquals($expected, $result, 'Content types differ.');
- $this->assertTrue($request->accepts('text/html'));
- }
- /**
- * Content types from accepts() should respect the client's q preference values.
- */
- public function testAcceptWithQvalueSorting(): void
- {
- $request = new ServerRequest(['environment' => [
- 'HTTP_ACCEPT' => 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0',
- ]]);
- $result = $request->accepts();
- $expected = ['application/xml', 'text/html', 'application/json'];
- $this->assertEquals($expected, $result);
- }
- /**
- * Test the getQuery() method
- */
- public function testGetQuery(): void
- {
- $array = [
- 'query' => [
- 'foo' => 'bar',
- 'zero' => '0',
- 'test' => [
- 'foo', 'bar',
- ],
- ],
- ];
- $request = new ServerRequest($array);
- $this->assertEquals([
- 'foo' => 'bar',
- 'zero' => '0',
- 'test' => [
- 'foo', 'bar',
- ],
- ], $request->getQuery());
- $this->assertSame('bar', $request->getQuery('foo'));
- $this->assertSame('0', $request->getQuery('zero'));
- $this->assertNull($request->getQuery('imaginary'));
- $this->assertSame('default', $request->getQuery('imaginary', 'default'));
- $this->assertFalse($request->getQuery('imaginary', false));
- $this->assertSame(['foo', 'bar'], $request->getQuery('test'));
- $this->assertSame('bar', $request->getQuery('test.1'));
- $this->assertNull($request->getQuery('test.2'));
- $this->assertSame('default', $request->getQuery('test.2', 'default'));
- }
- /**
- * Test getQueryParams
- */
- public function testGetQueryParams(): void
- {
- $get = [
- 'test' => ['foo', 'bar'],
- 'key' => 'value',
- ];
- $request = new ServerRequest([
- 'query' => $get,
- ]);
- $this->assertSame($get, $request->getQueryParams());
- }
- /**
- * Test withQueryParams and immutability
- */
- public function testWithQueryParams(): void
- {
- $get = [
- 'test' => ['foo', 'bar'],
- 'key' => 'value',
- ];
- $request = new ServerRequest([
- 'query' => $get,
- ]);
- $new = $request->withQueryParams(['new' => 'data']);
- $this->assertSame($get, $request->getQueryParams());
- $this->assertSame(['new' => 'data'], $new->getQueryParams());
- }
- /**
- * Test using param()
- */
- public function testReadingParams(): void
- {
- $request = new ServerRequest([
- 'params' => [
- 'controller' => 'Posts',
- 'admin' => true,
- 'truthy' => 1,
- 'zero' => '0',
- ],
- ]);
- $this->assertNull($request->getParam('not_set'));
- $this->assertTrue($request->getParam('admin'));
- $this->assertSame(1, $request->getParam('truthy'));
- $this->assertSame('Posts', $request->getParam('controller'));
- $this->assertSame('0', $request->getParam('zero'));
- }
- /**
- * Test the data() method reading
- */
- public function testGetData(): void
- {
- $post = [
- 'Model' => [
- 'field' => 'value',
- ],
- ];
- $request = new ServerRequest(compact('post'));
- $this->assertEquals($post['Model'], $request->getData('Model'));
- $this->assertEquals($post, $request->getData());
- $this->assertNull($request->getData('Model.imaginary'));
- $this->assertSame('value', $request->getData('Model.field', 'default'));
- $this->assertSame('default', $request->getData('Model.imaginary', 'default'));
- }
- /**
- * Test setting post data to a string throws exception.
- */
- public function testInvalidStringData(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('`post` key must be an array, object or null. Got `string` instead.');
- $post = 'strange, but could happen';
- new ServerRequest(compact('post'));
- }
- /**
- * Test writing falsey values.
- */
- public function testDataWritingFalsey(): void
- {
- $request = new ServerRequest();
- $request = $request->withData('Post.null', null);
- $this->assertNull($request->getData('Post.null'));
- $request = $request->withData('Post.false', false);
- $this->assertFalse($request->getData('Post.false'));
- $request = $request->withData('Post.zero', 0);
- $this->assertSame(0, $request->getData('Post.zero'));
- $request = $request->withData('Post.empty', '');
- $this->assertSame('', $request->getData('Post.empty'));
- }
- /**
- * Test reading params
- *
- * @dataProvider paramReadingDataProvider
- * @param mixed $expected
- */
- public function testGetParam(string $toRead, $expected): void
- {
- $request = new ServerRequest([
- 'url' => '/',
- 'params' => [
- 'action' => 'index',
- 'foo' => 'bar',
- 'baz' => [
- 'a' => [
- 'b' => 'c',
- ],
- ],
- 'admin' => true,
- 'truthy' => 1,
- 'zero' => '0',
- ],
- ]);
- $this->assertSame($expected, $request->getParam($toRead));
- }
- /**
- * Test getParam returning a default value.
- */
- public function testGetParamDefault(): void
- {
- $request = new ServerRequest([
- 'params' => [
- 'controller' => 'Articles',
- 'null' => null,
- ],
- ]);
- $this->assertSame('Articles', $request->getParam('controller', 'default'));
- $this->assertSame('default', $request->getParam('null', 'default'));
- $this->assertFalse($request->getParam('unset', false));
- $this->assertNull($request->getParam('unset'));
- }
- /**
- * Data provider for testing reading values with ServerRequest::getParam()
- *
- * @return array
- */
- public static function paramReadingDataProvider(): array
- {
- return [
- [
- 'action',
- 'index',
- ],
- [
- 'baz',
- [
- 'a' => [
- 'b' => 'c',
- ],
- ],
- ],
- [
- 'baz.a.b',
- 'c',
- ],
- [
- 'does_not_exist',
- null,
- ],
- [
- 'admin',
- true,
- ],
- [
- 'truthy',
- 1,
- ],
- [
- 'zero',
- '0',
- ],
- ];
- }
- /**
- * test writing request params with param()
- */
- public function testParamWriting(): void
- {
- $request = new ServerRequest(['url' => '/']);
- $request = $request->withParam('action', 'index');
- $this->assertInstanceOf(
- 'Cake\Http\ServerRequest',
- $request->withParam('some', 'thing'),
- 'Method has not returned $this'
- );
- $request = $request->withParam('Post.null', null);
- $this->assertNull($request->getParam('Post.null'), 'default value should be used.');
- $request = $request->withParam('Post.false', false);
- $this->assertFalse($request->getParam('Post.false'));
- $request = $request->withParam('Post.zero', 0);
- $this->assertSame(0, $request->getParam('Post.zero'));
- $request = $request->withParam('Post.empty', '');
- $this->assertSame('', $request->getParam('Post.empty'));
- $this->assertSame('index', $request->getParam('action'));
- $request = $request->withParam('action', 'edit');
- $this->assertSame('edit', $request->getParam('action'));
- }
- /**
- * Test accept language
- */
- public function testAcceptLanguage(): void
- {
- $request = new ServerRequest();
- // Weird language
- $request = $request->withEnv('HTTP_ACCEPT_LANGUAGE', 'inexistent,en-ca');
- $result = $request->acceptLanguage();
- $this->assertEquals(['inexistent', 'en-ca'], $result, 'Languages do not match');
- // No qualifier
- $request = $request->withEnv('HTTP_ACCEPT_LANGUAGE', 'es_mx,en_ca');
- $result = $request->acceptLanguage();
- $this->assertEquals(['es-mx', 'en-ca'], $result, 'Languages do not match');
- // With qualifier
- $request = $request->withEnv('HTTP_ACCEPT_LANGUAGE', 'en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4');
- $result = $request->acceptLanguage();
- $this->assertEquals(['en-us', 'en', 'pt-br', 'pt'], $result, 'Languages do not match');
- // With spaces
- $request = $request->withEnv('HTTP_ACCEPT_LANGUAGE', 'da, en-gb;q=0.8, en;q=0.7');
- $result = $request->acceptLanguage();
- $this->assertEquals(['da', 'en-gb', 'en'], $result, 'Languages do not match');
- // Checking if requested
- $request = $request->withEnv('HTTP_ACCEPT_LANGUAGE', 'es_mx,en_ca');
- $result = $request->acceptLanguage('en-ca');
- $this->assertTrue($result);
- $result = $request->acceptLanguage('en-CA');
- $this->assertTrue($result);
- $result = $request->acceptLanguage('en-us');
- $this->assertFalse($result);
- $result = $request->acceptLanguage('en-US');
- $this->assertFalse($result);
- }
- /**
- * Test getBody
- */
- public function testGetBody(): void
- {
- $request = new ServerRequest([
- 'input' => 'key=val&some=data',
- ]);
- $result = $request->getBody();
- $this->assertInstanceOf('Psr\Http\Message\StreamInterface', $result);
- $this->assertSame('key=val&some=data', $result->getContents());
- }
- /**
- * Test withBody
- */
- public function testWithBody(): void
- {
- $request = new ServerRequest([
- 'input' => 'key=val&some=data',
- ]);
- $body = $this->getMockBuilder('Psr\Http\Message\StreamInterface')->getMock();
- $new = $request->withBody($body);
- $this->assertNotSame($new, $request);
- $this->assertNotSame($body, $request->getBody());
- $this->assertSame($body, $new->getBody());
- }
- /**
- * Test getUri
- */
- public function testGetUri(): void
- {
- $request = new ServerRequest(['url' => 'articles/view/3']);
- $result = $request->getUri();
- $this->assertInstanceOf('Psr\Http\Message\UriInterface', $result);
- $this->assertSame('/articles/view/3', $result->getPath());
- }
- /**
- * Test withUri
- */
- public function testWithUri(): void
- {
- $request = new ServerRequest([
- 'environment' => [
- 'HTTP_HOST' => 'example.com',
- ],
- 'url' => 'articles/view/3',
- ]);
- $uri = $this->getMockBuilder('Psr\Http\Message\UriInterface')->getMock();
- $new = $request->withUri($uri);
- $this->assertNotSame($new, $request);
- $this->assertNotSame($uri, $request->getUri());
- $this->assertSame($uri, $new->getUri());
- }
- /**
- * Test withUri() and preserveHost
- */
- public function testWithUriPreserveHost(): void
- {
- $request = new ServerRequest([
- 'environment' => [
- 'HTTP_HOST' => 'localhost',
- ],
- 'url' => 'articles/view/3',
- ]);
- $uri = new Uri();
- $uri = $uri->withHost('example.com')
- ->withPort(123)
- ->withPath('articles/view/3');
- $new = $request->withUri($uri, false);
- $this->assertNotSame($new, $request);
- $this->assertSame('example.com:123', $new->getHeaderLine('Host'));
- }
- /**
- * Test withUri() and preserveHost missing the host header
- */
- public function testWithUriPreserveHostNoHostHeader(): void
- {
- $request = new ServerRequest([
- 'url' => 'articles/view/3',
- ]);
- $uri = new Uri();
- $uri = $uri->withHost('example.com')
- ->withPort(123)
- ->withPath('articles/view/3');
- $new = $request->withUri($uri, false);
- $this->assertSame('example.com:123', $new->getHeaderLine('Host'));
- }
- /**
- * Test the cookie() method.
- */
- public function testGetCookie(): void
- {
- $request = new ServerRequest([
- 'cookies' => [
- 'testing' => 'A value in the cookie',
- 'user' => [
- 'remember' => '1',
- ],
- '123' => 'a integer key cookie',
- ],
- ]);
- $this->assertSame('A value in the cookie', $request->getCookie('testing'));
- $this->assertSame('a integer key cookie', $request->getCookie('123'));
- $this->assertNull($request->getCookie('not there'));
- $this->assertSame('default', $request->getCookie('not there', 'default'));
- $this->assertSame('1', $request->getCookie('user.remember'));
- $this->assertSame('1', $request->getCookie('user.remember', 'default'));
- $this->assertSame('default', $request->getCookie('user.not there', 'default'));
- }
- /**
- * Test getCookieParams()
- */
- public function testGetCookieParams(): void
- {
- $cookies = [
- 'testing' => 'A value in the cookie',
- ];
- $request = new ServerRequest(['cookies' => $cookies]);
- $this->assertSame($cookies, $request->getCookieParams());
- }
- /**
- * Test withCookieParams()
- */
- public function testWithCookieParams(): void
- {
- $cookies = [
- 'testing' => 'A value in the cookie',
- ];
- $request = new ServerRequest(['cookies' => $cookies]);
- $new = $request->withCookieParams(['remember_me' => 1]);
- $this->assertNotSame($new, $request);
- $this->assertSame($cookies, $request->getCookieParams());
- $this->assertSame(['remember_me' => 1], $new->getCookieParams());
- }
- /**
- * Test getting a cookie collection from a request.
- */
- public function testGetCookieCollection(): void
- {
- $cookies = [
- 'remember_me' => 1,
- 'color' => 'blue',
- ];
- $request = new ServerRequest(['cookies' => $cookies]);
- $cookies = $request->getCookieCollection();
- $this->assertInstanceOf(CookieCollection::class, $cookies);
- $this->assertCount(2, $cookies);
- $this->assertSame('1', $cookies->get('remember_me')->getValue());
- $this->assertSame('blue', $cookies->get('color')->getValue());
- }
- /**
- * Test replacing cookies from a collection
- */
- public function testWithCookieCollection(): void
- {
- $cookies = new CookieCollection([new Cookie('remember_me', 1), new Cookie('color', 'red')]);
- $request = new ServerRequest(['cookies' => ['bad' => 'goaway']]);
- $new = $request->withCookieCollection($cookies);
- $this->assertNotSame($new, $request, 'Should clone');
- $this->assertSame(['bad' => 'goaway'], $request->getCookieParams());
- $this->assertSame(['remember_me' => '1', 'color' => 'red'], $new->getCookieParams());
- $cookies = $new->getCookieCollection();
- $this->assertCount(2, $cookies);
- $this->assertSame('red', $cookies->get('color')->getValue());
- }
- /**
- * TestAllowMethod
- */
- public function testAllowMethod(): void
- {
- $request = new ServerRequest(['environment' => [
- 'url' => '/posts/edit/1',
- 'REQUEST_METHOD' => 'PUT',
- ]]);
- $this->assertTrue($request->allowMethod('put'));
- $request = $request->withEnv('REQUEST_METHOD', 'DELETE');
- $this->assertTrue($request->allowMethod(['post', 'delete']));
- }
- /**
- * Test allowMethod throwing exception
- */
- public function testAllowMethodException(): void
- {
- $request = new ServerRequest([
- 'url' => '/posts/edit/1',
- 'environment' => ['REQUEST_METHOD' => 'PUT'],
- ]);
- try {
- $request->allowMethod(['POST', 'DELETE']);
- $this->fail('An expected exception has not been raised.');
- } catch (MethodNotAllowedException $e) {
- $this->assertEquals(['Allow' => 'POST, DELETE'], $e->getHeaders());
- }
- $this->expectException(MethodNotAllowedException::class);
- $request->allowMethod('POST');
- }
- /**
- * Tests getting the session from the request
- */
- public function testGetSession(): void
- {
- $session = new Session();
- $request = new ServerRequest(['session' => $session]);
- $this->assertSame($session, $request->getSession());
- $request = new ServerRequest();
- $this->assertEquals($session, $request->getSession());
- }
- public function testGetFlash(): void
- {
- $request = new ServerRequest();
- $this->assertInstanceOf(FlashMessage::class, $request->getFlash());
- }
- /**
- * Test the content type method.
- */
- public function testContentType(): void
- {
- $request = new ServerRequest([
- 'environment' => ['HTTP_CONTENT_TYPE' => 'application/json'],
- ]);
- $this->assertSame('application/json', $request->contentType());
- $request = new ServerRequest([
- 'environment' => ['HTTP_CONTENT_TYPE' => 'application/xml'],
- ]);
- $this->assertSame('application/xml', $request->contentType(), 'prefer non http header.');
- }
- /**
- * Test updating params in a psr7 fashion.
- */
- public function testWithParam(): void
- {
- $request = new ServerRequest([
- 'params' => ['controller' => 'Articles'],
- ]);
- $result = $request->withParam('action', 'view');
- $this->assertNotSame($result, $request, 'New instance should be made');
- $this->assertNull($request->getParam('action'), 'No side-effect on original');
- $this->assertSame('view', $result->getParam('action'));
- $result = $request->withParam('action', 'index')
- ->withParam('plugin', 'DebugKit')
- ->withParam('prefix', 'Admin');
- $this->assertNotSame($result, $request, 'New instance should be made');
- $this->assertNull($request->getParam('action'), 'No side-effect on original');
- $this->assertSame('index', $result->getParam('action'));
- $this->assertSame('DebugKit', $result->getParam('plugin'));
- $this->assertSame('Admin', $result->getParam('prefix'));
- }
- /**
- * Test getting the parsed body parameters.
- */
- public function testGetParsedBody(): void
- {
- $data = ['title' => 'First', 'body' => 'Best Article!'];
- $request = new ServerRequest(['post' => $data]);
- $this->assertSame($data, $request->getParsedBody());
- $request = new ServerRequest();
- $this->assertSame([], $request->getParsedBody());
- }
- /**
- * Test replacing the parsed body parameters.
- */
- public function testWithParsedBody(): void
- {
- $data = ['title' => 'First', 'body' => 'Best Article!'];
- $request = new ServerRequest([]);
- $new = $request->withParsedBody($data);
- $this->assertNotSame($request, $new);
- $this->assertSame([], $request->getParsedBody());
- $this->assertSame($data, $new->getParsedBody());
- }
- /**
- * Test updating POST data in a psr7 fashion.
- */
- public function testWithData(): void
- {
- $request = new ServerRequest([
- 'post' => [
- 'Model' => [
- 'field' => 'value',
- ],
- ],
- ]);
- $result = $request->withData('Model.new_value', 'new value');
- $this->assertNull($request->getData('Model.new_value'), 'Original request should not change.');
- $this->assertNotSame($result, $request);
- $this->assertSame('new value', $result->getData('Model.new_value'));
- $this->assertSame('new value', $result->getData()['Model']['new_value']);
- $this->assertSame('value', $result->getData('Model.field'));
- }
- /**
- * Test removing data from a request
- */
- public function testWithoutData(): void
- {
- $request = new ServerRequest([
- 'post' => [
- 'Model' => [
- 'id' => 1,
- 'field' => 'value',
- ],
- ],
- ]);
- $updated = $request->withoutData('Model.field');
- $this->assertNotSame($updated, $request);
- $this->assertSame('value', $request->getData('Model.field'), 'Original request should not change.');
- $this->assertNull($updated->getData('Model.field'), 'data removed from updated request');
- $this->assertFalse(isset($updated->getData()['Model']['field']), 'data removed from updated request');
- }
- /**
- * Test updating POST data when keys don't exist
- */
- public function testWithDataMissingIntermediaryKeys(): void
- {
- $request = new ServerRequest([
- 'post' => [
- 'Model' => [
- 'field' => 'value',
- ],
- ],
- ]);
- $result = $request->withData('Model.field.new_value', 'new value');
- $this->assertSame(
- 'new value',
- $result->getData('Model.field.new_value')
- );
- $this->assertSame(
- 'new value',
- $result->getData()['Model']['field']['new_value']
- );
- }
- /**
- * Test updating POST data with falsey values
- */
- public function testWithDataFalseyValues(): void
- {
- $request = new ServerRequest([
- 'post' => [],
- ]);
- $result = $request->withData('false', false)
- ->withData('null', null)
- ->withData('empty_string', '')
- ->withData('zero', 0)
- ->withData('zero_string', '0');
- $expected = [
- 'false' => false,
- 'null' => null,
- 'empty_string' => '',
- 'zero' => 0,
- 'zero_string' => '0',
- ];
- $this->assertSame($expected, $result->getData());
- }
- /**
- * Test setting attributes.
- */
- public function testWithAttribute(): void
- {
- $request = new ServerRequest([]);
- $this->assertNull($request->getAttribute('key'));
- $this->assertSame('default', $request->getAttribute('key', 'default'));
- $new = $request->withAttribute('key', 'value');
- $this->assertNotEquals($new, $request, 'Should be different');
- $this->assertNull($request->getAttribute('key'), 'Old instance not modified');
- $this->assertSame('value', $new->getAttribute('key'));
- $update = $new->withAttribute('key', ['complex']);
- $this->assertNotEquals($update, $new, 'Should be different');
- $this->assertSame(['complex'], $update->getAttribute('key'));
- }
- /**
- * Test that replacing the session can be done via withAttribute()
- */
- public function testWithAttributeSession(): void
- {
- $request = new ServerRequest([]);
- $request->getSession()->write('attrKey', 'session-value');
- $update = $request->withAttribute('session', Session::create());
- $this->assertSame('session-value', $request->getAttribute('session')->read('attrKey'));
- $this->assertNotSame($request->getAttribute('session'), $update->getAttribute('session'));
- $this->assertNotSame($request->getSession()->read('attrKey'), $update->getSession()->read('attrKey'));
- }
- /**
- * Test getting all attributes.
- */
- public function testGetAttributes(): void
- {
- $request = new ServerRequest([]);
- $new = $request->withAttribute('key', 'value')
- ->withAttribute('nully', null)
- ->withAttribute('falsey', false);
- $this->assertFalse($new->getAttribute('falsey'));
- $this->assertNull($new->getAttribute('nully'));
- $expected = [
- 'key' => 'value',
- 'nully' => null,
- 'falsey' => false,
- 'params' => [
- 'plugin' => null,
- 'controller' => null,
- 'action' => null,
- '_ext' => null,
- 'pass' => [],
- ],
- 'webroot' => '',
- 'base' => '',
- 'here' => '/',
- ];
- $this->assertEquals($expected, $new->getAttributes());
- }
- /**
- * Test unsetting attributes.
- */
- public function testWithoutAttribute(): void
- {
- $request = new ServerRequest([]);
- $new = $request->withAttribute('key', 'value');
- $update = $request->withoutAttribute('key');
- $this->assertNotEquals($update, $new, 'Should be different');
- $this->assertNull($update->getAttribute('key'));
- }
- /**
- * Test that withoutAttribute() cannot remove emulatedAttributes properties.
- *
- * @dataProvider emulatedPropertyProvider
- */
- public function testWithoutAttributesDenyEmulatedProperties(string $prop): void
- {
- $this->expectException(InvalidArgumentException::class);
- $request = new ServerRequest([]);
- $request->withoutAttribute($prop);
- }
- /**
- * Test the requestTarget methods.
- */
- public function testWithRequestTarget(): void
- {
- $request = new ServerRequest([
- 'environment' => [
- 'REQUEST_URI' => '/articles/view/1',
- 'QUERY_STRING' => 'comments=1&open=0',
- ],
- 'base' => '/basedir',
- ]);
- $this->assertSame(
- '/articles/view/1?comments=1&open=0',
- $request->getRequestTarget(),
- 'Should not include basedir.'
- );
- $new = $request->withRequestTarget('/articles/view/3');
- $this->assertNotSame($new, $request);
- $this->assertSame(
- '/articles/view/1?comments=1&open=0',
- $request->getRequestTarget(),
- 'should be unchanged.'
- );
- $this->assertSame('/articles/view/3', $new->getRequestTarget(), 'reflects method call');
- }
- /**
- * Test withEnv()
- */
- public function testWithEnv(): void
- {
- $request = new ServerRequest();
- $newRequest = $request->withEnv('HTTP_HOST', 'cakephp.org');
- $this->assertNotSame($request, $newRequest);
- $this->assertSame('cakephp.org', $newRequest->getEnv('HTTP_HOST'));
- }
- /**
- * Test getEnv()
- */
- public function testGetEnv(): void
- {
- $request = new ServerRequest([
- 'environment' => ['TEST' => 'ing'],
- ]);
- //Test default null
- $this->assertNull($request->getEnv('Foo'));
- //Test default set
- $this->assertSame('Bar', $request->getEnv('Foo', 'Bar'));
- //Test env() fallback
- $this->assertSame('ing', $request->getEnv('test'));
- }
- /**
- * Data provider for emulated property tests.
- *
- * @return array
- */
- public static function emulatedPropertyProvider(): array
- {
- return [
- ['here'],
- ['params'],
- ['base'],
- ['webroot'],
- ['session'],
- ];
- }
- }
|