RequestTest.php 67 KB

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