RequestTest.php 89 KB

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