RequestTest.php 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
  13. use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
  14. use Symfony\Component\HttpFoundation\Session\Session;
  15. use Symfony\Component\HttpFoundation\Request;
  16. class RequestTest extends TestCase
  17. {
  18. protected function tearDown()
  19. {
  20. // reset
  21. Request::setTrustedProxies(array(), -1);
  22. }
  23. public function testInitialize()
  24. {
  25. $request = new Request();
  26. $request->initialize(array('foo' => 'bar'));
  27. $this->assertEquals('bar', $request->query->get('foo'), '->initialize() takes an array of query parameters as its first argument');
  28. $request->initialize(array(), array('foo' => 'bar'));
  29. $this->assertEquals('bar', $request->request->get('foo'), '->initialize() takes an array of request parameters as its second argument');
  30. $request->initialize(array(), array(), array('foo' => 'bar'));
  31. $this->assertEquals('bar', $request->attributes->get('foo'), '->initialize() takes an array of attributes as its third argument');
  32. $request->initialize(array(), array(), array(), array(), array(), array('HTTP_FOO' => 'bar'));
  33. $this->assertEquals('bar', $request->headers->get('FOO'), '->initialize() takes an array of HTTP headers as its sixth argument');
  34. }
  35. public function testGetLocale()
  36. {
  37. $request = new Request();
  38. $request->setLocale('pl');
  39. $locale = $request->getLocale();
  40. $this->assertEquals('pl', $locale);
  41. }
  42. public function testGetUser()
  43. {
  44. $request = Request::create('http://user:password@test.com');
  45. $user = $request->getUser();
  46. $this->assertEquals('user', $user);
  47. }
  48. public function testGetPassword()
  49. {
  50. $request = Request::create('http://user:password@test.com');
  51. $password = $request->getPassword();
  52. $this->assertEquals('password', $password);
  53. }
  54. public function testIsNoCache()
  55. {
  56. $request = new Request();
  57. $isNoCache = $request->isNoCache();
  58. $this->assertFalse($isNoCache);
  59. }
  60. public function testGetContentType()
  61. {
  62. $request = new Request();
  63. $contentType = $request->getContentType();
  64. $this->assertNull($contentType);
  65. }
  66. public function testSetDefaultLocale()
  67. {
  68. $request = new Request();
  69. $request->setDefaultLocale('pl');
  70. $locale = $request->getLocale();
  71. $this->assertEquals('pl', $locale);
  72. }
  73. public function testCreate()
  74. {
  75. $request = Request::create('http://test.com/foo?bar=baz');
  76. $this->assertEquals('http://test.com/foo?bar=baz', $request->getUri());
  77. $this->assertEquals('/foo', $request->getPathInfo());
  78. $this->assertEquals('bar=baz', $request->getQueryString());
  79. $this->assertEquals(80, $request->getPort());
  80. $this->assertEquals('test.com', $request->getHttpHost());
  81. $this->assertFalse($request->isSecure());
  82. $request = Request::create('http://test.com/foo', 'GET', array('bar' => 'baz'));
  83. $this->assertEquals('http://test.com/foo?bar=baz', $request->getUri());
  84. $this->assertEquals('/foo', $request->getPathInfo());
  85. $this->assertEquals('bar=baz', $request->getQueryString());
  86. $this->assertEquals(80, $request->getPort());
  87. $this->assertEquals('test.com', $request->getHttpHost());
  88. $this->assertFalse($request->isSecure());
  89. $request = Request::create('http://test.com/foo?bar=foo', 'GET', array('bar' => 'baz'));
  90. $this->assertEquals('http://test.com/foo?bar=baz', $request->getUri());
  91. $this->assertEquals('/foo', $request->getPathInfo());
  92. $this->assertEquals('bar=baz', $request->getQueryString());
  93. $this->assertEquals(80, $request->getPort());
  94. $this->assertEquals('test.com', $request->getHttpHost());
  95. $this->assertFalse($request->isSecure());
  96. $request = Request::create('https://test.com/foo?bar=baz');
  97. $this->assertEquals('https://test.com/foo?bar=baz', $request->getUri());
  98. $this->assertEquals('/foo', $request->getPathInfo());
  99. $this->assertEquals('bar=baz', $request->getQueryString());
  100. $this->assertEquals(443, $request->getPort());
  101. $this->assertEquals('test.com', $request->getHttpHost());
  102. $this->assertTrue($request->isSecure());
  103. $request = Request::create('test.com:90/foo');
  104. $this->assertEquals('http://test.com:90/foo', $request->getUri());
  105. $this->assertEquals('/foo', $request->getPathInfo());
  106. $this->assertEquals('test.com', $request->getHost());
  107. $this->assertEquals('test.com:90', $request->getHttpHost());
  108. $this->assertEquals(90, $request->getPort());
  109. $this->assertFalse($request->isSecure());
  110. $request = Request::create('https://test.com:90/foo');
  111. $this->assertEquals('https://test.com:90/foo', $request->getUri());
  112. $this->assertEquals('/foo', $request->getPathInfo());
  113. $this->assertEquals('test.com', $request->getHost());
  114. $this->assertEquals('test.com:90', $request->getHttpHost());
  115. $this->assertEquals(90, $request->getPort());
  116. $this->assertTrue($request->isSecure());
  117. $request = Request::create('https://127.0.0.1:90/foo');
  118. $this->assertEquals('https://127.0.0.1:90/foo', $request->getUri());
  119. $this->assertEquals('/foo', $request->getPathInfo());
  120. $this->assertEquals('127.0.0.1', $request->getHost());
  121. $this->assertEquals('127.0.0.1:90', $request->getHttpHost());
  122. $this->assertEquals(90, $request->getPort());
  123. $this->assertTrue($request->isSecure());
  124. $request = Request::create('https://[::1]:90/foo');
  125. $this->assertEquals('https://[::1]:90/foo', $request->getUri());
  126. $this->assertEquals('/foo', $request->getPathInfo());
  127. $this->assertEquals('[::1]', $request->getHost());
  128. $this->assertEquals('[::1]:90', $request->getHttpHost());
  129. $this->assertEquals(90, $request->getPort());
  130. $this->assertTrue($request->isSecure());
  131. $request = Request::create('https://[::1]/foo');
  132. $this->assertEquals('https://[::1]/foo', $request->getUri());
  133. $this->assertEquals('/foo', $request->getPathInfo());
  134. $this->assertEquals('[::1]', $request->getHost());
  135. $this->assertEquals('[::1]', $request->getHttpHost());
  136. $this->assertEquals(443, $request->getPort());
  137. $this->assertTrue($request->isSecure());
  138. $json = '{"jsonrpc":"2.0","method":"echo","id":7,"params":["Hello World"]}';
  139. $request = Request::create('http://example.com/jsonrpc', 'POST', array(), array(), array(), array(), $json);
  140. $this->assertEquals($json, $request->getContent());
  141. $this->assertFalse($request->isSecure());
  142. $request = Request::create('http://test.com');
  143. $this->assertEquals('http://test.com/', $request->getUri());
  144. $this->assertEquals('/', $request->getPathInfo());
  145. $this->assertEquals('', $request->getQueryString());
  146. $this->assertEquals(80, $request->getPort());
  147. $this->assertEquals('test.com', $request->getHttpHost());
  148. $this->assertFalse($request->isSecure());
  149. $request = Request::create('http://test.com?test=1');
  150. $this->assertEquals('http://test.com/?test=1', $request->getUri());
  151. $this->assertEquals('/', $request->getPathInfo());
  152. $this->assertEquals('test=1', $request->getQueryString());
  153. $this->assertEquals(80, $request->getPort());
  154. $this->assertEquals('test.com', $request->getHttpHost());
  155. $this->assertFalse($request->isSecure());
  156. $request = Request::create('http://test.com:90/?test=1');
  157. $this->assertEquals('http://test.com:90/?test=1', $request->getUri());
  158. $this->assertEquals('/', $request->getPathInfo());
  159. $this->assertEquals('test=1', $request->getQueryString());
  160. $this->assertEquals(90, $request->getPort());
  161. $this->assertEquals('test.com:90', $request->getHttpHost());
  162. $this->assertFalse($request->isSecure());
  163. $request = Request::create('http://username:password@test.com');
  164. $this->assertEquals('http://test.com/', $request->getUri());
  165. $this->assertEquals('/', $request->getPathInfo());
  166. $this->assertEquals('', $request->getQueryString());
  167. $this->assertEquals(80, $request->getPort());
  168. $this->assertEquals('test.com', $request->getHttpHost());
  169. $this->assertEquals('username', $request->getUser());
  170. $this->assertEquals('password', $request->getPassword());
  171. $this->assertFalse($request->isSecure());
  172. $request = Request::create('http://username@test.com');
  173. $this->assertEquals('http://test.com/', $request->getUri());
  174. $this->assertEquals('/', $request->getPathInfo());
  175. $this->assertEquals('', $request->getQueryString());
  176. $this->assertEquals(80, $request->getPort());
  177. $this->assertEquals('test.com', $request->getHttpHost());
  178. $this->assertEquals('username', $request->getUser());
  179. $this->assertSame('', $request->getPassword());
  180. $this->assertFalse($request->isSecure());
  181. $request = Request::create('http://test.com/?foo');
  182. $this->assertEquals('/?foo', $request->getRequestUri());
  183. $this->assertEquals(array('foo' => ''), $request->query->all());
  184. // assume rewrite rule: (.*) --> app/app.php; app/ is a symlink to a symfony web/ directory
  185. $request = Request::create('http://test.com/apparthotel-1234', 'GET', array(), array(), array(),
  186. array(
  187. 'DOCUMENT_ROOT' => '/var/www/www.test.com',
  188. 'SCRIPT_FILENAME' => '/var/www/www.test.com/app/app.php',
  189. 'SCRIPT_NAME' => '/app/app.php',
  190. 'PHP_SELF' => '/app/app.php/apparthotel-1234',
  191. ));
  192. $this->assertEquals('http://test.com/apparthotel-1234', $request->getUri());
  193. $this->assertEquals('/apparthotel-1234', $request->getPathInfo());
  194. $this->assertEquals('', $request->getQueryString());
  195. $this->assertEquals(80, $request->getPort());
  196. $this->assertEquals('test.com', $request->getHttpHost());
  197. $this->assertFalse($request->isSecure());
  198. }
  199. public function testCreateCheckPrecedence()
  200. {
  201. // server is used by default
  202. $request = Request::create('/', 'DELETE', array(), array(), array(), array(
  203. 'HTTP_HOST' => 'example.com',
  204. 'HTTPS' => 'on',
  205. 'SERVER_PORT' => 443,
  206. 'PHP_AUTH_USER' => 'fabien',
  207. 'PHP_AUTH_PW' => 'pa$$',
  208. 'QUERY_STRING' => 'foo=bar',
  209. 'CONTENT_TYPE' => 'application/json',
  210. ));
  211. $this->assertEquals('example.com', $request->getHost());
  212. $this->assertEquals(443, $request->getPort());
  213. $this->assertTrue($request->isSecure());
  214. $this->assertEquals('fabien', $request->getUser());
  215. $this->assertEquals('pa$$', $request->getPassword());
  216. $this->assertEquals('', $request->getQueryString());
  217. $this->assertEquals('application/json', $request->headers->get('CONTENT_TYPE'));
  218. // URI has precedence over server
  219. $request = Request::create('http://thomas:pokemon@example.net:8080/?foo=bar', 'GET', array(), array(), array(), array(
  220. 'HTTP_HOST' => 'example.com',
  221. 'HTTPS' => 'on',
  222. 'SERVER_PORT' => 443,
  223. ));
  224. $this->assertEquals('example.net', $request->getHost());
  225. $this->assertEquals(8080, $request->getPort());
  226. $this->assertFalse($request->isSecure());
  227. $this->assertEquals('thomas', $request->getUser());
  228. $this->assertEquals('pokemon', $request->getPassword());
  229. $this->assertEquals('foo=bar', $request->getQueryString());
  230. }
  231. public function testDuplicate()
  232. {
  233. $request = new Request(array('foo' => 'bar'), array('foo' => 'bar'), array('foo' => 'bar'), array(), array(), array('HTTP_FOO' => 'bar'));
  234. $dup = $request->duplicate();
  235. $this->assertEquals($request->query->all(), $dup->query->all(), '->duplicate() duplicates a request an copy the current query parameters');
  236. $this->assertEquals($request->request->all(), $dup->request->all(), '->duplicate() duplicates a request an copy the current request parameters');
  237. $this->assertEquals($request->attributes->all(), $dup->attributes->all(), '->duplicate() duplicates a request an copy the current attributes');
  238. $this->assertEquals($request->headers->all(), $dup->headers->all(), '->duplicate() duplicates a request an copy the current HTTP headers');
  239. $dup = $request->duplicate(array('foo' => 'foobar'), array('foo' => 'foobar'), array('foo' => 'foobar'), array(), array(), array('HTTP_FOO' => 'foobar'));
  240. $this->assertEquals(array('foo' => 'foobar'), $dup->query->all(), '->duplicate() overrides the query parameters if provided');
  241. $this->assertEquals(array('foo' => 'foobar'), $dup->request->all(), '->duplicate() overrides the request parameters if provided');
  242. $this->assertEquals(array('foo' => 'foobar'), $dup->attributes->all(), '->duplicate() overrides the attributes if provided');
  243. $this->assertEquals(array('foo' => array('foobar')), $dup->headers->all(), '->duplicate() overrides the HTTP header if provided');
  244. }
  245. public function testDuplicateWithFormat()
  246. {
  247. $request = new Request(array(), array(), array('_format' => 'json'));
  248. $dup = $request->duplicate();
  249. $this->assertEquals('json', $dup->getRequestFormat());
  250. $this->assertEquals('json', $dup->attributes->get('_format'));
  251. $request = new Request();
  252. $request->setRequestFormat('xml');
  253. $dup = $request->duplicate();
  254. $this->assertEquals('xml', $dup->getRequestFormat());
  255. }
  256. /**
  257. * @dataProvider getFormatToMimeTypeMapProviderWithAdditionalNullFormat
  258. */
  259. public function testGetFormatFromMimeType($format, $mimeTypes)
  260. {
  261. $request = new Request();
  262. foreach ($mimeTypes as $mime) {
  263. $this->assertEquals($format, $request->getFormat($mime));
  264. }
  265. $request->setFormat($format, $mimeTypes);
  266. foreach ($mimeTypes as $mime) {
  267. $this->assertEquals($format, $request->getFormat($mime));
  268. if (null !== $format) {
  269. $this->assertEquals($mimeTypes[0], $request->getMimeType($format));
  270. }
  271. }
  272. }
  273. public function getFormatToMimeTypeMapProviderWithAdditionalNullFormat()
  274. {
  275. return array_merge(
  276. array(array(null, array(null, 'unexistent-mime-type'))),
  277. $this->getFormatToMimeTypeMapProvider()
  278. );
  279. }
  280. public function testGetFormatFromMimeTypeWithParameters()
  281. {
  282. $request = new Request();
  283. $this->assertEquals('json', $request->getFormat('application/json; charset=utf-8'));
  284. }
  285. /**
  286. * @dataProvider getFormatToMimeTypeMapProvider
  287. */
  288. public function testGetMimeTypeFromFormat($format, $mimeTypes)
  289. {
  290. $request = new Request();
  291. $this->assertEquals($mimeTypes[0], $request->getMimeType($format));
  292. }
  293. /**
  294. * @dataProvider getFormatToMimeTypeMapProvider
  295. */
  296. public function testGetMimeTypesFromFormat($format, $mimeTypes)
  297. {
  298. $this->assertEquals($mimeTypes, Request::getMimeTypes($format));
  299. }
  300. public function testGetMimeTypesFromInexistentFormat()
  301. {
  302. $request = new Request();
  303. $this->assertNull($request->getMimeType('foo'));
  304. $this->assertEquals(array(), Request::getMimeTypes('foo'));
  305. }
  306. public function testGetFormatWithCustomMimeType()
  307. {
  308. $request = new Request();
  309. $request->setFormat('custom', 'application/vnd.foo.api;myversion=2.3');
  310. $this->assertEquals('custom', $request->getFormat('application/vnd.foo.api;myversion=2.3'));
  311. }
  312. public function getFormatToMimeTypeMapProvider()
  313. {
  314. return array(
  315. array('txt', array('text/plain')),
  316. array('js', array('application/javascript', 'application/x-javascript', 'text/javascript')),
  317. array('css', array('text/css')),
  318. array('json', array('application/json', 'application/x-json')),
  319. array('jsonld', array('application/ld+json')),
  320. array('xml', array('text/xml', 'application/xml', 'application/x-xml')),
  321. array('rdf', array('application/rdf+xml')),
  322. array('atom', array('application/atom+xml')),
  323. );
  324. }
  325. public function testGetUri()
  326. {
  327. $server = array();
  328. // Standard Request on non default PORT
  329. // http://host:8080/index.php/path/info?query=string
  330. $server['HTTP_HOST'] = 'host:8080';
  331. $server['SERVER_NAME'] = 'servername';
  332. $server['SERVER_PORT'] = '8080';
  333. $server['QUERY_STRING'] = 'query=string';
  334. $server['REQUEST_URI'] = '/index.php/path/info?query=string';
  335. $server['SCRIPT_NAME'] = '/index.php';
  336. $server['PATH_INFO'] = '/path/info';
  337. $server['PATH_TRANSLATED'] = 'redirect:/index.php/path/info';
  338. $server['PHP_SELF'] = '/index_dev.php/path/info';
  339. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  340. $request = new Request();
  341. $request->initialize(array(), array(), array(), array(), array(), $server);
  342. $this->assertEquals('http://host:8080/index.php/path/info?query=string', $request->getUri(), '->getUri() with non default port');
  343. // Use std port number
  344. $server['HTTP_HOST'] = 'host';
  345. $server['SERVER_NAME'] = 'servername';
  346. $server['SERVER_PORT'] = '80';
  347. $request->initialize(array(), array(), array(), array(), array(), $server);
  348. $this->assertEquals('http://host/index.php/path/info?query=string', $request->getUri(), '->getUri() with default port');
  349. // Without HOST HEADER
  350. unset($server['HTTP_HOST']);
  351. $server['SERVER_NAME'] = 'servername';
  352. $server['SERVER_PORT'] = '80';
  353. $request->initialize(array(), array(), array(), array(), array(), $server);
  354. $this->assertEquals('http://servername/index.php/path/info?query=string', $request->getUri(), '->getUri() with default port without HOST_HEADER');
  355. // Request with URL REWRITING (hide index.php)
  356. // RewriteCond %{REQUEST_FILENAME} !-f
  357. // RewriteRule ^(.*)$ index.php [QSA,L]
  358. // http://host:8080/path/info?query=string
  359. $server = array();
  360. $server['HTTP_HOST'] = 'host:8080';
  361. $server['SERVER_NAME'] = 'servername';
  362. $server['SERVER_PORT'] = '8080';
  363. $server['REDIRECT_QUERY_STRING'] = 'query=string';
  364. $server['REDIRECT_URL'] = '/path/info';
  365. $server['SCRIPT_NAME'] = '/index.php';
  366. $server['QUERY_STRING'] = 'query=string';
  367. $server['REQUEST_URI'] = '/path/info?toto=test&1=1';
  368. $server['SCRIPT_NAME'] = '/index.php';
  369. $server['PHP_SELF'] = '/index.php';
  370. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  371. $request->initialize(array(), array(), array(), array(), array(), $server);
  372. $this->assertEquals('http://host:8080/path/info?query=string', $request->getUri(), '->getUri() with rewrite');
  373. // Use std port number
  374. // http://host/path/info?query=string
  375. $server['HTTP_HOST'] = 'host';
  376. $server['SERVER_NAME'] = 'servername';
  377. $server['SERVER_PORT'] = '80';
  378. $request->initialize(array(), array(), array(), array(), array(), $server);
  379. $this->assertEquals('http://host/path/info?query=string', $request->getUri(), '->getUri() with rewrite and default port');
  380. // Without HOST HEADER
  381. unset($server['HTTP_HOST']);
  382. $server['SERVER_NAME'] = 'servername';
  383. $server['SERVER_PORT'] = '80';
  384. $request->initialize(array(), array(), array(), array(), array(), $server);
  385. $this->assertEquals('http://servername/path/info?query=string', $request->getUri(), '->getUri() with rewrite, default port without HOST_HEADER');
  386. // With encoded characters
  387. $server = array(
  388. 'HTTP_HOST' => 'host:8080',
  389. 'SERVER_NAME' => 'servername',
  390. 'SERVER_PORT' => '8080',
  391. 'QUERY_STRING' => 'query=string',
  392. 'REQUEST_URI' => '/ba%20se/index_dev.php/foo%20bar/in+fo?query=string',
  393. 'SCRIPT_NAME' => '/ba se/index_dev.php',
  394. 'PATH_TRANSLATED' => 'redirect:/index.php/foo bar/in+fo',
  395. 'PHP_SELF' => '/ba se/index_dev.php/path/info',
  396. 'SCRIPT_FILENAME' => '/some/where/ba se/index_dev.php',
  397. );
  398. $request->initialize(array(), array(), array(), array(), array(), $server);
  399. $this->assertEquals(
  400. 'http://host:8080/ba%20se/index_dev.php/foo%20bar/in+fo?query=string',
  401. $request->getUri()
  402. );
  403. // with user info
  404. $server['PHP_AUTH_USER'] = 'fabien';
  405. $request->initialize(array(), array(), array(), array(), array(), $server);
  406. $this->assertEquals('http://host:8080/ba%20se/index_dev.php/foo%20bar/in+fo?query=string', $request->getUri());
  407. $server['PHP_AUTH_PW'] = 'symfony';
  408. $request->initialize(array(), array(), array(), array(), array(), $server);
  409. $this->assertEquals('http://host:8080/ba%20se/index_dev.php/foo%20bar/in+fo?query=string', $request->getUri());
  410. }
  411. public function testGetUriForPath()
  412. {
  413. $request = Request::create('http://test.com/foo?bar=baz');
  414. $this->assertEquals('http://test.com/some/path', $request->getUriForPath('/some/path'));
  415. $request = Request::create('http://test.com:90/foo?bar=baz');
  416. $this->assertEquals('http://test.com:90/some/path', $request->getUriForPath('/some/path'));
  417. $request = Request::create('https://test.com/foo?bar=baz');
  418. $this->assertEquals('https://test.com/some/path', $request->getUriForPath('/some/path'));
  419. $request = Request::create('https://test.com:90/foo?bar=baz');
  420. $this->assertEquals('https://test.com:90/some/path', $request->getUriForPath('/some/path'));
  421. $server = array();
  422. // Standard Request on non default PORT
  423. // http://host:8080/index.php/path/info?query=string
  424. $server['HTTP_HOST'] = 'host:8080';
  425. $server['SERVER_NAME'] = 'servername';
  426. $server['SERVER_PORT'] = '8080';
  427. $server['QUERY_STRING'] = 'query=string';
  428. $server['REQUEST_URI'] = '/index.php/path/info?query=string';
  429. $server['SCRIPT_NAME'] = '/index.php';
  430. $server['PATH_INFO'] = '/path/info';
  431. $server['PATH_TRANSLATED'] = 'redirect:/index.php/path/info';
  432. $server['PHP_SELF'] = '/index_dev.php/path/info';
  433. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  434. $request = new Request();
  435. $request->initialize(array(), array(), array(), array(), array(), $server);
  436. $this->assertEquals('http://host:8080/index.php/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with non default port');
  437. // Use std port number
  438. $server['HTTP_HOST'] = 'host';
  439. $server['SERVER_NAME'] = 'servername';
  440. $server['SERVER_PORT'] = '80';
  441. $request->initialize(array(), array(), array(), array(), array(), $server);
  442. $this->assertEquals('http://host/index.php/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with default port');
  443. // Without HOST HEADER
  444. unset($server['HTTP_HOST']);
  445. $server['SERVER_NAME'] = 'servername';
  446. $server['SERVER_PORT'] = '80';
  447. $request->initialize(array(), array(), array(), array(), array(), $server);
  448. $this->assertEquals('http://servername/index.php/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with default port without HOST_HEADER');
  449. // Request with URL REWRITING (hide index.php)
  450. // RewriteCond %{REQUEST_FILENAME} !-f
  451. // RewriteRule ^(.*)$ index.php [QSA,L]
  452. // http://host:8080/path/info?query=string
  453. $server = array();
  454. $server['HTTP_HOST'] = 'host:8080';
  455. $server['SERVER_NAME'] = 'servername';
  456. $server['SERVER_PORT'] = '8080';
  457. $server['REDIRECT_QUERY_STRING'] = 'query=string';
  458. $server['REDIRECT_URL'] = '/path/info';
  459. $server['SCRIPT_NAME'] = '/index.php';
  460. $server['QUERY_STRING'] = 'query=string';
  461. $server['REQUEST_URI'] = '/path/info?toto=test&1=1';
  462. $server['SCRIPT_NAME'] = '/index.php';
  463. $server['PHP_SELF'] = '/index.php';
  464. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  465. $request->initialize(array(), array(), array(), array(), array(), $server);
  466. $this->assertEquals('http://host:8080/some/path', $request->getUriForPath('/some/path'), '->getUri() with rewrite');
  467. // Use std port number
  468. // http://host/path/info?query=string
  469. $server['HTTP_HOST'] = 'host';
  470. $server['SERVER_NAME'] = 'servername';
  471. $server['SERVER_PORT'] = '80';
  472. $request->initialize(array(), array(), array(), array(), array(), $server);
  473. $this->assertEquals('http://host/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with rewrite and default port');
  474. // Without HOST HEADER
  475. unset($server['HTTP_HOST']);
  476. $server['SERVER_NAME'] = 'servername';
  477. $server['SERVER_PORT'] = '80';
  478. $request->initialize(array(), array(), array(), array(), array(), $server);
  479. $this->assertEquals('http://servername/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with rewrite, default port without HOST_HEADER');
  480. $this->assertEquals('servername', $request->getHttpHost());
  481. // with user info
  482. $server['PHP_AUTH_USER'] = 'fabien';
  483. $request->initialize(array(), array(), array(), array(), array(), $server);
  484. $this->assertEquals('http://servername/some/path', $request->getUriForPath('/some/path'));
  485. $server['PHP_AUTH_PW'] = 'symfony';
  486. $request->initialize(array(), array(), array(), array(), array(), $server);
  487. $this->assertEquals('http://servername/some/path', $request->getUriForPath('/some/path'));
  488. }
  489. /**
  490. * @dataProvider getRelativeUriForPathData()
  491. */
  492. public function testGetRelativeUriForPath($expected, $pathinfo, $path)
  493. {
  494. $this->assertEquals($expected, Request::create($pathinfo)->getRelativeUriForPath($path));
  495. }
  496. public function getRelativeUriForPathData()
  497. {
  498. return array(
  499. array('me.png', '/foo', '/me.png'),
  500. array('../me.png', '/foo/bar', '/me.png'),
  501. array('me.png', '/foo/bar', '/foo/me.png'),
  502. array('../baz/me.png', '/foo/bar/b', '/foo/baz/me.png'),
  503. array('../../fooz/baz/me.png', '/foo/bar/b', '/fooz/baz/me.png'),
  504. array('baz/me.png', '/foo/bar/b', 'baz/me.png'),
  505. );
  506. }
  507. public function testGetUserInfo()
  508. {
  509. $request = new Request();
  510. $server = array('PHP_AUTH_USER' => 'fabien');
  511. $request->initialize(array(), array(), array(), array(), array(), $server);
  512. $this->assertEquals('fabien', $request->getUserInfo());
  513. $server['PHP_AUTH_USER'] = '0';
  514. $request->initialize(array(), array(), array(), array(), array(), $server);
  515. $this->assertEquals('0', $request->getUserInfo());
  516. $server['PHP_AUTH_PW'] = '0';
  517. $request->initialize(array(), array(), array(), array(), array(), $server);
  518. $this->assertEquals('0:0', $request->getUserInfo());
  519. }
  520. public function testGetSchemeAndHttpHost()
  521. {
  522. $request = new Request();
  523. $server = array();
  524. $server['SERVER_NAME'] = 'servername';
  525. $server['SERVER_PORT'] = '90';
  526. $request->initialize(array(), array(), array(), array(), array(), $server);
  527. $this->assertEquals('http://servername:90', $request->getSchemeAndHttpHost());
  528. $server['PHP_AUTH_USER'] = 'fabien';
  529. $request->initialize(array(), array(), array(), array(), array(), $server);
  530. $this->assertEquals('http://servername:90', $request->getSchemeAndHttpHost());
  531. $server['PHP_AUTH_USER'] = '0';
  532. $request->initialize(array(), array(), array(), array(), array(), $server);
  533. $this->assertEquals('http://servername:90', $request->getSchemeAndHttpHost());
  534. $server['PHP_AUTH_PW'] = '0';
  535. $request->initialize(array(), array(), array(), array(), array(), $server);
  536. $this->assertEquals('http://servername:90', $request->getSchemeAndHttpHost());
  537. }
  538. /**
  539. * @dataProvider getQueryStringNormalizationData
  540. */
  541. public function testGetQueryString($query, $expectedQuery, $msg)
  542. {
  543. $request = new Request();
  544. $request->server->set('QUERY_STRING', $query);
  545. $this->assertSame($expectedQuery, $request->getQueryString(), $msg);
  546. }
  547. public function getQueryStringNormalizationData()
  548. {
  549. return array(
  550. array('foo', 'foo', 'works with valueless parameters'),
  551. array('foo=', 'foo=', 'includes a dangling equal sign'),
  552. array('bar=&foo=bar', 'bar=&foo=bar', '->works with empty parameters'),
  553. array('foo=bar&bar=', 'bar=&foo=bar', 'sorts keys alphabetically'),
  554. // GET parameters, that are submitted from a HTML form, encode spaces as "+" by default (as defined in enctype application/x-www-form-urlencoded).
  555. // PHP also converts "+" to spaces when filling the global _GET or when using the function parse_str.
  556. array('him=John%20Doe&her=Jane+Doe', 'her=Jane%20Doe&him=John%20Doe', 'normalizes spaces in both encodings "%20" and "+"'),
  557. array('foo[]=1&foo[]=2', 'foo%5B%5D=1&foo%5B%5D=2', 'allows array notation'),
  558. array('foo=1&foo=2', 'foo=1&foo=2', 'allows repeated parameters'),
  559. array('pa%3Dram=foo%26bar%3Dbaz&test=test', 'pa%3Dram=foo%26bar%3Dbaz&test=test', 'works with encoded delimiters'),
  560. array('0', '0', 'allows "0"'),
  561. array('Jane Doe&John%20Doe', 'Jane%20Doe&John%20Doe', 'normalizes encoding in keys'),
  562. array('her=Jane Doe&him=John%20Doe', 'her=Jane%20Doe&him=John%20Doe', 'normalizes encoding in values'),
  563. array('foo=bar&&&test&&', 'foo=bar&test', 'removes unneeded delimiters'),
  564. array('formula=e=m*c^2', 'formula=e%3Dm%2Ac%5E2', 'correctly treats only the first "=" as delimiter and the next as value'),
  565. // Ignore pairs with empty key, even if there was a value, e.g. "=value", as such nameless values cannot be retrieved anyway.
  566. // PHP also does not include them when building _GET.
  567. array('foo=bar&=a=b&=x=y', 'foo=bar', 'removes params with empty key'),
  568. );
  569. }
  570. public function testGetQueryStringReturnsNull()
  571. {
  572. $request = new Request();
  573. $this->assertNull($request->getQueryString(), '->getQueryString() returns null for non-existent query string');
  574. $request->server->set('QUERY_STRING', '');
  575. $this->assertNull($request->getQueryString(), '->getQueryString() returns null for empty query string');
  576. }
  577. public function testGetHost()
  578. {
  579. $request = new Request();
  580. $request->initialize(array('foo' => 'bar'));
  581. $this->assertEquals('', $request->getHost(), '->getHost() return empty string if not initialized');
  582. $request->initialize(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'www.example.com'));
  583. $this->assertEquals('www.example.com', $request->getHost(), '->getHost() from Host Header');
  584. // Host header with port number
  585. $request->initialize(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'www.example.com:8080'));
  586. $this->assertEquals('www.example.com', $request->getHost(), '->getHost() from Host Header with port number');
  587. // Server values
  588. $request->initialize(array(), array(), array(), array(), array(), array('SERVER_NAME' => 'www.example.com'));
  589. $this->assertEquals('www.example.com', $request->getHost(), '->getHost() from server name');
  590. $request->initialize(array(), array(), array(), array(), array(), array('SERVER_NAME' => 'www.example.com', 'HTTP_HOST' => 'www.host.com'));
  591. $this->assertEquals('www.host.com', $request->getHost(), '->getHost() value from Host header has priority over SERVER_NAME ');
  592. }
  593. public function testGetPort()
  594. {
  595. $request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
  596. 'HTTP_X_FORWARDED_PROTO' => 'https',
  597. 'HTTP_X_FORWARDED_PORT' => '443',
  598. ));
  599. $port = $request->getPort();
  600. $this->assertEquals(80, $port, 'Without trusted proxies FORWARDED_PROTO and FORWARDED_PORT are ignored.');
  601. Request::setTrustedProxies(array('1.1.1.1'), Request::HEADER_X_FORWARDED_ALL);
  602. $request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
  603. 'HTTP_X_FORWARDED_PROTO' => 'https',
  604. 'HTTP_X_FORWARDED_PORT' => '8443',
  605. ));
  606. $this->assertEquals(80, $request->getPort(), 'With PROTO and PORT on untrusted connection server value takes precedence.');
  607. $request->server->set('REMOTE_ADDR', '1.1.1.1');
  608. $this->assertEquals(8443, $request->getPort(), 'With PROTO and PORT set PORT takes precedence.');
  609. $request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
  610. 'HTTP_X_FORWARDED_PROTO' => 'https',
  611. ));
  612. $this->assertEquals(80, $request->getPort(), 'With only PROTO set getPort() ignores trusted headers on untrusted connection.');
  613. $request->server->set('REMOTE_ADDR', '1.1.1.1');
  614. $this->assertEquals(443, $request->getPort(), 'With only PROTO set getPort() defaults to 443.');
  615. $request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
  616. 'HTTP_X_FORWARDED_PROTO' => 'http',
  617. ));
  618. $this->assertEquals(80, $request->getPort(), 'If X_FORWARDED_PROTO is set to HTTP getPort() ignores trusted headers on untrusted connection.');
  619. $request->server->set('REMOTE_ADDR', '1.1.1.1');
  620. $this->assertEquals(80, $request->getPort(), 'If X_FORWARDED_PROTO is set to HTTP getPort() returns port of the original request.');
  621. $request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
  622. 'HTTP_X_FORWARDED_PROTO' => 'On',
  623. ));
  624. $this->assertEquals(80, $request->getPort(), 'With only PROTO set and value is On, getPort() ignores trusted headers on untrusted connection.');
  625. $request->server->set('REMOTE_ADDR', '1.1.1.1');
  626. $this->assertEquals(443, $request->getPort(), 'With only PROTO set and value is On, getPort() defaults to 443.');
  627. $request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
  628. 'HTTP_X_FORWARDED_PROTO' => '1',
  629. ));
  630. $this->assertEquals(80, $request->getPort(), 'With only PROTO set and value is 1, getPort() ignores trusted headers on untrusted connection.');
  631. $request->server->set('REMOTE_ADDR', '1.1.1.1');
  632. $this->assertEquals(443, $request->getPort(), 'With only PROTO set and value is 1, getPort() defaults to 443.');
  633. $request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
  634. 'HTTP_X_FORWARDED_PROTO' => 'something-else',
  635. ));
  636. $port = $request->getPort();
  637. $this->assertEquals(80, $port, 'With only PROTO set and value is not recognized, getPort() defaults to 80.');
  638. }
  639. /**
  640. * @expectedException \RuntimeException
  641. */
  642. public function testGetHostWithFakeHttpHostValue()
  643. {
  644. $request = new Request();
  645. $request->initialize(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'www.host.com?query=string'));
  646. $request->getHost();
  647. }
  648. public function testGetSetMethod()
  649. {
  650. $request = new Request();
  651. $this->assertEquals('GET', $request->getMethod(), '->getMethod() returns GET if no method is defined');
  652. $request->setMethod('get');
  653. $this->assertEquals('GET', $request->getMethod(), '->getMethod() returns an uppercased string');
  654. $request->setMethod('PURGE');
  655. $this->assertEquals('PURGE', $request->getMethod(), '->getMethod() returns the method even if it is not a standard one');
  656. $request->setMethod('POST');
  657. $this->assertEquals('POST', $request->getMethod(), '->getMethod() returns the method POST if no _method is defined');
  658. $request->setMethod('POST');
  659. $request->request->set('_method', 'purge');
  660. $this->assertEquals('POST', $request->getMethod(), '->getMethod() does not return the method from _method if defined and POST but support not enabled');
  661. $request = new Request();
  662. $request->setMethod('POST');
  663. $request->request->set('_method', 'purge');
  664. $this->assertFalse(Request::getHttpMethodParameterOverride(), 'httpMethodParameterOverride should be disabled by default');
  665. Request::enableHttpMethodParameterOverride();
  666. $this->assertTrue(Request::getHttpMethodParameterOverride(), 'httpMethodParameterOverride should be enabled now but it is not');
  667. $this->assertEquals('PURGE', $request->getMethod(), '->getMethod() returns the method from _method if defined and POST');
  668. $this->disableHttpMethodParameterOverride();
  669. $request = new Request();
  670. $request->setMethod('POST');
  671. $request->query->set('_method', 'purge');
  672. $this->assertEquals('POST', $request->getMethod(), '->getMethod() does not return the method from _method if defined and POST but support not enabled');
  673. $request = new Request();
  674. $request->setMethod('POST');
  675. $request->query->set('_method', 'purge');
  676. Request::enableHttpMethodParameterOverride();
  677. $this->assertEquals('PURGE', $request->getMethod(), '->getMethod() returns the method from _method if defined and POST');
  678. $this->disableHttpMethodParameterOverride();
  679. $request = new Request();
  680. $request->setMethod('POST');
  681. $request->headers->set('X-HTTP-METHOD-OVERRIDE', 'delete');
  682. $this->assertEquals('DELETE', $request->getMethod(), '->getMethod() returns the method from X-HTTP-Method-Override even though _method is set if defined and POST');
  683. $request = new Request();
  684. $request->setMethod('POST');
  685. $request->headers->set('X-HTTP-METHOD-OVERRIDE', 'delete');
  686. $this->assertEquals('DELETE', $request->getMethod(), '->getMethod() returns the method from X-HTTP-Method-Override if defined and POST');
  687. }
  688. /**
  689. * @dataProvider getClientIpsProvider
  690. */
  691. public function testGetClientIp($expected, $remoteAddr, $httpForwardedFor, $trustedProxies)
  692. {
  693. $request = $this->getRequestInstanceForClientIpTests($remoteAddr, $httpForwardedFor, $trustedProxies);
  694. $this->assertEquals($expected[0], $request->getClientIp());
  695. }
  696. /**
  697. * @dataProvider getClientIpsProvider
  698. */
  699. public function testGetClientIps($expected, $remoteAddr, $httpForwardedFor, $trustedProxies)
  700. {
  701. $request = $this->getRequestInstanceForClientIpTests($remoteAddr, $httpForwardedFor, $trustedProxies);
  702. $this->assertEquals($expected, $request->getClientIps());
  703. }
  704. /**
  705. * @dataProvider getClientIpsForwardedProvider
  706. */
  707. public function testGetClientIpsForwarded($expected, $remoteAddr, $httpForwarded, $trustedProxies)
  708. {
  709. $request = $this->getRequestInstanceForClientIpsForwardedTests($remoteAddr, $httpForwarded, $trustedProxies);
  710. $this->assertEquals($expected, $request->getClientIps());
  711. }
  712. public function getClientIpsForwardedProvider()
  713. {
  714. // $expected $remoteAddr $httpForwarded $trustedProxies
  715. return array(
  716. array(array('127.0.0.1'), '127.0.0.1', 'for="_gazonk"', null),
  717. array(array('127.0.0.1'), '127.0.0.1', 'for="_gazonk"', array('127.0.0.1')),
  718. array(array('88.88.88.88'), '127.0.0.1', 'for="88.88.88.88:80"', array('127.0.0.1')),
  719. array(array('192.0.2.60'), '::1', 'for=192.0.2.60;proto=http;by=203.0.113.43', array('::1')),
  720. array(array('2620:0:1cfe:face:b00c::3', '192.0.2.43'), '::1', 'for=192.0.2.43, for=2620:0:1cfe:face:b00c::3', array('::1')),
  721. array(array('2001:db8:cafe::17'), '::1', 'for="[2001:db8:cafe::17]:4711', array('::1')),
  722. );
  723. }
  724. public function getClientIpsProvider()
  725. {
  726. // $expected $remoteAddr $httpForwardedFor $trustedProxies
  727. return array(
  728. // simple IPv4
  729. array(array('88.88.88.88'), '88.88.88.88', null, null),
  730. // trust the IPv4 remote addr
  731. array(array('88.88.88.88'), '88.88.88.88', null, array('88.88.88.88')),
  732. // simple IPv6
  733. array(array('::1'), '::1', null, null),
  734. // trust the IPv6 remote addr
  735. array(array('::1'), '::1', null, array('::1')),
  736. // forwarded for with remote IPv4 addr not trusted
  737. array(array('127.0.0.1'), '127.0.0.1', '88.88.88.88', null),
  738. // forwarded for with remote IPv4 addr trusted
  739. array(array('88.88.88.88'), '127.0.0.1', '88.88.88.88', array('127.0.0.1')),
  740. // forwarded for with remote IPv4 and all FF addrs trusted
  741. array(array('88.88.88.88'), '127.0.0.1', '88.88.88.88', array('127.0.0.1', '88.88.88.88')),
  742. // forwarded for with remote IPv4 range trusted
  743. array(array('88.88.88.88'), '123.45.67.89', '88.88.88.88', array('123.45.67.0/24')),
  744. // forwarded for with remote IPv6 addr not trusted
  745. array(array('1620:0:1cfe:face:b00c::3'), '1620:0:1cfe:face:b00c::3', '2620:0:1cfe:face:b00c::3', null),
  746. // forwarded for with remote IPv6 addr trusted
  747. array(array('2620:0:1cfe:face:b00c::3'), '1620:0:1cfe:face:b00c::3', '2620:0:1cfe:face:b00c::3', array('1620:0:1cfe:face:b00c::3')),
  748. // forwarded for with remote IPv6 range trusted
  749. array(array('88.88.88.88'), '2a01:198:603:0:396e:4789:8e99:890f', '88.88.88.88', array('2a01:198:603:0::/65')),
  750. // multiple forwarded for with remote IPv4 addr trusted
  751. array(array('88.88.88.88', '87.65.43.21', '127.0.0.1'), '123.45.67.89', '127.0.0.1, 87.65.43.21, 88.88.88.88', array('123.45.67.89')),
  752. // multiple forwarded for with remote IPv4 addr and some reverse proxies trusted
  753. array(array('87.65.43.21', '127.0.0.1'), '123.45.67.89', '127.0.0.1, 87.65.43.21, 88.88.88.88', array('123.45.67.89', '88.88.88.88')),
  754. // multiple forwarded for with remote IPv4 addr and some reverse proxies trusted but in the middle
  755. array(array('88.88.88.88', '127.0.0.1'), '123.45.67.89', '127.0.0.1, 87.65.43.21, 88.88.88.88', array('123.45.67.89', '87.65.43.21')),
  756. // multiple forwarded for with remote IPv4 addr and all reverse proxies trusted
  757. array(array('127.0.0.1'), '123.45.67.89', '127.0.0.1, 87.65.43.21, 88.88.88.88', array('123.45.67.89', '87.65.43.21', '88.88.88.88', '127.0.0.1')),
  758. // multiple forwarded for with remote IPv6 addr trusted
  759. array(array('2620:0:1cfe:face:b00c::3', '3620:0:1cfe:face:b00c::3'), '1620:0:1cfe:face:b00c::3', '3620:0:1cfe:face:b00c::3,2620:0:1cfe:face:b00c::3', array('1620:0:1cfe:face:b00c::3')),
  760. // multiple forwarded for with remote IPv6 addr and some reverse proxies trusted
  761. array(array('3620:0:1cfe:face:b00c::3'), '1620:0:1cfe:face:b00c::3', '3620:0:1cfe:face:b00c::3,2620:0:1cfe:face:b00c::3', array('1620:0:1cfe:face:b00c::3', '2620:0:1cfe:face:b00c::3')),
  762. // multiple forwarded for with remote IPv4 addr and some reverse proxies trusted but in the middle
  763. array(array('2620:0:1cfe:face:b00c::3', '4620:0:1cfe:face:b00c::3'), '1620:0:1cfe:face:b00c::3', '4620:0:1cfe:face:b00c::3,3620:0:1cfe:face:b00c::3,2620:0:1cfe:face:b00c::3', array('1620:0:1cfe:face:b00c::3', '3620:0:1cfe:face:b00c::3')),
  764. // client IP with port
  765. array(array('88.88.88.88'), '127.0.0.1', '88.88.88.88:12345, 127.0.0.1', array('127.0.0.1')),
  766. // invalid forwarded IP is ignored
  767. array(array('88.88.88.88'), '127.0.0.1', 'unknown,88.88.88.88', array('127.0.0.1')),
  768. array(array('88.88.88.88'), '127.0.0.1', '}__test|O:21:&quot;JDatabaseDriverMysqli&quot;:3:{s:2,88.88.88.88', array('127.0.0.1')),
  769. );
  770. }
  771. /**
  772. * @expectedException \Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException
  773. * @dataProvider getClientIpsWithConflictingHeadersProvider
  774. */
  775. public function testGetClientIpsWithConflictingHeaders($httpForwarded, $httpXForwardedFor)
  776. {
  777. $request = new Request();
  778. $server = array(
  779. 'REMOTE_ADDR' => '88.88.88.88',
  780. 'HTTP_FORWARDED' => $httpForwarded,
  781. 'HTTP_X_FORWARDED_FOR' => $httpXForwardedFor,
  782. );
  783. Request::setTrustedProxies(array('88.88.88.88'), Request::HEADER_X_FORWARDED_ALL | Request::HEADER_FORWARDED);
  784. $request->initialize(array(), array(), array(), array(), array(), $server);
  785. $request->getClientIps();
  786. }
  787. /**
  788. * @dataProvider getClientIpsWithConflictingHeadersProvider
  789. */
  790. public function testGetClientIpsOnlyXHttpForwardedForTrusted($httpForwarded, $httpXForwardedFor)
  791. {
  792. $request = new Request();
  793. $server = array(
  794. 'REMOTE_ADDR' => '88.88.88.88',
  795. 'HTTP_FORWARDED' => $httpForwarded,
  796. 'HTTP_X_FORWARDED_FOR' => $httpXForwardedFor,
  797. );
  798. Request::setTrustedProxies(array('88.88.88.88'), Request::HEADER_X_FORWARDED_FOR);
  799. $request->initialize(array(), array(), array(), array(), array(), $server);
  800. $this->assertSame(array_reverse(explode(',', $httpXForwardedFor)), $request->getClientIps());
  801. }
  802. public function getClientIpsWithConflictingHeadersProvider()
  803. {
  804. // $httpForwarded $httpXForwardedFor
  805. return array(
  806. array('for=87.65.43.21', '192.0.2.60'),
  807. array('for=87.65.43.21, for=192.0.2.60', '192.0.2.60'),
  808. array('for=192.0.2.60', '192.0.2.60,87.65.43.21'),
  809. array('for="::face", for=192.0.2.60', '192.0.2.60,192.0.2.43'),
  810. array('for=87.65.43.21, for=192.0.2.60', '192.0.2.60,87.65.43.21'),
  811. );
  812. }
  813. /**
  814. * @dataProvider getClientIpsWithAgreeingHeadersProvider
  815. */
  816. public function testGetClientIpsWithAgreeingHeaders($httpForwarded, $httpXForwardedFor, $expectedIps)
  817. {
  818. $request = new Request();
  819. $server = array(
  820. 'REMOTE_ADDR' => '88.88.88.88',
  821. 'HTTP_FORWARDED' => $httpForwarded,
  822. 'HTTP_X_FORWARDED_FOR' => $httpXForwardedFor,
  823. );
  824. Request::setTrustedProxies(array('88.88.88.88'), Request::HEADER_X_FORWARDED_ALL);
  825. $request->initialize(array(), array(), array(), array(), array(), $server);
  826. $clientIps = $request->getClientIps();
  827. $this->assertSame($expectedIps, $clientIps);
  828. }
  829. public function getClientIpsWithAgreeingHeadersProvider()
  830. {
  831. // $httpForwarded $httpXForwardedFor
  832. return array(
  833. array('for="192.0.2.60"', '192.0.2.60', array('192.0.2.60')),
  834. array('for=192.0.2.60, for=87.65.43.21', '192.0.2.60,87.65.43.21', array('87.65.43.21', '192.0.2.60')),
  835. array('for="[::face]", for=192.0.2.60', '::face,192.0.2.60', array('192.0.2.60', '::face')),
  836. array('for="192.0.2.60:80"', '192.0.2.60', array('192.0.2.60')),
  837. array('for=192.0.2.60;proto=http;by=203.0.113.43', '192.0.2.60', array('192.0.2.60')),
  838. array('for="[2001:db8:cafe::17]:4711"', '2001:db8:cafe::17', array('2001:db8:cafe::17')),
  839. );
  840. }
  841. public function testGetContentWorksTwiceInDefaultMode()
  842. {
  843. $req = new Request();
  844. $this->assertEquals('', $req->getContent());
  845. $this->assertEquals('', $req->getContent());
  846. }
  847. public function testGetContentReturnsResource()
  848. {
  849. $req = new Request();
  850. $retval = $req->getContent(true);
  851. $this->assertInternalType('resource', $retval);
  852. $this->assertEquals('', fread($retval, 1));
  853. $this->assertTrue(feof($retval));
  854. }
  855. public function testGetContentReturnsResourceWhenContentSetInConstructor()
  856. {
  857. $req = new Request(array(), array(), array(), array(), array(), array(), 'MyContent');
  858. $resource = $req->getContent(true);
  859. $this->assertInternalType('resource', $resource);
  860. $this->assertEquals('MyContent', stream_get_contents($resource));
  861. }
  862. public function testContentAsResource()
  863. {
  864. $resource = fopen('php://memory', 'r+');
  865. fwrite($resource, 'My other content');
  866. rewind($resource);
  867. $req = new Request(array(), array(), array(), array(), array(), array(), $resource);
  868. $this->assertEquals('My other content', stream_get_contents($req->getContent(true)));
  869. $this->assertEquals('My other content', $req->getContent());
  870. }
  871. /**
  872. * @expectedException \LogicException
  873. * @dataProvider getContentCantBeCalledTwiceWithResourcesProvider
  874. */
  875. public function testGetContentCantBeCalledTwiceWithResources($first, $second)
  876. {
  877. if (\PHP_VERSION_ID >= 50600) {
  878. $this->markTestSkipped('PHP >= 5.6 allows to open php://input several times.');
  879. }
  880. $req = new Request();
  881. $req->getContent($first);
  882. $req->getContent($second);
  883. }
  884. public function getContentCantBeCalledTwiceWithResourcesProvider()
  885. {
  886. return array(
  887. 'Resource then fetch' => array(true, false),
  888. 'Resource then resource' => array(true, true),
  889. );
  890. }
  891. /**
  892. * @dataProvider getContentCanBeCalledTwiceWithResourcesProvider
  893. * @requires PHP 5.6
  894. */
  895. public function testGetContentCanBeCalledTwiceWithResources($first, $second)
  896. {
  897. $req = new Request();
  898. $a = $req->getContent($first);
  899. $b = $req->getContent($second);
  900. if ($first) {
  901. $a = stream_get_contents($a);
  902. }
  903. if ($second) {
  904. $b = stream_get_contents($b);
  905. }
  906. $this->assertSame($a, $b);
  907. }
  908. public function getContentCanBeCalledTwiceWithResourcesProvider()
  909. {
  910. return array(
  911. 'Fetch then fetch' => array(false, false),
  912. 'Fetch then resource' => array(false, true),
  913. 'Resource then fetch' => array(true, false),
  914. 'Resource then resource' => array(true, true),
  915. );
  916. }
  917. public function provideOverloadedMethods()
  918. {
  919. return array(
  920. array('PUT'),
  921. array('DELETE'),
  922. array('PATCH'),
  923. array('put'),
  924. array('delete'),
  925. array('patch'),
  926. );
  927. }
  928. /**
  929. * @dataProvider provideOverloadedMethods
  930. */
  931. public function testCreateFromGlobals($method)
  932. {
  933. $normalizedMethod = strtoupper($method);
  934. $_GET['foo1'] = 'bar1';
  935. $_POST['foo2'] = 'bar2';
  936. $_COOKIE['foo3'] = 'bar3';
  937. $_FILES['foo4'] = array('bar4');
  938. $_SERVER['foo5'] = 'bar5';
  939. $request = Request::createFromGlobals();
  940. $this->assertEquals('bar1', $request->query->get('foo1'), '::fromGlobals() uses values from $_GET');
  941. $this->assertEquals('bar2', $request->request->get('foo2'), '::fromGlobals() uses values from $_POST');
  942. $this->assertEquals('bar3', $request->cookies->get('foo3'), '::fromGlobals() uses values from $_COOKIE');
  943. $this->assertEquals(array('bar4'), $request->files->get('foo4'), '::fromGlobals() uses values from $_FILES');
  944. $this->assertEquals('bar5', $request->server->get('foo5'), '::fromGlobals() uses values from $_SERVER');
  945. unset($_GET['foo1'], $_POST['foo2'], $_COOKIE['foo3'], $_FILES['foo4'], $_SERVER['foo5']);
  946. $_SERVER['REQUEST_METHOD'] = $method;
  947. $_SERVER['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
  948. $request = RequestContentProxy::createFromGlobals();
  949. $this->assertEquals($normalizedMethod, $request->getMethod());
  950. $this->assertEquals('mycontent', $request->request->get('content'));
  951. unset($_SERVER['REQUEST_METHOD'], $_SERVER['CONTENT_TYPE']);
  952. Request::createFromGlobals();
  953. Request::enableHttpMethodParameterOverride();
  954. $_POST['_method'] = $method;
  955. $_POST['foo6'] = 'bar6';
  956. $_SERVER['REQUEST_METHOD'] = 'PoSt';
  957. $request = Request::createFromGlobals();
  958. $this->assertEquals($normalizedMethod, $request->getMethod());
  959. $this->assertEquals('POST', $request->getRealMethod());
  960. $this->assertEquals('bar6', $request->request->get('foo6'));
  961. unset($_POST['_method'], $_POST['foo6'], $_SERVER['REQUEST_METHOD']);
  962. $this->disableHttpMethodParameterOverride();
  963. }
  964. public function testOverrideGlobals()
  965. {
  966. $request = new Request();
  967. $request->initialize(array('foo' => 'bar'));
  968. // as the Request::overrideGlobals really work, it erase $_SERVER, so we must backup it
  969. $server = $_SERVER;
  970. $request->overrideGlobals();
  971. $this->assertEquals(array('foo' => 'bar'), $_GET);
  972. $request->initialize(array(), array('foo' => 'bar'));
  973. $request->overrideGlobals();
  974. $this->assertEquals(array('foo' => 'bar'), $_POST);
  975. $this->assertArrayNotHasKey('HTTP_X_FORWARDED_PROTO', $_SERVER);
  976. $request->headers->set('X_FORWARDED_PROTO', 'https');
  977. Request::setTrustedProxies(array('1.1.1.1'), Request::HEADER_X_FORWARDED_ALL);
  978. $this->assertFalse($request->isSecure());
  979. $request->server->set('REMOTE_ADDR', '1.1.1.1');
  980. $this->assertTrue($request->isSecure());
  981. $request->overrideGlobals();
  982. $this->assertArrayHasKey('HTTP_X_FORWARDED_PROTO', $_SERVER);
  983. $request->headers->set('CONTENT_TYPE', 'multipart/form-data');
  984. $request->headers->set('CONTENT_LENGTH', 12345);
  985. $request->overrideGlobals();
  986. $this->assertArrayHasKey('CONTENT_TYPE', $_SERVER);
  987. $this->assertArrayHasKey('CONTENT_LENGTH', $_SERVER);
  988. $request->initialize(array('foo' => 'bar', 'baz' => 'foo'));
  989. $request->query->remove('baz');
  990. $request->overrideGlobals();
  991. $this->assertEquals(array('foo' => 'bar'), $_GET);
  992. $this->assertEquals('foo=bar', $_SERVER['QUERY_STRING']);
  993. $this->assertEquals('foo=bar', $request->server->get('QUERY_STRING'));
  994. // restore initial $_SERVER array
  995. $_SERVER = $server;
  996. }
  997. public function testGetScriptName()
  998. {
  999. $request = new Request();
  1000. $this->assertEquals('', $request->getScriptName());
  1001. $server = array();
  1002. $server['SCRIPT_NAME'] = '/index.php';
  1003. $request->initialize(array(), array(), array(), array(), array(), $server);
  1004. $this->assertEquals('/index.php', $request->getScriptName());
  1005. $server = array();
  1006. $server['ORIG_SCRIPT_NAME'] = '/frontend.php';
  1007. $request->initialize(array(), array(), array(), array(), array(), $server);
  1008. $this->assertEquals('/frontend.php', $request->getScriptName());
  1009. $server = array();
  1010. $server['SCRIPT_NAME'] = '/index.php';
  1011. $server['ORIG_SCRIPT_NAME'] = '/frontend.php';
  1012. $request->initialize(array(), array(), array(), array(), array(), $server);
  1013. $this->assertEquals('/index.php', $request->getScriptName());
  1014. }
  1015. public function testGetBasePath()
  1016. {
  1017. $request = new Request();
  1018. $this->assertEquals('', $request->getBasePath());
  1019. $server = array();
  1020. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  1021. $request->initialize(array(), array(), array(), array(), array(), $server);
  1022. $this->assertEquals('', $request->getBasePath());
  1023. $server = array();
  1024. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  1025. $server['SCRIPT_NAME'] = '/index.php';
  1026. $request->initialize(array(), array(), array(), array(), array(), $server);
  1027. $this->assertEquals('', $request->getBasePath());
  1028. $server = array();
  1029. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  1030. $server['PHP_SELF'] = '/index.php';
  1031. $request->initialize(array(), array(), array(), array(), array(), $server);
  1032. $this->assertEquals('', $request->getBasePath());
  1033. $server = array();
  1034. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  1035. $server['ORIG_SCRIPT_NAME'] = '/index.php';
  1036. $request->initialize(array(), array(), array(), array(), array(), $server);
  1037. $this->assertEquals('', $request->getBasePath());
  1038. }
  1039. public function testGetPathInfo()
  1040. {
  1041. $request = new Request();
  1042. $this->assertEquals('/', $request->getPathInfo());
  1043. $server = array();
  1044. $server['REQUEST_URI'] = '/path/info';
  1045. $request->initialize(array(), array(), array(), array(), array(), $server);
  1046. $this->assertEquals('/path/info', $request->getPathInfo());
  1047. $server = array();
  1048. $server['REQUEST_URI'] = '/path%20test/info';
  1049. $request->initialize(array(), array(), array(), array(), array(), $server);
  1050. $this->assertEquals('/path%20test/info', $request->getPathInfo());
  1051. $server = array();
  1052. $server['REQUEST_URI'] = '?a=b';
  1053. $request->initialize(array(), array(), array(), array(), array(), $server);
  1054. $this->assertEquals('/', $request->getPathInfo());
  1055. }
  1056. public function testGetParameterPrecedence()
  1057. {
  1058. $request = new Request();
  1059. $request->attributes->set('foo', 'attr');
  1060. $request->query->set('foo', 'query');
  1061. $request->request->set('foo', 'body');
  1062. $this->assertSame('attr', $request->get('foo'));
  1063. $request->attributes->remove('foo');
  1064. $this->assertSame('query', $request->get('foo'));
  1065. $request->query->remove('foo');
  1066. $this->assertSame('body', $request->get('foo'));
  1067. $request->request->remove('foo');
  1068. $this->assertNull($request->get('foo'));
  1069. }
  1070. public function testGetPreferredLanguage()
  1071. {
  1072. $request = new Request();
  1073. $this->assertNull($request->getPreferredLanguage());
  1074. $this->assertNull($request->getPreferredLanguage(array()));
  1075. $this->assertEquals('fr', $request->getPreferredLanguage(array('fr')));
  1076. $this->assertEquals('fr', $request->getPreferredLanguage(array('fr', 'en')));
  1077. $this->assertEquals('en', $request->getPreferredLanguage(array('en', 'fr')));
  1078. $this->assertEquals('fr-ch', $request->getPreferredLanguage(array('fr-ch', 'fr-fr')));
  1079. $request = new Request();
  1080. $request->headers->set('Accept-language', 'zh, en-us; q=0.8, en; q=0.6');
  1081. $this->assertEquals('en', $request->getPreferredLanguage(array('en', 'en-us')));
  1082. $request = new Request();
  1083. $request->headers->set('Accept-language', 'zh, en-us; q=0.8, en; q=0.6');
  1084. $this->assertEquals('en', $request->getPreferredLanguage(array('fr', 'en')));
  1085. $request = new Request();
  1086. $request->headers->set('Accept-language', 'zh, en-us; q=0.8');
  1087. $this->assertEquals('en', $request->getPreferredLanguage(array('fr', 'en')));
  1088. $request = new Request();
  1089. $request->headers->set('Accept-language', 'zh, en-us; q=0.8, fr-fr; q=0.6, fr; q=0.5');
  1090. $this->assertEquals('en', $request->getPreferredLanguage(array('fr', 'en')));
  1091. }
  1092. public function testIsXmlHttpRequest()
  1093. {
  1094. $request = new Request();
  1095. $this->assertFalse($request->isXmlHttpRequest());
  1096. $request->headers->set('X-Requested-With', 'XMLHttpRequest');
  1097. $this->assertTrue($request->isXmlHttpRequest());
  1098. $request->headers->remove('X-Requested-With');
  1099. $this->assertFalse($request->isXmlHttpRequest());
  1100. }
  1101. /**
  1102. * @requires extension intl
  1103. */
  1104. public function testIntlLocale()
  1105. {
  1106. $request = new Request();
  1107. $request->setDefaultLocale('fr');
  1108. $this->assertEquals('fr', $request->getLocale());
  1109. $this->assertEquals('fr', \Locale::getDefault());
  1110. $request->setLocale('en');
  1111. $this->assertEquals('en', $request->getLocale());
  1112. $this->assertEquals('en', \Locale::getDefault());
  1113. $request->setDefaultLocale('de');
  1114. $this->assertEquals('en', $request->getLocale());
  1115. $this->assertEquals('en', \Locale::getDefault());
  1116. }
  1117. public function testGetCharsets()
  1118. {
  1119. $request = new Request();
  1120. $this->assertEquals(array(), $request->getCharsets());
  1121. $request->headers->set('Accept-Charset', 'ISO-8859-1, US-ASCII, UTF-8; q=0.8, ISO-10646-UCS-2; q=0.6');
  1122. $this->assertEquals(array(), $request->getCharsets()); // testing caching
  1123. $request = new Request();
  1124. $request->headers->set('Accept-Charset', 'ISO-8859-1, US-ASCII, UTF-8; q=0.8, ISO-10646-UCS-2; q=0.6');
  1125. $this->assertEquals(array('ISO-8859-1', 'US-ASCII', 'UTF-8', 'ISO-10646-UCS-2'), $request->getCharsets());
  1126. $request = new Request();
  1127. $request->headers->set('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7');
  1128. $this->assertEquals(array('ISO-8859-1', 'utf-8', '*'), $request->getCharsets());
  1129. }
  1130. public function testGetEncodings()
  1131. {
  1132. $request = new Request();
  1133. $this->assertEquals(array(), $request->getEncodings());
  1134. $request->headers->set('Accept-Encoding', 'gzip,deflate,sdch');
  1135. $this->assertEquals(array(), $request->getEncodings()); // testing caching
  1136. $request = new Request();
  1137. $request->headers->set('Accept-Encoding', 'gzip,deflate,sdch');
  1138. $this->assertEquals(array('gzip', 'deflate', 'sdch'), $request->getEncodings());
  1139. $request = new Request();
  1140. $request->headers->set('Accept-Encoding', 'gzip;q=0.4,deflate;q=0.9,compress;q=0.7');
  1141. $this->assertEquals(array('deflate', 'compress', 'gzip'), $request->getEncodings());
  1142. }
  1143. public function testGetAcceptableContentTypes()
  1144. {
  1145. $request = new Request();
  1146. $this->assertEquals(array(), $request->getAcceptableContentTypes());
  1147. $request->headers->set('Accept', 'application/vnd.wap.wmlscriptc, text/vnd.wap.wml, application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html, multipart/mixed, */*');
  1148. $this->assertEquals(array(), $request->getAcceptableContentTypes()); // testing caching
  1149. $request = new Request();
  1150. $request->headers->set('Accept', 'application/vnd.wap.wmlscriptc, text/vnd.wap.wml, application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html, multipart/mixed, */*');
  1151. $this->assertEquals(array('application/vnd.wap.wmlscriptc', 'text/vnd.wap.wml', 'application/vnd.wap.xhtml+xml', 'application/xhtml+xml', 'text/html', 'multipart/mixed', '*/*'), $request->getAcceptableContentTypes());
  1152. }
  1153. public function testGetLanguages()
  1154. {
  1155. $request = new Request();
  1156. $this->assertEquals(array(), $request->getLanguages());
  1157. $request = new Request();
  1158. $request->headers->set('Accept-language', 'zh, en-us; q=0.8, en; q=0.6');
  1159. $this->assertEquals(array('zh', 'en_US', 'en'), $request->getLanguages());
  1160. $this->assertEquals(array('zh', 'en_US', 'en'), $request->getLanguages());
  1161. $request = new Request();
  1162. $request->headers->set('Accept-language', 'zh, en-us; q=0.6, en; q=0.8');
  1163. $this->assertEquals(array('zh', 'en', 'en_US'), $request->getLanguages()); // Test out of order qvalues
  1164. $request = new Request();
  1165. $request->headers->set('Accept-language', 'zh, en, en-us');
  1166. $this->assertEquals(array('zh', 'en', 'en_US'), $request->getLanguages()); // Test equal weighting without qvalues
  1167. $request = new Request();
  1168. $request->headers->set('Accept-language', 'zh; q=0.6, en, en-us; q=0.6');
  1169. $this->assertEquals(array('en', 'zh', 'en_US'), $request->getLanguages()); // Test equal weighting with qvalues
  1170. $request = new Request();
  1171. $request->headers->set('Accept-language', 'zh, i-cherokee; q=0.6');
  1172. $this->assertEquals(array('zh', 'cherokee'), $request->getLanguages());
  1173. }
  1174. public function testGetRequestFormat()
  1175. {
  1176. $request = new Request();
  1177. $this->assertEquals('html', $request->getRequestFormat());
  1178. // Ensure that setting different default values over time is possible,
  1179. // aka. setRequestFormat determines the state.
  1180. $this->assertEquals('json', $request->getRequestFormat('json'));
  1181. $this->assertEquals('html', $request->getRequestFormat('html'));
  1182. $request = new Request();
  1183. $this->assertNull($request->getRequestFormat(null));
  1184. $request = new Request();
  1185. $this->assertNull($request->setRequestFormat('foo'));
  1186. $this->assertEquals('foo', $request->getRequestFormat(null));
  1187. $request = new Request(array('_format' => 'foo'));
  1188. $this->assertEquals('html', $request->getRequestFormat());
  1189. }
  1190. public function testHasSession()
  1191. {
  1192. $request = new Request();
  1193. $this->assertFalse($request->hasSession());
  1194. $request->setSession(new Session(new MockArraySessionStorage()));
  1195. $this->assertTrue($request->hasSession());
  1196. }
  1197. public function testGetSession()
  1198. {
  1199. $request = new Request();
  1200. $request->setSession(new Session(new MockArraySessionStorage()));
  1201. $this->assertTrue($request->hasSession());
  1202. $session = $request->getSession();
  1203. $this->assertObjectHasAttribute('storage', $session);
  1204. $this->assertObjectHasAttribute('flashName', $session);
  1205. $this->assertObjectHasAttribute('attributeName', $session);
  1206. }
  1207. public function testHasPreviousSession()
  1208. {
  1209. $request = new Request();
  1210. $this->assertFalse($request->hasPreviousSession());
  1211. $request->cookies->set('MOCKSESSID', 'foo');
  1212. $this->assertFalse($request->hasPreviousSession());
  1213. $request->setSession(new Session(new MockArraySessionStorage()));
  1214. $this->assertTrue($request->hasPreviousSession());
  1215. }
  1216. public function testToString()
  1217. {
  1218. $request = new Request();
  1219. $request->headers->set('Accept-language', 'zh, en-us; q=0.8, en; q=0.6');
  1220. $request->cookies->set('Foo', 'Bar');
  1221. $asString = (string) $request;
  1222. $this->assertContains('Accept-Language: zh, en-us; q=0.8, en; q=0.6', $asString);
  1223. $this->assertContains('Cookie: Foo=Bar', $asString);
  1224. $request->cookies->set('Another', 'Cookie');
  1225. $asString = (string) $request;
  1226. $this->assertContains('Cookie: Foo=Bar; Another=Cookie', $asString);
  1227. }
  1228. public function testIsMethod()
  1229. {
  1230. $request = new Request();
  1231. $request->setMethod('POST');
  1232. $this->assertTrue($request->isMethod('POST'));
  1233. $this->assertTrue($request->isMethod('post'));
  1234. $this->assertFalse($request->isMethod('GET'));
  1235. $this->assertFalse($request->isMethod('get'));
  1236. $request->setMethod('GET');
  1237. $this->assertTrue($request->isMethod('GET'));
  1238. $this->assertTrue($request->isMethod('get'));
  1239. $this->assertFalse($request->isMethod('POST'));
  1240. $this->assertFalse($request->isMethod('post'));
  1241. }
  1242. /**
  1243. * @dataProvider getBaseUrlData
  1244. */
  1245. public function testGetBaseUrl($uri, $server, $expectedBaseUrl, $expectedPathInfo)
  1246. {
  1247. $request = Request::create($uri, 'GET', array(), array(), array(), $server);
  1248. $this->assertSame($expectedBaseUrl, $request->getBaseUrl(), 'baseUrl');
  1249. $this->assertSame($expectedPathInfo, $request->getPathInfo(), 'pathInfo');
  1250. }
  1251. public function getBaseUrlData()
  1252. {
  1253. return array(
  1254. array(
  1255. '/fruit/strawberry/1234index.php/blah',
  1256. array(
  1257. 'SCRIPT_FILENAME' => 'E:/Sites/cc-new/public_html/fruit/index.php',
  1258. 'SCRIPT_NAME' => '/fruit/index.php',
  1259. 'PHP_SELF' => '/fruit/index.php',
  1260. ),
  1261. '/fruit',
  1262. '/strawberry/1234index.php/blah',
  1263. ),
  1264. array(
  1265. '/fruit/strawberry/1234index.php/blah',
  1266. array(
  1267. 'SCRIPT_FILENAME' => 'E:/Sites/cc-new/public_html/index.php',
  1268. 'SCRIPT_NAME' => '/index.php',
  1269. 'PHP_SELF' => '/index.php',
  1270. ),
  1271. '',
  1272. '/fruit/strawberry/1234index.php/blah',
  1273. ),
  1274. array(
  1275. '/foo%20bar/',
  1276. array(
  1277. 'SCRIPT_FILENAME' => '/home/John Doe/public_html/foo bar/app.php',
  1278. 'SCRIPT_NAME' => '/foo bar/app.php',
  1279. 'PHP_SELF' => '/foo bar/app.php',
  1280. ),
  1281. '/foo%20bar',
  1282. '/',
  1283. ),
  1284. array(
  1285. '/foo%20bar/home',
  1286. array(
  1287. 'SCRIPT_FILENAME' => '/home/John Doe/public_html/foo bar/app.php',
  1288. 'SCRIPT_NAME' => '/foo bar/app.php',
  1289. 'PHP_SELF' => '/foo bar/app.php',
  1290. ),
  1291. '/foo%20bar',
  1292. '/home',
  1293. ),
  1294. array(
  1295. '/foo%20bar/app.php/home',
  1296. array(
  1297. 'SCRIPT_FILENAME' => '/home/John Doe/public_html/foo bar/app.php',
  1298. 'SCRIPT_NAME' => '/foo bar/app.php',
  1299. 'PHP_SELF' => '/foo bar/app.php',
  1300. ),
  1301. '/foo%20bar/app.php',
  1302. '/home',
  1303. ),
  1304. array(
  1305. '/foo%20bar/app.php/home%3Dbaz',
  1306. array(
  1307. 'SCRIPT_FILENAME' => '/home/John Doe/public_html/foo bar/app.php',
  1308. 'SCRIPT_NAME' => '/foo bar/app.php',
  1309. 'PHP_SELF' => '/foo bar/app.php',
  1310. ),
  1311. '/foo%20bar/app.php',
  1312. '/home%3Dbaz',
  1313. ),
  1314. array(
  1315. '/foo/bar+baz',
  1316. array(
  1317. 'SCRIPT_FILENAME' => '/home/John Doe/public_html/foo/app.php',
  1318. 'SCRIPT_NAME' => '/foo/app.php',
  1319. 'PHP_SELF' => '/foo/app.php',
  1320. ),
  1321. '/foo',
  1322. '/bar+baz',
  1323. ),
  1324. );
  1325. }
  1326. /**
  1327. * @dataProvider urlencodedStringPrefixData
  1328. */
  1329. public function testUrlencodedStringPrefix($string, $prefix, $expect)
  1330. {
  1331. $request = new Request();
  1332. $me = new \ReflectionMethod($request, 'getUrlencodedPrefix');
  1333. $me->setAccessible(true);
  1334. $this->assertSame($expect, $me->invoke($request, $string, $prefix));
  1335. }
  1336. public function urlencodedStringPrefixData()
  1337. {
  1338. return array(
  1339. array('foo', 'foo', 'foo'),
  1340. array('fo%6f', 'foo', 'fo%6f'),
  1341. array('foo/bar', 'foo', 'foo'),
  1342. array('fo%6f/bar', 'foo', 'fo%6f'),
  1343. array('f%6f%6f/bar', 'foo', 'f%6f%6f'),
  1344. array('%66%6F%6F/bar', 'foo', '%66%6F%6F'),
  1345. array('fo+o/bar', 'fo+o', 'fo+o'),
  1346. array('fo%2Bo/bar', 'fo+o', 'fo%2Bo'),
  1347. );
  1348. }
  1349. private function disableHttpMethodParameterOverride()
  1350. {
  1351. $class = new \ReflectionClass('Symfony\\Component\\HttpFoundation\\Request');
  1352. $property = $class->getProperty('httpMethodParameterOverride');
  1353. $property->setAccessible(true);
  1354. $property->setValue(false);
  1355. }
  1356. private function getRequestInstanceForClientIpTests($remoteAddr, $httpForwardedFor, $trustedProxies)
  1357. {
  1358. $request = new Request();
  1359. $server = array('REMOTE_ADDR' => $remoteAddr);
  1360. if (null !== $httpForwardedFor) {
  1361. $server['HTTP_X_FORWARDED_FOR'] = $httpForwardedFor;
  1362. }
  1363. if ($trustedProxies) {
  1364. Request::setTrustedProxies($trustedProxies, Request::HEADER_X_FORWARDED_ALL);
  1365. }
  1366. $request->initialize(array(), array(), array(), array(), array(), $server);
  1367. return $request;
  1368. }
  1369. private function getRequestInstanceForClientIpsForwardedTests($remoteAddr, $httpForwarded, $trustedProxies)
  1370. {
  1371. $request = new Request();
  1372. $server = array('REMOTE_ADDR' => $remoteAddr);
  1373. if (null !== $httpForwarded) {
  1374. $server['HTTP_FORWARDED'] = $httpForwarded;
  1375. }
  1376. if ($trustedProxies) {
  1377. Request::setTrustedProxies($trustedProxies, Request::HEADER_FORWARDED);
  1378. }
  1379. $request->initialize(array(), array(), array(), array(), array(), $server);
  1380. return $request;
  1381. }
  1382. public function testTrustedProxiesXForwardedFor()
  1383. {
  1384. $request = Request::create('http://example.com/');
  1385. $request->server->set('REMOTE_ADDR', '3.3.3.3');
  1386. $request->headers->set('X_FORWARDED_FOR', '1.1.1.1, 2.2.2.2');
  1387. $request->headers->set('X_FORWARDED_HOST', 'foo.example.com:1234, real.example.com:8080');
  1388. $request->headers->set('X_FORWARDED_PROTO', 'https');
  1389. $request->headers->set('X_FORWARDED_PORT', 443);
  1390. // no trusted proxies
  1391. $this->assertEquals('3.3.3.3', $request->getClientIp());
  1392. $this->assertEquals('example.com', $request->getHost());
  1393. $this->assertEquals(80, $request->getPort());
  1394. $this->assertFalse($request->isSecure());
  1395. // disabling proxy trusting
  1396. Request::setTrustedProxies(array(), Request::HEADER_X_FORWARDED_ALL);
  1397. $this->assertEquals('3.3.3.3', $request->getClientIp());
  1398. $this->assertEquals('example.com', $request->getHost());
  1399. $this->assertEquals(80, $request->getPort());
  1400. $this->assertFalse($request->isSecure());
  1401. // request is forwarded by a non-trusted proxy
  1402. Request::setTrustedProxies(array('2.2.2.2'), Request::HEADER_X_FORWARDED_ALL);
  1403. $this->assertEquals('3.3.3.3', $request->getClientIp());
  1404. $this->assertEquals('example.com', $request->getHost());
  1405. $this->assertEquals(80, $request->getPort());
  1406. $this->assertFalse($request->isSecure());
  1407. // trusted proxy via setTrustedProxies()
  1408. Request::setTrustedProxies(array('3.3.3.3', '2.2.2.2'), Request::HEADER_X_FORWARDED_ALL);
  1409. $this->assertEquals('1.1.1.1', $request->getClientIp());
  1410. $this->assertEquals('foo.example.com', $request->getHost());
  1411. $this->assertEquals(443, $request->getPort());
  1412. $this->assertTrue($request->isSecure());
  1413. // trusted proxy via setTrustedProxies()
  1414. Request::setTrustedProxies(array('3.3.3.4', '2.2.2.2'), Request::HEADER_X_FORWARDED_ALL);
  1415. $this->assertEquals('3.3.3.3', $request->getClientIp());
  1416. $this->assertEquals('example.com', $request->getHost());
  1417. $this->assertEquals(80, $request->getPort());
  1418. $this->assertFalse($request->isSecure());
  1419. // check various X_FORWARDED_PROTO header values
  1420. Request::setTrustedProxies(array('3.3.3.3', '2.2.2.2'), Request::HEADER_X_FORWARDED_ALL);
  1421. $request->headers->set('X_FORWARDED_PROTO', 'ssl');
  1422. $this->assertTrue($request->isSecure());
  1423. $request->headers->set('X_FORWARDED_PROTO', 'https, http');
  1424. $this->assertTrue($request->isSecure());
  1425. }
  1426. /**
  1427. * @group legacy
  1428. * @expectedDeprecation The "Symfony\Component\HttpFoundation\Request::setTrustedHeaderName()" method is deprecated since Symfony 3.3 and will be removed in 4.0. Use the $trustedHeaderSet argument of the Request::setTrustedProxies() method instead.
  1429. */
  1430. public function testLegacyTrustedProxies()
  1431. {
  1432. $request = Request::create('http://example.com/');
  1433. $request->server->set('REMOTE_ADDR', '3.3.3.3');
  1434. $request->headers->set('X_FORWARDED_FOR', '1.1.1.1, 2.2.2.2');
  1435. $request->headers->set('X_FORWARDED_HOST', 'foo.example.com, real.example.com:8080');
  1436. $request->headers->set('X_FORWARDED_PROTO', 'https');
  1437. $request->headers->set('X_FORWARDED_PORT', 443);
  1438. $request->headers->set('X_MY_FOR', '3.3.3.3, 4.4.4.4');
  1439. $request->headers->set('X_MY_HOST', 'my.example.com');
  1440. $request->headers->set('X_MY_PROTO', 'http');
  1441. $request->headers->set('X_MY_PORT', 81);
  1442. Request::setTrustedProxies(array('3.3.3.3', '2.2.2.2'), Request::HEADER_X_FORWARDED_ALL);
  1443. // custom header names
  1444. Request::setTrustedHeaderName(Request::HEADER_CLIENT_IP, 'X_MY_FOR');
  1445. Request::setTrustedHeaderName(Request::HEADER_CLIENT_HOST, 'X_MY_HOST');
  1446. Request::setTrustedHeaderName(Request::HEADER_CLIENT_PORT, 'X_MY_PORT');
  1447. Request::setTrustedHeaderName(Request::HEADER_CLIENT_PROTO, 'X_MY_PROTO');
  1448. $this->assertEquals('4.4.4.4', $request->getClientIp());
  1449. $this->assertEquals('my.example.com', $request->getHost());
  1450. $this->assertEquals(81, $request->getPort());
  1451. $this->assertFalse($request->isSecure());
  1452. // disabling via empty header names
  1453. Request::setTrustedHeaderName(Request::HEADER_CLIENT_IP, null);
  1454. Request::setTrustedHeaderName(Request::HEADER_CLIENT_HOST, null);
  1455. Request::setTrustedHeaderName(Request::HEADER_CLIENT_PORT, null);
  1456. Request::setTrustedHeaderName(Request::HEADER_CLIENT_PROTO, null);
  1457. $this->assertEquals('3.3.3.3', $request->getClientIp());
  1458. $this->assertEquals('example.com', $request->getHost());
  1459. $this->assertEquals(80, $request->getPort());
  1460. $this->assertFalse($request->isSecure());
  1461. //reset
  1462. Request::setTrustedHeaderName(Request::HEADER_FORWARDED, 'FORWARDED');
  1463. Request::setTrustedHeaderName(Request::HEADER_CLIENT_IP, 'X_FORWARDED_FOR');
  1464. Request::setTrustedHeaderName(Request::HEADER_CLIENT_HOST, 'X_FORWARDED_HOST');
  1465. Request::setTrustedHeaderName(Request::HEADER_CLIENT_PORT, 'X_FORWARDED_PORT');
  1466. Request::setTrustedHeaderName(Request::HEADER_CLIENT_PROTO, 'X_FORWARDED_PROTO');
  1467. }
  1468. public function testTrustedProxiesForwarded()
  1469. {
  1470. $request = Request::create('http://example.com/');
  1471. $request->server->set('REMOTE_ADDR', '3.3.3.3');
  1472. $request->headers->set('FORWARDED', 'for=1.1.1.1, host=foo.example.com:8080, proto=https, for=2.2.2.2, host=real.example.com:8080');
  1473. // no trusted proxies
  1474. $this->assertEquals('3.3.3.3', $request->getClientIp());
  1475. $this->assertEquals('example.com', $request->getHost());
  1476. $this->assertEquals(80, $request->getPort());
  1477. $this->assertFalse($request->isSecure());
  1478. // disabling proxy trusting
  1479. Request::setTrustedProxies(array(), Request::HEADER_FORWARDED);
  1480. $this->assertEquals('3.3.3.3', $request->getClientIp());
  1481. $this->assertEquals('example.com', $request->getHost());
  1482. $this->assertEquals(80, $request->getPort());
  1483. $this->assertFalse($request->isSecure());
  1484. // request is forwarded by a non-trusted proxy
  1485. Request::setTrustedProxies(array('2.2.2.2'), Request::HEADER_FORWARDED);
  1486. $this->assertEquals('3.3.3.3', $request->getClientIp());
  1487. $this->assertEquals('example.com', $request->getHost());
  1488. $this->assertEquals(80, $request->getPort());
  1489. $this->assertFalse($request->isSecure());
  1490. // trusted proxy via setTrustedProxies()
  1491. Request::setTrustedProxies(array('3.3.3.3', '2.2.2.2'), Request::HEADER_FORWARDED);
  1492. $this->assertEquals('1.1.1.1', $request->getClientIp());
  1493. $this->assertEquals('foo.example.com', $request->getHost());
  1494. $this->assertEquals(8080, $request->getPort());
  1495. $this->assertTrue($request->isSecure());
  1496. // trusted proxy via setTrustedProxies()
  1497. Request::setTrustedProxies(array('3.3.3.4', '2.2.2.2'), Request::HEADER_FORWARDED);
  1498. $this->assertEquals('3.3.3.3', $request->getClientIp());
  1499. $this->assertEquals('example.com', $request->getHost());
  1500. $this->assertEquals(80, $request->getPort());
  1501. $this->assertFalse($request->isSecure());
  1502. // check various X_FORWARDED_PROTO header values
  1503. Request::setTrustedProxies(array('3.3.3.3', '2.2.2.2'), Request::HEADER_FORWARDED);
  1504. $request->headers->set('FORWARDED', 'proto=ssl');
  1505. $this->assertTrue($request->isSecure());
  1506. $request->headers->set('FORWARDED', 'proto=https, proto=http');
  1507. $this->assertTrue($request->isSecure());
  1508. }
  1509. /**
  1510. * @group legacy
  1511. * @expectedException \InvalidArgumentException
  1512. */
  1513. public function testSetTrustedProxiesInvalidHeaderName()
  1514. {
  1515. Request::create('http://example.com/');
  1516. Request::setTrustedHeaderName('bogus name', 'X_MY_FOR');
  1517. }
  1518. /**
  1519. * @group legacy
  1520. * @expectedException \InvalidArgumentException
  1521. */
  1522. public function testGetTrustedProxiesInvalidHeaderName()
  1523. {
  1524. Request::create('http://example.com/');
  1525. Request::getTrustedHeaderName('bogus name');
  1526. }
  1527. /**
  1528. * @dataProvider iisRequestUriProvider
  1529. */
  1530. public function testIISRequestUri($headers, $server, $expectedRequestUri)
  1531. {
  1532. $request = new Request();
  1533. $request->headers->replace($headers);
  1534. $request->server->replace($server);
  1535. $this->assertEquals($expectedRequestUri, $request->getRequestUri(), '->getRequestUri() is correct');
  1536. $subRequestUri = '/bar/foo';
  1537. $subRequest = Request::create($subRequestUri, 'get', array(), array(), array(), $request->server->all());
  1538. $this->assertEquals($subRequestUri, $subRequest->getRequestUri(), '->getRequestUri() is correct in sub request');
  1539. }
  1540. public function iisRequestUriProvider()
  1541. {
  1542. return array(
  1543. array(
  1544. array(
  1545. 'X_ORIGINAL_URL' => '/foo/bar',
  1546. ),
  1547. array(),
  1548. '/foo/bar',
  1549. ),
  1550. array(
  1551. array(
  1552. 'X_REWRITE_URL' => '/foo/bar',
  1553. ),
  1554. array(),
  1555. '/foo/bar',
  1556. ),
  1557. array(
  1558. array(),
  1559. array(
  1560. 'IIS_WasUrlRewritten' => '1',
  1561. 'UNENCODED_URL' => '/foo/bar',
  1562. ),
  1563. '/foo/bar',
  1564. ),
  1565. array(
  1566. array(
  1567. 'X_ORIGINAL_URL' => '/foo/bar',
  1568. ),
  1569. array(
  1570. 'HTTP_X_ORIGINAL_URL' => '/foo/bar',
  1571. ),
  1572. '/foo/bar',
  1573. ),
  1574. array(
  1575. array(
  1576. 'X_ORIGINAL_URL' => '/foo/bar',
  1577. ),
  1578. array(
  1579. 'IIS_WasUrlRewritten' => '1',
  1580. 'UNENCODED_URL' => '/foo/bar',
  1581. ),
  1582. '/foo/bar',
  1583. ),
  1584. array(
  1585. array(
  1586. 'X_ORIGINAL_URL' => '/foo/bar',
  1587. ),
  1588. array(
  1589. 'HTTP_X_ORIGINAL_URL' => '/foo/bar',
  1590. 'IIS_WasUrlRewritten' => '1',
  1591. 'UNENCODED_URL' => '/foo/bar',
  1592. ),
  1593. '/foo/bar',
  1594. ),
  1595. array(
  1596. array(),
  1597. array(
  1598. 'ORIG_PATH_INFO' => '/foo/bar',
  1599. ),
  1600. '/foo/bar',
  1601. ),
  1602. array(
  1603. array(),
  1604. array(
  1605. 'ORIG_PATH_INFO' => '/foo/bar',
  1606. 'QUERY_STRING' => 'foo=bar',
  1607. ),
  1608. '/foo/bar?foo=bar',
  1609. ),
  1610. );
  1611. }
  1612. public function testTrustedHosts()
  1613. {
  1614. // create a request
  1615. $request = Request::create('/');
  1616. // no trusted host set -> no host check
  1617. $request->headers->set('host', 'evil.com');
  1618. $this->assertEquals('evil.com', $request->getHost());
  1619. // add a trusted domain and all its subdomains
  1620. Request::setTrustedHosts(array('^([a-z]{9}\.)?trusted\.com$'));
  1621. // untrusted host
  1622. $request->headers->set('host', 'evil.com');
  1623. try {
  1624. $request->getHost();
  1625. $this->fail('Request::getHost() should throw an exception when host is not trusted.');
  1626. } catch (SuspiciousOperationException $e) {
  1627. $this->assertEquals('Untrusted Host "evil.com".', $e->getMessage());
  1628. }
  1629. // trusted hosts
  1630. $request->headers->set('host', 'trusted.com');
  1631. $this->assertEquals('trusted.com', $request->getHost());
  1632. $this->assertEquals(80, $request->getPort());
  1633. $request->server->set('HTTPS', true);
  1634. $request->headers->set('host', 'trusted.com');
  1635. $this->assertEquals('trusted.com', $request->getHost());
  1636. $this->assertEquals(443, $request->getPort());
  1637. $request->server->set('HTTPS', false);
  1638. $request->headers->set('host', 'trusted.com:8000');
  1639. $this->assertEquals('trusted.com', $request->getHost());
  1640. $this->assertEquals(8000, $request->getPort());
  1641. $request->headers->set('host', 'subdomain.trusted.com');
  1642. $this->assertEquals('subdomain.trusted.com', $request->getHost());
  1643. // reset request for following tests
  1644. Request::setTrustedHosts(array());
  1645. }
  1646. public function testFactory()
  1647. {
  1648. Request::setFactory(function (array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null) {
  1649. return new NewRequest();
  1650. });
  1651. $this->assertEquals('foo', Request::create('/')->getFoo());
  1652. Request::setFactory(null);
  1653. }
  1654. /**
  1655. * @dataProvider getLongHostNames
  1656. */
  1657. public function testVeryLongHosts($host)
  1658. {
  1659. $start = microtime(true);
  1660. $request = Request::create('/');
  1661. $request->headers->set('host', $host);
  1662. $this->assertEquals($host, $request->getHost());
  1663. $this->assertLessThan(5, microtime(true) - $start);
  1664. }
  1665. /**
  1666. * @dataProvider getHostValidities
  1667. */
  1668. public function testHostValidity($host, $isValid, $expectedHost = null, $expectedPort = null)
  1669. {
  1670. $request = Request::create('/');
  1671. $request->headers->set('host', $host);
  1672. if ($isValid) {
  1673. $this->assertSame($expectedHost ?: $host, $request->getHost());
  1674. if ($expectedPort) {
  1675. $this->assertSame($expectedPort, $request->getPort());
  1676. }
  1677. } else {
  1678. if (method_exists($this, 'expectException')) {
  1679. $this->expectException(SuspiciousOperationException::class);
  1680. $this->expectExceptionMessage('Invalid Host');
  1681. } else {
  1682. $this->setExpectedException(SuspiciousOperationException::class, 'Invalid Host');
  1683. }
  1684. $request->getHost();
  1685. }
  1686. }
  1687. public function getHostValidities()
  1688. {
  1689. return array(
  1690. array('.a', false),
  1691. array('a..', false),
  1692. array('a.', true),
  1693. array("\xE9", false),
  1694. array('[::1]', true),
  1695. array('[::1]:80', true, '[::1]', 80),
  1696. array(str_repeat('.', 101), false),
  1697. );
  1698. }
  1699. public function getLongHostNames()
  1700. {
  1701. return array(
  1702. array('a'.str_repeat('.a', 40000)),
  1703. array(str_repeat(':', 101)),
  1704. );
  1705. }
  1706. /**
  1707. * @dataProvider methodIdempotentProvider
  1708. */
  1709. public function testMethodIdempotent($method, $idempotent)
  1710. {
  1711. $request = new Request();
  1712. $request->setMethod($method);
  1713. $this->assertEquals($idempotent, $request->isMethodIdempotent());
  1714. }
  1715. public function methodIdempotentProvider()
  1716. {
  1717. return array(
  1718. array('HEAD', true),
  1719. array('GET', true),
  1720. array('POST', false),
  1721. array('PUT', true),
  1722. array('PATCH', false),
  1723. array('DELETE', true),
  1724. array('PURGE', true),
  1725. array('OPTIONS', true),
  1726. array('TRACE', true),
  1727. array('CONNECT', false),
  1728. );
  1729. }
  1730. /**
  1731. * @dataProvider methodSafeProvider
  1732. */
  1733. public function testMethodSafe($method, $safe)
  1734. {
  1735. $request = new Request();
  1736. $request->setMethod($method);
  1737. $this->assertEquals($safe, $request->isMethodSafe(false));
  1738. }
  1739. public function methodSafeProvider()
  1740. {
  1741. return array(
  1742. array('HEAD', true),
  1743. array('GET', true),
  1744. array('POST', false),
  1745. array('PUT', false),
  1746. array('PATCH', false),
  1747. array('DELETE', false),
  1748. array('PURGE', false),
  1749. array('OPTIONS', true),
  1750. array('TRACE', true),
  1751. array('CONNECT', false),
  1752. );
  1753. }
  1754. /**
  1755. * @group legacy
  1756. * @expectedDeprecation Checking only for cacheable HTTP methods with Symfony\Component\HttpFoundation\Request::isMethodSafe() is deprecated since Symfony 3.2 and will throw an exception in 4.0. Disable checking only for cacheable methods by calling the method with `false` as first argument or use the Request::isMethodCacheable() instead.
  1757. */
  1758. public function testMethodSafeChecksCacheable()
  1759. {
  1760. $request = new Request();
  1761. $request->setMethod('OPTIONS');
  1762. $this->assertFalse($request->isMethodSafe());
  1763. }
  1764. /**
  1765. * @dataProvider methodCacheableProvider
  1766. */
  1767. public function testMethodCacheable($method, $cacheable)
  1768. {
  1769. $request = new Request();
  1770. $request->setMethod($method);
  1771. $this->assertEquals($cacheable, $request->isMethodCacheable());
  1772. }
  1773. public function methodCacheableProvider()
  1774. {
  1775. return array(
  1776. array('HEAD', true),
  1777. array('GET', true),
  1778. array('POST', false),
  1779. array('PUT', false),
  1780. array('PATCH', false),
  1781. array('DELETE', false),
  1782. array('PURGE', false),
  1783. array('OPTIONS', false),
  1784. array('TRACE', false),
  1785. array('CONNECT', false),
  1786. );
  1787. }
  1788. /**
  1789. * @group legacy
  1790. */
  1791. public function testGetTrustedHeaderName()
  1792. {
  1793. Request::setTrustedProxies(array('8.8.8.8'), Request::HEADER_X_FORWARDED_ALL);
  1794. $this->assertNull(Request::getTrustedHeaderName(Request::HEADER_FORWARDED));
  1795. $this->assertSame('X_FORWARDED_FOR', Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP));
  1796. $this->assertSame('X_FORWARDED_HOST', Request::getTrustedHeaderName(Request::HEADER_CLIENT_HOST));
  1797. $this->assertSame('X_FORWARDED_PORT', Request::getTrustedHeaderName(Request::HEADER_CLIENT_PORT));
  1798. $this->assertSame('X_FORWARDED_PROTO', Request::getTrustedHeaderName(Request::HEADER_CLIENT_PROTO));
  1799. Request::setTrustedProxies(array('8.8.8.8'), Request::HEADER_FORWARDED);
  1800. $this->assertSame('FORWARDED', Request::getTrustedHeaderName(Request::HEADER_FORWARDED));
  1801. $this->assertNull(Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP));
  1802. $this->assertNull(Request::getTrustedHeaderName(Request::HEADER_CLIENT_HOST));
  1803. $this->assertNull(Request::getTrustedHeaderName(Request::HEADER_CLIENT_PORT));
  1804. $this->assertNull(Request::getTrustedHeaderName(Request::HEADER_CLIENT_PROTO));
  1805. Request::setTrustedHeaderName(Request::HEADER_FORWARDED, 'A');
  1806. Request::setTrustedHeaderName(Request::HEADER_CLIENT_IP, 'B');
  1807. Request::setTrustedHeaderName(Request::HEADER_CLIENT_HOST, 'C');
  1808. Request::setTrustedHeaderName(Request::HEADER_CLIENT_PORT, 'D');
  1809. Request::setTrustedHeaderName(Request::HEADER_CLIENT_PROTO, 'E');
  1810. Request::setTrustedProxies(array('8.8.8.8'), Request::HEADER_FORWARDED);
  1811. $this->assertSame('A', Request::getTrustedHeaderName(Request::HEADER_FORWARDED));
  1812. $this->assertNull(Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP));
  1813. $this->assertNull(Request::getTrustedHeaderName(Request::HEADER_CLIENT_HOST));
  1814. $this->assertNull(Request::getTrustedHeaderName(Request::HEADER_CLIENT_PORT));
  1815. $this->assertNull(Request::getTrustedHeaderName(Request::HEADER_CLIENT_PROTO));
  1816. Request::setTrustedProxies(array('8.8.8.8'), Request::HEADER_X_FORWARDED_ALL);
  1817. $this->assertNull(Request::getTrustedHeaderName(Request::HEADER_FORWARDED));
  1818. $this->assertSame('B', Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP));
  1819. $this->assertSame('C', Request::getTrustedHeaderName(Request::HEADER_CLIENT_HOST));
  1820. $this->assertSame('D', Request::getTrustedHeaderName(Request::HEADER_CLIENT_PORT));
  1821. $this->assertSame('E', Request::getTrustedHeaderName(Request::HEADER_CLIENT_PROTO));
  1822. Request::setTrustedProxies(array('8.8.8.8'), Request::HEADER_FORWARDED);
  1823. $this->assertSame('A', Request::getTrustedHeaderName(Request::HEADER_FORWARDED));
  1824. //reset
  1825. Request::setTrustedHeaderName(Request::HEADER_FORWARDED, 'FORWARDED');
  1826. Request::setTrustedHeaderName(Request::HEADER_CLIENT_IP, 'X_FORWARDED_FOR');
  1827. Request::setTrustedHeaderName(Request::HEADER_CLIENT_HOST, 'X_FORWARDED_HOST');
  1828. Request::setTrustedHeaderName(Request::HEADER_CLIENT_PORT, 'X_FORWARDED_PORT');
  1829. Request::setTrustedHeaderName(Request::HEADER_CLIENT_PROTO, 'X_FORWARDED_PROTO');
  1830. }
  1831. /**
  1832. * @dataProvider protocolVersionProvider
  1833. */
  1834. public function testProtocolVersion($serverProtocol, $trustedProxy, $via, $expected)
  1835. {
  1836. if ($trustedProxy) {
  1837. Request::setTrustedProxies(array('1.1.1.1'), -1);
  1838. }
  1839. $request = new Request();
  1840. $request->server->set('SERVER_PROTOCOL', $serverProtocol);
  1841. $request->server->set('REMOTE_ADDR', '1.1.1.1');
  1842. $request->headers->set('Via', $via);
  1843. $this->assertSame($expected, $request->getProtocolVersion());
  1844. }
  1845. public function protocolVersionProvider()
  1846. {
  1847. return array(
  1848. 'untrusted without via' => array('HTTP/2.0', false, '', 'HTTP/2.0'),
  1849. 'untrusted with via' => array('HTTP/2.0', false, '1.0 fred, 1.1 nowhere.com (Apache/1.1)', 'HTTP/2.0'),
  1850. 'trusted without via' => array('HTTP/2.0', true, '', 'HTTP/2.0'),
  1851. 'trusted with via' => array('HTTP/2.0', true, '1.0 fred, 1.1 nowhere.com (Apache/1.1)', 'HTTP/1.0'),
  1852. 'trusted with via and protocol name' => array('HTTP/2.0', true, 'HTTP/1.0 fred, HTTP/1.1 nowhere.com (Apache/1.1)', 'HTTP/1.0'),
  1853. 'trusted with broken via' => array('HTTP/2.0', true, 'HTTP/1^0 foo', 'HTTP/2.0'),
  1854. 'trusted with partially-broken via' => array('HTTP/2.0', true, '1.0 fred, foo', 'HTTP/1.0'),
  1855. );
  1856. }
  1857. public function nonstandardRequestsData()
  1858. {
  1859. return array(
  1860. array('', '', '/', 'http://host:8080/', ''),
  1861. array('/', '', '/', 'http://host:8080/', ''),
  1862. array('hello/app.php/x', '', '/x', 'http://host:8080/hello/app.php/x', '/hello', '/hello/app.php'),
  1863. array('/hello/app.php/x', '', '/x', 'http://host:8080/hello/app.php/x', '/hello', '/hello/app.php'),
  1864. array('', 'a=b', '/', 'http://host:8080/?a=b'),
  1865. array('?a=b', 'a=b', '/', 'http://host:8080/?a=b'),
  1866. array('/?a=b', 'a=b', '/', 'http://host:8080/?a=b'),
  1867. array('x', 'a=b', '/x', 'http://host:8080/x?a=b'),
  1868. array('x?a=b', 'a=b', '/x', 'http://host:8080/x?a=b'),
  1869. array('/x?a=b', 'a=b', '/x', 'http://host:8080/x?a=b'),
  1870. array('hello/x', '', '/x', 'http://host:8080/hello/x', '/hello'),
  1871. array('/hello/x', '', '/x', 'http://host:8080/hello/x', '/hello'),
  1872. array('hello/app.php/x', 'a=b', '/x', 'http://host:8080/hello/app.php/x?a=b', '/hello', '/hello/app.php'),
  1873. array('hello/app.php/x?a=b', 'a=b', '/x', 'http://host:8080/hello/app.php/x?a=b', '/hello', '/hello/app.php'),
  1874. array('/hello/app.php/x?a=b', 'a=b', '/x', 'http://host:8080/hello/app.php/x?a=b', '/hello', '/hello/app.php'),
  1875. );
  1876. }
  1877. /**
  1878. * @dataProvider nonstandardRequestsData
  1879. */
  1880. public function testNonstandardRequests($requestUri, $queryString, $expectedPathInfo, $expectedUri, $expectedBasePath = '', $expectedBaseUrl = null)
  1881. {
  1882. if (null === $expectedBaseUrl) {
  1883. $expectedBaseUrl = $expectedBasePath;
  1884. }
  1885. $server = array(
  1886. 'HTTP_HOST' => 'host:8080',
  1887. 'SERVER_PORT' => '8080',
  1888. 'QUERY_STRING' => $queryString,
  1889. 'PHP_SELF' => '/hello/app.php',
  1890. 'SCRIPT_FILENAME' => '/some/path/app.php',
  1891. 'REQUEST_URI' => $requestUri,
  1892. );
  1893. $request = new Request(array(), array(), array(), array(), array(), $server);
  1894. $this->assertEquals($expectedPathInfo, $request->getPathInfo());
  1895. $this->assertEquals($expectedUri, $request->getUri());
  1896. $this->assertEquals($queryString, $request->getQueryString());
  1897. $this->assertEquals(8080, $request->getPort());
  1898. $this->assertEquals('host:8080', $request->getHttpHost());
  1899. $this->assertEquals($expectedBaseUrl, $request->getBaseUrl());
  1900. $this->assertEquals($expectedBasePath, $request->getBasePath());
  1901. }
  1902. }
  1903. class RequestContentProxy extends Request
  1904. {
  1905. public function getContent($asResource = false)
  1906. {
  1907. return http_build_query(array('_method' => 'PUT', 'content' => 'mycontent'), '', '&');
  1908. }
  1909. }
  1910. class NewRequest extends Request
  1911. {
  1912. public function getFoo()
  1913. {
  1914. return 'foo';
  1915. }
  1916. }