RequestTest.php 82 KB

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