RequestTest.php 91KB

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