RequestTest.php 92 KB

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