RequestTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Tests\Component\HttpFoundation;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\File\UploadedFile;
  13. class RequestTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @covers Symfony\Component\HttpFoundation\Request::__construct
  17. */
  18. public function testConstructor()
  19. {
  20. $this->testInitialize();
  21. }
  22. /**
  23. * @covers Symfony\Component\HttpFoundation\Request::initialize
  24. */
  25. public function testInitialize()
  26. {
  27. $request = new Request();
  28. $request->initialize(array('foo' => 'bar'));
  29. $this->assertEquals('bar', $request->query->get('foo'), '->initialize() takes an array of query parameters as its first argument');
  30. $request->initialize(array(), array('foo' => 'bar'));
  31. $this->assertEquals('bar', $request->request->get('foo'), '->initialize() takes an array of request parameters as its second argument');
  32. $request->initialize(array(), array(), array('foo' => 'bar'));
  33. $this->assertEquals('bar', $request->attributes->get('foo'), '->initialize() takes an array of attributes as its thrid argument');
  34. $request->initialize(array(), array(), array(), array(), array(), array('HTTP_FOO' => 'bar'));
  35. $this->assertEquals('bar', $request->headers->get('FOO'), '->initialize() takes an array of HTTP headers as its fourth argument');
  36. }
  37. /**
  38. * @covers Symfony\Component\HttpFoundation\Request::create
  39. */
  40. public function testCreate()
  41. {
  42. $request = Request::create('http://test.com/foo?bar=baz');
  43. $this->assertEquals('http://test.com/foo?bar=baz', $request->getUri());
  44. $this->assertEquals('/foo', $request->getPathInfo());
  45. $this->assertEquals('bar=baz', $request->getQueryString());
  46. $this->assertEquals(80, $request->getPort());
  47. $request = Request::create('https://test.com/foo?bar=baz');
  48. $this->assertEquals('https://test.com/foo?bar=baz', $request->getUri());
  49. $this->assertEquals('/foo', $request->getPathInfo());
  50. $this->assertEquals('bar=baz', $request->getQueryString());
  51. $this->assertEquals(443, $request->getPort());
  52. $request = Request::create('test.com:90/foo');
  53. $this->assertEquals('http://test.com:90/foo', $request->getUri());
  54. $this->assertEquals('/foo', $request->getPathInfo());
  55. $this->assertEquals('test.com', $request->getHost());
  56. $this->assertEquals(90, $request->getPort());
  57. $request = Request::create('https://test.com:90/foo');
  58. $this->assertEquals('https://test.com:90/foo', $request->getUri());
  59. $this->assertEquals('/foo', $request->getPathInfo());
  60. $this->assertEquals('test.com', $request->getHost());
  61. $this->assertEquals(90, $request->getPort());
  62. }
  63. /**
  64. * @covers Symfony\Component\HttpFoundation\Request::duplicate
  65. */
  66. public function testDuplicate()
  67. {
  68. $request = new Request(array('foo' => 'bar'), array('foo' => 'bar'), array('foo' => 'bar'), array(), array(), array('HTTP_FOO' => 'bar'));
  69. $dup = $request->duplicate();
  70. $this->assertEquals($request->query->all(), $dup->query->all(), '->duplicate() duplicates a request an copy the current query parameters');
  71. $this->assertEquals($request->request->all(), $dup->request->all(), '->duplicate() duplicates a request an copy the current request parameters');
  72. $this->assertEquals($request->attributes->all(), $dup->attributes->all(), '->duplicate() duplicates a request an copy the current attributes');
  73. $this->assertEquals($request->headers->all(), $dup->headers->all(), '->duplicate() duplicates a request an copy the current HTTP headers');
  74. $dup = $request->duplicate(array('foo' => 'foobar'), array('foo' => 'foobar'), array('foo' => 'foobar'), array(), array(), array('HTTP_FOO' => 'foobar'));
  75. $this->assertEquals(array('foo' => 'foobar'), $dup->query->all(), '->duplicate() overrides the query parameters if provided');
  76. $this->assertEquals(array('foo' => 'foobar'), $dup->request->all(), '->duplicate() overrides the request parameters if provided');
  77. $this->assertEquals(array('foo' => 'foobar'), $dup->attributes->all(), '->duplicate() overrides the attributes if provided');
  78. $this->assertEquals(array('foo' => array('foobar')), $dup->headers->all(), '->duplicate() overrides the HTTP header if provided');
  79. }
  80. /**
  81. * @covers Symfony\Component\HttpFoundation\Request::getFormat
  82. * @dataProvider getFormatToMimeTypeMapProvider
  83. */
  84. public function testGetFormatFromMimeType($format, $mimeTypes)
  85. {
  86. $request = new Request();
  87. foreach ($mimeTypes as $mime) {
  88. $this->assertEquals($format, $request->getFormat($mime));
  89. }
  90. }
  91. /**
  92. * @covers Symfony\Component\HttpFoundation\Request::getMimeType
  93. * @dataProvider getFormatToMimeTypeMapProvider
  94. */
  95. public function testGetMimeTypeFromFormat($format, $mimeTypes)
  96. {
  97. if (!is_null($format)) {
  98. $request = new Request();
  99. $this->assertEquals($mimeTypes[0], $request->getMimeType($format));
  100. }
  101. }
  102. public function getFormatToMimeTypeMapProvider()
  103. {
  104. return array(
  105. array(null, array(null, 'unexistant-mime-type')),
  106. array('txt', array('text/plain')),
  107. array('js', array('application/javascript', 'application/x-javascript', 'text/javascript')),
  108. array('css', array('text/css')),
  109. array('json', array('application/json', 'application/x-json')),
  110. array('xml', array('text/xml', 'application/xml', 'application/x-xml')),
  111. array('rdf', array('application/rdf+xml')),
  112. array('atom',array('application/atom+xml')),
  113. );
  114. }
  115. /**
  116. * @covers Symfony\Component\HttpFoundation\Request::getUri
  117. */
  118. public function testGetUri()
  119. {
  120. $server = array();
  121. // Standard Request on non default PORT
  122. // http://hostname:8080/index.php/path/info?query=string
  123. $server['HTTP_HOST'] = 'hostname:8080';
  124. $server['SERVER_NAME'] = 'hostname';
  125. $server['SERVER_PORT'] = '8080';
  126. $server['QUERY_STRING'] = 'query=string';
  127. $server['REQUEST_URI'] = '/index.php/path/info?query=string';
  128. $server['SCRIPT_NAME'] = '/index.php';
  129. $server['PATH_INFO'] = '/path/info';
  130. $server['PATH_TRANSLATED'] = 'redirect:/index.php/path/info';
  131. $server['PHP_SELF'] = '/index_dev.php/path/info';
  132. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  133. $request = new Request();
  134. $request->initialize(array(), array(), array(), array(), array(), $server);
  135. $this->assertEquals('http://hostname:8080/index.php/path/info?query=string', $request->getUri(), '->getUri() with non default port');
  136. // Use std port number
  137. $server['HTTP_HOST'] = 'hostname';
  138. $server['SERVER_NAME'] = 'hostname';
  139. $server['SERVER_PORT'] = '80';
  140. $request->initialize(array(), array(), array(), array(), array(), $server);
  141. $this->assertEquals('http://hostname/index.php/path/info?query=string', $request->getUri(), '->getUri() with default port');
  142. // Without HOST HEADER
  143. unset($server['HTTP_HOST']);
  144. $server['SERVER_NAME'] = 'hostname';
  145. $server['SERVER_PORT'] = '80';
  146. $request->initialize(array(), array(), array(), array(), array(), $server);
  147. $this->assertEquals('http://hostname/index.php/path/info?query=string', $request->getUri(), '->getUri() with default port without HOST_HEADER');
  148. // Request with URL REWRITING (hide index.php)
  149. // RewriteCond %{REQUEST_FILENAME} !-f
  150. // RewriteRule ^(.*)$ index.php [QSA,L]
  151. // http://hostname:8080/path/info?query=string
  152. $server = array();
  153. $server['HTTP_HOST'] = 'hostname:8080';
  154. $server['SERVER_NAME'] = 'hostname';
  155. $server['SERVER_PORT'] = '8080';
  156. $server['REDIRECT_QUERY_STRING'] = 'query=string';
  157. $server['REDIRECT_URL'] = '/path/info';
  158. $server['SCRIPT_NAME'] = '/index.php';
  159. $server['QUERY_STRING'] = 'query=string';
  160. $server['REQUEST_URI'] = '/path/info?toto=test&1=1';
  161. $server['SCRIPT_NAME'] = '/index.php';
  162. $server['PHP_SELF'] = '/index.php';
  163. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  164. $request->initialize(array(), array(), array(), array(), array(), $server);
  165. $this->assertEquals('http://hostname:8080/path/info?query=string', $request->getUri(), '->getUri() with rewrite');
  166. // Use std port number
  167. // http://hostname/path/info?query=string
  168. $server['HTTP_HOST'] = 'hostname';
  169. $server['SERVER_NAME'] = 'hostname';
  170. $server['SERVER_PORT'] = '80';
  171. $request->initialize(array(), array(), array(), array(), array(), $server);
  172. $this->assertEquals('http://hostname/path/info?query=string', $request->getUri(), '->getUri() with rewrite and default port');
  173. // Without HOST HEADER
  174. unset($server['HTTP_HOST']);
  175. $server['SERVER_NAME'] = 'hostname';
  176. $server['SERVER_PORT'] = '80';
  177. $request->initialize(array(), array(), array(), array(), array(), $server);
  178. $this->assertEquals('http://hostname/path/info?query=string', $request->getUri(), '->getUri() with rewrite, default port without HOST_HEADER');
  179. }
  180. /**
  181. * @covers Symfony\Component\HttpFoundation\Request::getUriForPath
  182. */
  183. public function testGetUriForPath()
  184. {
  185. $request = Request::create('http://test.com/foo?bar=baz');
  186. $this->assertEquals('http://test.com/some/path', $request->getUriForPath('/some/path'));
  187. $request = Request::create('http://test.com:90/foo?bar=baz');
  188. $this->assertEquals('http://test.com:90/some/path', $request->getUriForPath('/some/path'));
  189. $request = Request::create('https://test.com/foo?bar=baz');
  190. $this->assertEquals('https://test.com/some/path', $request->getUriForPath('/some/path'));
  191. $request = Request::create('https://test.com:90/foo?bar=baz');
  192. $this->assertEquals('https://test.com:90/some/path', $request->getUriForPath('/some/path'));
  193. $server = array();
  194. // Standard Request on non default PORT
  195. // http://hostname:8080/index.php/path/info?query=string
  196. $server['HTTP_HOST'] = 'hostname:8080';
  197. $server['SERVER_NAME'] = 'hostname';
  198. $server['SERVER_PORT'] = '8080';
  199. $server['QUERY_STRING'] = 'query=string';
  200. $server['REQUEST_URI'] = '/index.php/path/info?query=string';
  201. $server['SCRIPT_NAME'] = '/index.php';
  202. $server['PATH_INFO'] = '/path/info';
  203. $server['PATH_TRANSLATED'] = 'redirect:/index.php/path/info';
  204. $server['PHP_SELF'] = '/index_dev.php/path/info';
  205. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  206. $request = new Request();
  207. $request->initialize(array(), array(), array(), array(), array(),$server);
  208. $this->assertEquals('http://hostname:8080/index.php/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with non default port');
  209. // Use std port number
  210. $server['HTTP_HOST'] = 'hostname';
  211. $server['SERVER_NAME'] = 'hostname';
  212. $server['SERVER_PORT'] = '80';
  213. $request->initialize(array(), array(), array(), array(), array(), $server);
  214. $this->assertEquals('http://hostname/index.php/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with default port');
  215. // Without HOST HEADER
  216. unset($server['HTTP_HOST']);
  217. $server['SERVER_NAME'] = 'hostname';
  218. $server['SERVER_PORT'] = '80';
  219. $request->initialize(array(), array(), array(), array(), array(), $server);
  220. $this->assertEquals('http://hostname/index.php/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with default port without HOST_HEADER');
  221. // Request with URL REWRITING (hide index.php)
  222. // RewriteCond %{REQUEST_FILENAME} !-f
  223. // RewriteRule ^(.*)$ index.php [QSA,L]
  224. // http://hostname:8080/path/info?query=string
  225. $server = array();
  226. $server['HTTP_HOST'] = 'hostname:8080';
  227. $server['SERVER_NAME'] = 'hostname';
  228. $server['SERVER_PORT'] = '8080';
  229. $server['REDIRECT_QUERY_STRING'] = 'query=string';
  230. $server['REDIRECT_URL'] = '/path/info';
  231. $server['SCRIPT_NAME'] = '/index.php';
  232. $server['QUERY_STRING'] = 'query=string';
  233. $server['REQUEST_URI'] = '/path/info?toto=test&1=1';
  234. $server['SCRIPT_NAME'] = '/index.php';
  235. $server['PHP_SELF'] = '/index.php';
  236. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  237. $request->initialize(array(), array(), array(), array(), array(), $server);
  238. $this->assertEquals('http://hostname:8080/some/path', $request->getUriForPath('/some/path'), '->getUri() with rewrite');
  239. // Use std port number
  240. // http://hostname/path/info?query=string
  241. $server['HTTP_HOST'] = 'hostname';
  242. $server['SERVER_NAME'] = 'hostname';
  243. $server['SERVER_PORT'] = '80';
  244. $request->initialize(array(), array(), array(), array(), array(), $server);
  245. $this->assertEquals('http://hostname/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with rewrite and default port');
  246. // Without HOST HEADER
  247. unset($server['HTTP_HOST']);
  248. $server['SERVER_NAME'] = 'hostname';
  249. $server['SERVER_PORT'] = '80';
  250. $request->initialize(array(), array(), array(), array(), array(), $server);
  251. $this->assertEquals('http://hostname/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with rewrite, default port without HOST_HEADER');
  252. }
  253. /**
  254. * @covers Symfony\Component\HttpFoundation\Request::getQueryString
  255. */
  256. public function testGetQueryString()
  257. {
  258. $request = new Request();
  259. $request->server->set('QUERY_STRING', 'foo');
  260. $this->assertEquals('foo', $request->getQueryString(), '->getQueryString() works with valueless parameters');
  261. $request->server->set('QUERY_STRING', 'foo=');
  262. $this->assertEquals('foo=', $request->getQueryString(), '->getQueryString() includes a dangling equal sign');
  263. $request->server->set('QUERY_STRING', 'bar=&foo=bar');
  264. $this->assertEquals('bar=&foo=bar', $request->getQueryString(), '->getQueryString() works when empty parameters');
  265. $request->server->set('QUERY_STRING', 'foo=bar&bar=');
  266. $this->assertEquals('bar=&foo=bar', $request->getQueryString(), '->getQueryString() sorts keys alphabetically');
  267. $request->server->set('QUERY_STRING', 'him=John%20Doe&her=Jane+Doe');
  268. $this->assertEquals('her=Jane+Doe&him=John+Doe', $request->getQueryString(), '->getQueryString() normalizes encoding');
  269. $request->server->set('QUERY_STRING', 'foo[]=1&foo[]=2');
  270. $this->assertEquals('foo%5B%5D=1&foo%5B%5D=2', $request->getQueryString(), '->getQueryString() allows array notation');
  271. $request->server->set('QUERY_STRING', 'foo=1&foo=2');
  272. $this->assertEquals('foo=1&foo=2', $request->getQueryString(), '->getQueryString() allows repeated parameters');
  273. }
  274. /**
  275. * @covers Symfony\Component\HttpFoundation\Request::getHost
  276. */
  277. public function testGetHost()
  278. {
  279. $request = new Request();
  280. $request->initialize(array('foo' => 'bar'));
  281. $this->assertEquals('', $request->getHost(), '->getHost() return empty string if not initialized');
  282. $request->initialize(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'www.exemple.com'));
  283. $this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from Host Header');
  284. // Host header with port number.
  285. $request->initialize(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'www.exemple.com:8080'));
  286. $this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from Host Header with port number');
  287. // Server values.
  288. $request->initialize(array(), array(), array(), array(), array(), array('SERVER_NAME' => 'www.exemple.com'));
  289. $this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from server name');
  290. // X_FORWARDED_HOST.
  291. $request->initialize(array(), array(), array(), array(), array(), array('HTTP_X_FORWARDED_HOST' => 'www.exemple.com'));
  292. $this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from X_FORWARDED_HOST');
  293. // X_FORWARDED_HOST
  294. $request->initialize(array(), array(), array(), array(), array(), array('HTTP_X_FORWARDED_HOST' => 'www.exemple.com, www.second.com'));
  295. $this->assertEquals('www.second.com', $request->getHost(), '->getHost() value from X_FORWARDED_HOST use last value');
  296. // X_FORWARDED_HOST with port number
  297. $request->initialize(array(), array(), array(), array(), array(), array('HTTP_X_FORWARDED_HOST' => 'www.exemple.com, www.second.com:8080'));
  298. $this->assertEquals('www.second.com', $request->getHost(), '->getHost() value from X_FORWARDED_HOST with port number');
  299. $request->initialize(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'www.exemple.com', 'HTTP_X_FORWARDED_HOST' => 'www.forward.com'));
  300. $this->assertEquals('www.forward.com', $request->getHost(), '->getHost() value from X_FORWARDED_HOST has priority over Host');
  301. $request->initialize(array(), array(), array(), array(), array(), array('SERVER_NAME' => 'www.exemple.com', 'HTTP_X_FORWARDED_HOST' => 'www.forward.com'));
  302. $this->assertEquals('www.forward.com', $request->getHost(), '->getHost() value from X_FORWARDED_HOST has priority over SERVER_NAME ');
  303. $request->initialize(array(), array(), array(), array(), array(), array('SERVER_NAME' => 'www.exemple.com', 'HTTP_HOST' => 'www.host.com'));
  304. $this->assertEquals('www.host.com', $request->getHost(), '->getHost() value from Host header has priority over SERVER_NAME ');
  305. }
  306. /**
  307. * @covers Symfony\Component\HttpFoundation\Request::setMethod
  308. * @covers Symfony\Component\HttpFoundation\Request::getMethod
  309. */
  310. public function testGetSetMethod()
  311. {
  312. $request = new Request();
  313. $this->assertEquals('GET', $request->getMethod(), '->getMethod() returns GET if no method is defined');
  314. $request->setMethod('get');
  315. $this->assertEquals('GET', $request->getMethod(), '->getMethod() returns an uppercased string');
  316. $request->setMethod('PURGE');
  317. $this->assertEquals('PURGE', $request->getMethod(), '->getMethod() returns the method even if it is not a standard one');
  318. $request->setMethod('POST');
  319. $this->assertEquals('POST', $request->getMethod(), '->getMethod() returns the method POST if no _method is defined');
  320. $request->setMethod('POST');
  321. $request->request->set('_method', 'purge');
  322. $this->assertEquals('PURGE', $request->getMethod(), '->getMethod() returns the method from _method if defined and POST');
  323. }
  324. public function testGetContentWorksTwiceInDefaultMode()
  325. {
  326. $req = new Request;
  327. $this->assertEquals('', $req->getContent());
  328. $this->assertEquals('', $req->getContent());
  329. }
  330. public function testGetContentReturnsResource()
  331. {
  332. $req = new Request;
  333. $retval = $req->getContent(true);
  334. $this->assertInternalType('resource', $retval);
  335. $this->assertEquals("", fread($retval, 1));
  336. $this->assertTrue(feof($retval));
  337. }
  338. /**
  339. * @expectedException LogicException
  340. * @dataProvider getContentCantBeCalledTwiceWithResourcesProvider
  341. */
  342. public function testGetContentCantBeCalledTwiceWithResources($first, $second)
  343. {
  344. $req = new Request;
  345. $req->getContent($first);
  346. $req->getContent($second);
  347. }
  348. public function getContentCantBeCalledTwiceWithResourcesProvider()
  349. {
  350. return array(
  351. 'Resource then fetch' => array(true, false),
  352. 'Resource then resource' => array(true, true),
  353. 'Fetch then resource' => array(false, true),
  354. );
  355. }
  356. public function testCreateFromGlobals()
  357. {
  358. $_GET['foo1'] = 'bar1';
  359. $_POST['foo2'] = 'bar2';
  360. $_COOKIE['foo3'] = 'bar3';
  361. $_FILES['foo4'] = array('bar4');
  362. $_SERVER['foo5'] = 'bar5';
  363. $request = Request::createFromGlobals();
  364. $this->assertEquals('bar1', $request->query->get('foo1'), '::fromGlobals() uses values from $_GET');
  365. $this->assertEquals('bar2', $request->request->get('foo2'), '::fromGlobals() uses values from $_POST');
  366. $this->assertEquals('bar3', $request->cookies->get('foo3'), '::fromGlobals() uses values from $_COOKIE');
  367. $this->assertEquals(array('bar4'), $request->files->get('foo4'), '::fromGlobals() uses values from $_FILES');
  368. $this->assertEquals('bar5', $request->server->get('foo5'), '::fromGlobals() uses values from $_SERVER');
  369. unset($_GET['foo1'], $_POST['foo2'], $_COOKIE['foo3'], $_FILES['foo4'], $_SERVER['foo5']);
  370. }
  371. }