No Description

RequestTest.php 72KB

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