RequestTest.php 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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\Tests\Component\HttpFoundation;
  11. use Symfony\Component\HttpFoundation\HeaderBag;
  12. use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
  13. use Symfony\Component\HttpFoundation\Session;
  14. use Symfony\Component\HttpFoundation\Request;
  15. class RequestTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function setUp()
  18. {
  19. Request::trustProxyData();
  20. }
  21. /**
  22. * @covers Symfony\Component\HttpFoundation\Request::__construct
  23. */
  24. public function testConstructor()
  25. {
  26. $this->testInitialize();
  27. }
  28. /**
  29. * @covers Symfony\Component\HttpFoundation\Request::initialize
  30. */
  31. public function testInitialize()
  32. {
  33. $request = new Request();
  34. $request->initialize(array('foo' => 'bar'));
  35. $this->assertEquals('bar', $request->query->get('foo'), '->initialize() takes an array of query parameters as its first argument');
  36. $request->initialize(array(), array('foo' => 'bar'));
  37. $this->assertEquals('bar', $request->request->get('foo'), '->initialize() takes an array of request parameters as its second argument');
  38. $request->initialize(array(), array(), array('foo' => 'bar'));
  39. $this->assertEquals('bar', $request->attributes->get('foo'), '->initialize() takes an array of attributes as its third argument');
  40. $request->initialize(array(), array(), array(), array(), array(), array('HTTP_FOO' => 'bar'));
  41. $this->assertEquals('bar', $request->headers->get('FOO'), '->initialize() takes an array of HTTP headers as its fourth argument');
  42. }
  43. /**
  44. * @covers Symfony\Component\HttpFoundation\Request::create
  45. */
  46. public function testCreate()
  47. {
  48. $request = Request::create('http://test.com/foo?bar=baz');
  49. $this->assertEquals('http://test.com/foo?bar=baz', $request->getUri());
  50. $this->assertEquals('/foo', $request->getPathInfo());
  51. $this->assertEquals('bar=baz', $request->getQueryString());
  52. $this->assertEquals(80, $request->getPort());
  53. $this->assertEquals('test.com', $request->getHttpHost());
  54. $this->assertFalse($request->isSecure());
  55. $request = Request::create('https://test.com/foo?bar=baz');
  56. $this->assertEquals('https://test.com/foo?bar=baz', $request->getUri());
  57. $this->assertEquals('/foo', $request->getPathInfo());
  58. $this->assertEquals('bar=baz', $request->getQueryString());
  59. $this->assertEquals(443, $request->getPort());
  60. $this->assertEquals('test.com', $request->getHttpHost());
  61. $this->assertTrue($request->isSecure());
  62. $request = Request::create('test.com:90/foo');
  63. $this->assertEquals('http://test.com:90/foo', $request->getUri());
  64. $this->assertEquals('/foo', $request->getPathInfo());
  65. $this->assertEquals('test.com', $request->getHost());
  66. $this->assertEquals('test.com:90', $request->getHttpHost());
  67. $this->assertEquals(90, $request->getPort());
  68. $this->assertFalse($request->isSecure());
  69. $request = Request::create('https://test.com:90/foo');
  70. $this->assertEquals('https://test.com:90/foo', $request->getUri());
  71. $this->assertEquals('/foo', $request->getPathInfo());
  72. $this->assertEquals('test.com', $request->getHost());
  73. $this->assertEquals('test.com:90', $request->getHttpHost());
  74. $this->assertEquals(90, $request->getPort());
  75. $this->assertTrue($request->isSecure());
  76. $request = Request::create('https://127.0.0.1:90/foo');
  77. $this->assertEquals('https://127.0.0.1:90/foo', $request->getUri());
  78. $this->assertEquals('/foo', $request->getPathInfo());
  79. $this->assertEquals('127.0.0.1', $request->getHost());
  80. $this->assertEquals('127.0.0.1:90', $request->getHttpHost());
  81. $this->assertEquals(90, $request->getPort());
  82. $this->assertTrue($request->isSecure());
  83. $request = Request::create('https://[::1]:90/foo');
  84. $this->assertEquals('https://[::1]:90/foo', $request->getUri());
  85. $this->assertEquals('/foo', $request->getPathInfo());
  86. $this->assertEquals('[::1]', $request->getHost());
  87. $this->assertEquals('[::1]:90', $request->getHttpHost());
  88. $this->assertEquals(90, $request->getPort());
  89. $this->assertTrue($request->isSecure());
  90. $json = '{"jsonrpc":"2.0","method":"echo","id":7,"params":["Hello World"]}';
  91. $request = Request::create('http://example.com/jsonrpc', 'POST', array(), array(), array(), array(), $json);
  92. $this->assertEquals($json, $request->getContent());
  93. $this->assertFalse($request->isSecure());
  94. $request = Request::create('http://test.com');
  95. $this->assertEquals('http://test.com/', $request->getUri());
  96. $this->assertEquals('/', $request->getPathInfo());
  97. $this->assertEquals('', $request->getQueryString());
  98. $this->assertEquals(80, $request->getPort());
  99. $this->assertEquals('test.com', $request->getHttpHost());
  100. $this->assertFalse($request->isSecure());
  101. $request = Request::create('http://test.com:90/?test=1');
  102. $this->assertEquals('http://test.com:90/?test=1', $request->getUri());
  103. $this->assertEquals('/', $request->getPathInfo());
  104. $this->assertEquals('test=1', $request->getQueryString());
  105. $this->assertEquals(90, $request->getPort());
  106. $this->assertEquals('test.com:90', $request->getHttpHost());
  107. $this->assertFalse($request->isSecure());
  108. }
  109. /**
  110. * @covers Symfony\Component\HttpFoundation\Request::duplicate
  111. */
  112. public function testDuplicate()
  113. {
  114. $request = new Request(array('foo' => 'bar'), array('foo' => 'bar'), array('foo' => 'bar'), array(), array(), array('HTTP_FOO' => 'bar'));
  115. $dup = $request->duplicate();
  116. $this->assertEquals($request->query->all(), $dup->query->all(), '->duplicate() duplicates a request an copy the current query parameters');
  117. $this->assertEquals($request->request->all(), $dup->request->all(), '->duplicate() duplicates a request an copy the current request parameters');
  118. $this->assertEquals($request->attributes->all(), $dup->attributes->all(), '->duplicate() duplicates a request an copy the current attributes');
  119. $this->assertEquals($request->headers->all(), $dup->headers->all(), '->duplicate() duplicates a request an copy the current HTTP headers');
  120. $dup = $request->duplicate(array('foo' => 'foobar'), array('foo' => 'foobar'), array('foo' => 'foobar'), array(), array(), array('HTTP_FOO' => 'foobar'));
  121. $this->assertEquals(array('foo' => 'foobar'), $dup->query->all(), '->duplicate() overrides the query parameters if provided');
  122. $this->assertEquals(array('foo' => 'foobar'), $dup->request->all(), '->duplicate() overrides the request parameters if provided');
  123. $this->assertEquals(array('foo' => 'foobar'), $dup->attributes->all(), '->duplicate() overrides the attributes if provided');
  124. $this->assertEquals(array('foo' => array('foobar')), $dup->headers->all(), '->duplicate() overrides the HTTP header if provided');
  125. }
  126. /**
  127. * @covers Symfony\Component\HttpFoundation\Request::getFormat
  128. * @covers Symfony\Component\HttpFoundation\Request::setFormat
  129. * @dataProvider getFormatToMimeTypeMapProvider
  130. */
  131. public function testGetFormatFromMimeType($format, $mimeTypes)
  132. {
  133. $request = new Request();
  134. foreach ($mimeTypes as $mime) {
  135. $this->assertEquals($format, $request->getFormat($mime));
  136. }
  137. $request->setFormat($format, $mimeTypes);
  138. foreach ($mimeTypes as $mime) {
  139. $this->assertEquals($format, $request->getFormat($mime));
  140. }
  141. }
  142. /**
  143. * @covers Symfony\Component\HttpFoundation\Request::getFormat
  144. */
  145. public function testGetFormatFromMimeTypeWithParameters()
  146. {
  147. $request = new Request();
  148. $this->assertEquals('json', $request->getFormat('application/json; charset=utf-8'));
  149. }
  150. /**
  151. * @covers Symfony\Component\HttpFoundation\Request::getMimeType
  152. * @dataProvider getFormatToMimeTypeMapProvider
  153. */
  154. public function testGetMimeTypeFromFormat($format, $mimeTypes)
  155. {
  156. if (null !== $format) {
  157. $request = new Request();
  158. $this->assertEquals($mimeTypes[0], $request->getMimeType($format));
  159. }
  160. }
  161. public function getFormatToMimeTypeMapProvider()
  162. {
  163. return array(
  164. array(null, array(null, 'unexistent-mime-type')),
  165. array('txt', array('text/plain')),
  166. array('js', array('application/javascript', 'application/x-javascript', 'text/javascript')),
  167. array('css', array('text/css')),
  168. array('json', array('application/json', 'application/x-json')),
  169. array('xml', array('text/xml', 'application/xml', 'application/x-xml')),
  170. array('rdf', array('application/rdf+xml')),
  171. array('atom',array('application/atom+xml')),
  172. );
  173. }
  174. /**
  175. * @covers Symfony\Component\HttpFoundation\Request::getUri
  176. */
  177. public function testGetUri()
  178. {
  179. $server = array();
  180. // Standard Request on non default PORT
  181. // http://hostname:8080/index.php/path/info?query=string
  182. $server['HTTP_HOST'] = 'hostname:8080';
  183. $server['SERVER_NAME'] = 'servername';
  184. $server['SERVER_PORT'] = '8080';
  185. $server['QUERY_STRING'] = 'query=string';
  186. $server['REQUEST_URI'] = '/index.php/path/info?query=string';
  187. $server['SCRIPT_NAME'] = '/index.php';
  188. $server['PATH_INFO'] = '/path/info';
  189. $server['PATH_TRANSLATED'] = 'redirect:/index.php/path/info';
  190. $server['PHP_SELF'] = '/index_dev.php/path/info';
  191. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  192. $request = new Request();
  193. $request->initialize(array(), array(), array(), array(), array(), $server);
  194. $this->assertEquals('http://hostname:8080/index.php/path/info?query=string', $request->getUri(), '->getUri() with non default port');
  195. // Use std port number
  196. $server['HTTP_HOST'] = 'hostname';
  197. $server['SERVER_NAME'] = 'servername';
  198. $server['SERVER_PORT'] = '80';
  199. $request->initialize(array(), array(), array(), array(), array(), $server);
  200. $this->assertEquals('http://hostname/index.php/path/info?query=string', $request->getUri(), '->getUri() with default port');
  201. // Without HOST HEADER
  202. unset($server['HTTP_HOST']);
  203. $server['SERVER_NAME'] = 'servername';
  204. $server['SERVER_PORT'] = '80';
  205. $request->initialize(array(), array(), array(), array(), array(), $server);
  206. $this->assertEquals('http://servername/index.php/path/info?query=string', $request->getUri(), '->getUri() with default port without HOST_HEADER');
  207. // Request with URL REWRITING (hide index.php)
  208. // RewriteCond %{REQUEST_FILENAME} !-f
  209. // RewriteRule ^(.*)$ index.php [QSA,L]
  210. // http://hostname:8080/path/info?query=string
  211. $server = array();
  212. $server['HTTP_HOST'] = 'hostname:8080';
  213. $server['SERVER_NAME'] = 'servername';
  214. $server['SERVER_PORT'] = '8080';
  215. $server['REDIRECT_QUERY_STRING'] = 'query=string';
  216. $server['REDIRECT_URL'] = '/path/info';
  217. $server['SCRIPT_NAME'] = '/index.php';
  218. $server['QUERY_STRING'] = 'query=string';
  219. $server['REQUEST_URI'] = '/path/info?toto=test&1=1';
  220. $server['SCRIPT_NAME'] = '/index.php';
  221. $server['PHP_SELF'] = '/index.php';
  222. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  223. $request->initialize(array(), array(), array(), array(), array(), $server);
  224. $this->assertEquals('http://hostname:8080/path/info?query=string', $request->getUri(), '->getUri() with rewrite');
  225. // Use std port number
  226. // http://hostname/path/info?query=string
  227. $server['HTTP_HOST'] = 'hostname';
  228. $server['SERVER_NAME'] = 'servername';
  229. $server['SERVER_PORT'] = '80';
  230. $request->initialize(array(), array(), array(), array(), array(), $server);
  231. $this->assertEquals('http://hostname/path/info?query=string', $request->getUri(), '->getUri() with rewrite and default port');
  232. // Without HOST HEADER
  233. unset($server['HTTP_HOST']);
  234. $server['SERVER_NAME'] = 'servername';
  235. $server['SERVER_PORT'] = '80';
  236. $request->initialize(array(), array(), array(), array(), array(), $server);
  237. $this->assertEquals('http://servername/path/info?query=string', $request->getUri(), '->getUri() with rewrite, default port without HOST_HEADER');
  238. }
  239. /**
  240. * @covers Symfony\Component\HttpFoundation\Request::getUriForPath
  241. */
  242. public function testGetUriForPath()
  243. {
  244. $request = Request::create('http://test.com/foo?bar=baz');
  245. $this->assertEquals('http://test.com/some/path', $request->getUriForPath('/some/path'));
  246. $request = Request::create('http://test.com:90/foo?bar=baz');
  247. $this->assertEquals('http://test.com:90/some/path', $request->getUriForPath('/some/path'));
  248. $request = Request::create('https://test.com/foo?bar=baz');
  249. $this->assertEquals('https://test.com/some/path', $request->getUriForPath('/some/path'));
  250. $request = Request::create('https://test.com:90/foo?bar=baz');
  251. $this->assertEquals('https://test.com:90/some/path', $request->getUriForPath('/some/path'));
  252. $server = array();
  253. // Standard Request on non default PORT
  254. // http://hostname:8080/index.php/path/info?query=string
  255. $server['HTTP_HOST'] = 'hostname:8080';
  256. $server['SERVER_NAME'] = 'servername';
  257. $server['SERVER_PORT'] = '8080';
  258. $server['QUERY_STRING'] = 'query=string';
  259. $server['REQUEST_URI'] = '/index.php/path/info?query=string';
  260. $server['SCRIPT_NAME'] = '/index.php';
  261. $server['PATH_INFO'] = '/path/info';
  262. $server['PATH_TRANSLATED'] = 'redirect:/index.php/path/info';
  263. $server['PHP_SELF'] = '/index_dev.php/path/info';
  264. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  265. $request = new Request();
  266. $request->initialize(array(), array(), array(), array(), array(),$server);
  267. $this->assertEquals('http://hostname:8080/index.php/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with non default port');
  268. // Use std port number
  269. $server['HTTP_HOST'] = 'hostname';
  270. $server['SERVER_NAME'] = 'servername';
  271. $server['SERVER_PORT'] = '80';
  272. $request->initialize(array(), array(), array(), array(), array(), $server);
  273. $this->assertEquals('http://hostname/index.php/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with default port');
  274. // Without HOST HEADER
  275. unset($server['HTTP_HOST']);
  276. $server['SERVER_NAME'] = 'servername';
  277. $server['SERVER_PORT'] = '80';
  278. $request->initialize(array(), array(), array(), array(), array(), $server);
  279. $this->assertEquals('http://servername/index.php/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with default port without HOST_HEADER');
  280. // Request with URL REWRITING (hide index.php)
  281. // RewriteCond %{REQUEST_FILENAME} !-f
  282. // RewriteRule ^(.*)$ index.php [QSA,L]
  283. // http://hostname:8080/path/info?query=string
  284. $server = array();
  285. $server['HTTP_HOST'] = 'hostname:8080';
  286. $server['SERVER_NAME'] = 'servername';
  287. $server['SERVER_PORT'] = '8080';
  288. $server['REDIRECT_QUERY_STRING'] = 'query=string';
  289. $server['REDIRECT_URL'] = '/path/info';
  290. $server['SCRIPT_NAME'] = '/index.php';
  291. $server['QUERY_STRING'] = 'query=string';
  292. $server['REQUEST_URI'] = '/path/info?toto=test&1=1';
  293. $server['SCRIPT_NAME'] = '/index.php';
  294. $server['PHP_SELF'] = '/index.php';
  295. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  296. $request->initialize(array(), array(), array(), array(), array(), $server);
  297. $this->assertEquals('http://hostname:8080/some/path', $request->getUriForPath('/some/path'), '->getUri() with rewrite');
  298. // Use std port number
  299. // http://hostname/path/info?query=string
  300. $server['HTTP_HOST'] = 'hostname';
  301. $server['SERVER_NAME'] = 'servername';
  302. $server['SERVER_PORT'] = '80';
  303. $request->initialize(array(), array(), array(), array(), array(), $server);
  304. $this->assertEquals('http://hostname/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with rewrite and default port');
  305. // Without HOST HEADER
  306. unset($server['HTTP_HOST']);
  307. $server['SERVER_NAME'] = 'servername';
  308. $server['SERVER_PORT'] = '80';
  309. $request->initialize(array(), array(), array(), array(), array(), $server);
  310. $this->assertEquals('http://servername/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with rewrite, default port without HOST_HEADER');
  311. $this->assertEquals('servername', $request->getHttpHost());
  312. }
  313. /**
  314. * @covers Symfony\Component\HttpFoundation\Request::getQueryString
  315. */
  316. public function testGetQueryString()
  317. {
  318. $request = new Request();
  319. $request->server->set('QUERY_STRING', 'foo');
  320. $this->assertEquals('foo', $request->getQueryString(), '->getQueryString() works with valueless parameters');
  321. $request->server->set('QUERY_STRING', 'foo=');
  322. $this->assertEquals('foo=', $request->getQueryString(), '->getQueryString() includes a dangling equal sign');
  323. $request->server->set('QUERY_STRING', 'bar=&foo=bar');
  324. $this->assertEquals('bar=&foo=bar', $request->getQueryString(), '->getQueryString() works when empty parameters');
  325. $request->server->set('QUERY_STRING', 'foo=bar&bar=');
  326. $this->assertEquals('bar=&foo=bar', $request->getQueryString(), '->getQueryString() sorts keys alphabetically');
  327. $request->server->set('QUERY_STRING', 'him=John%20Doe&her=Jane+Doe');
  328. $this->assertEquals('her=Jane%2BDoe&him=John%20Doe', $request->getQueryString(), '->getQueryString() normalizes encoding');
  329. $request->server->set('QUERY_STRING', 'foo[]=1&foo[]=2');
  330. $this->assertEquals('foo%5B%5D=1&foo%5B%5D=2', $request->getQueryString(), '->getQueryString() allows array notation');
  331. $request->server->set('QUERY_STRING', 'foo=1&foo=2');
  332. $this->assertEquals('foo=1&foo=2', $request->getQueryString(), '->getQueryString() allows repeated parameters');
  333. }
  334. /**
  335. * @covers Symfony\Component\HttpFoundation\Request::getHost
  336. */
  337. public function testGetHost()
  338. {
  339. $request = new Request();
  340. $request->initialize(array('foo' => 'bar'));
  341. $this->assertEquals('', $request->getHost(), '->getHost() return empty string if not initialized');
  342. $request->initialize(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'www.exemple.com'));
  343. $this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from Host Header');
  344. // Host header with port number.
  345. $request->initialize(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'www.exemple.com:8080'));
  346. $this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from Host Header with port number');
  347. // Server values.
  348. $request->initialize(array(), array(), array(), array(), array(), array('SERVER_NAME' => 'www.exemple.com'));
  349. $this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from server name');
  350. // X_FORWARDED_HOST.
  351. $request->initialize(array(), array(), array(), array(), array(), array('HTTP_X_FORWARDED_HOST' => 'www.exemple.com'));
  352. $this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from X_FORWARDED_HOST');
  353. // X_FORWARDED_HOST
  354. $request->initialize(array(), array(), array(), array(), array(), array('HTTP_X_FORWARDED_HOST' => 'www.exemple.com, www.second.com'));
  355. $this->assertEquals('www.second.com', $request->getHost(), '->getHost() value from X_FORWARDED_HOST use last value');
  356. // X_FORWARDED_HOST with port number
  357. $request->initialize(array(), array(), array(), array(), array(), array('HTTP_X_FORWARDED_HOST' => 'www.exemple.com, www.second.com:8080'));
  358. $this->assertEquals('www.second.com', $request->getHost(), '->getHost() value from X_FORWARDED_HOST with port number');
  359. $request->initialize(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'www.exemple.com', 'HTTP_X_FORWARDED_HOST' => 'www.forward.com'));
  360. $this->assertEquals('www.forward.com', $request->getHost(), '->getHost() value from X_FORWARDED_HOST has priority over Host');
  361. $request->initialize(array(), array(), array(), array(), array(), array('SERVER_NAME' => 'www.exemple.com', 'HTTP_X_FORWARDED_HOST' => 'www.forward.com'));
  362. $this->assertEquals('www.forward.com', $request->getHost(), '->getHost() value from X_FORWARDED_HOST has priority over SERVER_NAME ');
  363. $request->initialize(array(), array(), array(), array(), array(), array('SERVER_NAME' => 'www.exemple.com', 'HTTP_HOST' => 'www.host.com'));
  364. $this->assertEquals('www.host.com', $request->getHost(), '->getHost() value from Host header has priority over SERVER_NAME ');
  365. }
  366. /**
  367. * @covers Symfony\Component\HttpFoundation\Request::setMethod
  368. * @covers Symfony\Component\HttpFoundation\Request::getMethod
  369. */
  370. public function testGetSetMethod()
  371. {
  372. $request = new Request();
  373. $this->assertEquals('GET', $request->getMethod(), '->getMethod() returns GET if no method is defined');
  374. $request->setMethod('get');
  375. $this->assertEquals('GET', $request->getMethod(), '->getMethod() returns an uppercased string');
  376. $request->setMethod('PURGE');
  377. $this->assertEquals('PURGE', $request->getMethod(), '->getMethod() returns the method even if it is not a standard one');
  378. $request->setMethod('POST');
  379. $this->assertEquals('POST', $request->getMethod(), '->getMethod() returns the method POST if no _method is defined');
  380. $request->setMethod('POST');
  381. $request->request->set('_method', 'purge');
  382. $this->assertEquals('PURGE', $request->getMethod(), '->getMethod() returns the method from _method if defined and POST');
  383. $request->setMethod('POST');
  384. $request->headers->set('X-HTTP-METHOD-OVERRIDE', 'delete');
  385. $this->assertEquals('DELETE', $request->getMethod(), '->getMethod() returns the method from X-HTTP-Method-Override even though _method is set if defined and POST');
  386. $request = new Request();
  387. $request->setMethod('POST');
  388. $request->headers->set('X-HTTP-METHOD-OVERRIDE', 'delete');
  389. $this->assertEquals('DELETE', $request->getMethod(), '->getMethod() returns the method from X-HTTP-Method-Override if defined and POST');
  390. }
  391. /**
  392. * @dataProvider testGetClientIpProvider
  393. */
  394. public function testGetClientIp($expected, $proxy, $remoteAddr, $httpClientIp, $httpForwardedFor)
  395. {
  396. $request = new Request;
  397. $this->assertEquals('', $request->getClientIp());
  398. $this->assertEquals('', $request->getClientIp(true));
  399. $server = array('REMOTE_ADDR' => $remoteAddr);
  400. if (null !== $httpClientIp) {
  401. $server['HTTP_CLIENT_IP'] = $httpClientIp;
  402. }
  403. if (null !== $httpForwardedFor) {
  404. $server['HTTP_X_FORWARDED_FOR'] = $httpForwardedFor;
  405. }
  406. $request->initialize(array(), array(), array(), array(), array(), $server);
  407. $this->assertEquals($expected, $request->getClientIp($proxy));
  408. }
  409. public function testGetClientIpProvider()
  410. {
  411. return array(
  412. array('88.88.88.88', false, '88.88.88.88', null, null),
  413. array('127.0.0.1', false, '127.0.0.1', '88.88.88.88', null),
  414. array('88.88.88.88', true, '127.0.0.1', '88.88.88.88', null),
  415. array('127.0.0.1', false, '127.0.0.1', null, '88.88.88.88'),
  416. array('88.88.88.88', true, '127.0.0.1', null, '88.88.88.88'),
  417. array('::1', false, '::1', null, null),
  418. array('2620:0:1cfe:face:b00c::3', true, '::1', '2620:0:1cfe:face:b00c::3', null),
  419. );
  420. }
  421. public function testGetContentWorksTwiceInDefaultMode()
  422. {
  423. $req = new Request;
  424. $this->assertEquals('', $req->getContent());
  425. $this->assertEquals('', $req->getContent());
  426. }
  427. public function testGetContentReturnsResource()
  428. {
  429. $req = new Request;
  430. $retval = $req->getContent(true);
  431. $this->assertInternalType('resource', $retval);
  432. $this->assertEquals("", fread($retval, 1));
  433. $this->assertTrue(feof($retval));
  434. }
  435. /**
  436. * @expectedException LogicException
  437. * @dataProvider getContentCantBeCalledTwiceWithResourcesProvider
  438. */
  439. public function testGetContentCantBeCalledTwiceWithResources($first, $second)
  440. {
  441. $req = new Request;
  442. $req->getContent($first);
  443. $req->getContent($second);
  444. }
  445. public function getContentCantBeCalledTwiceWithResourcesProvider()
  446. {
  447. return array(
  448. 'Resource then fetch' => array(true, false),
  449. 'Resource then resource' => array(true, true),
  450. 'Fetch then resource' => array(false, true),
  451. );
  452. }
  453. public function testCreateFromGlobals()
  454. {
  455. $_GET['foo1'] = 'bar1';
  456. $_POST['foo2'] = 'bar2';
  457. $_COOKIE['foo3'] = 'bar3';
  458. $_FILES['foo4'] = array('bar4');
  459. $_SERVER['foo5'] = 'bar5';
  460. $request = Request::createFromGlobals();
  461. $this->assertEquals('bar1', $request->query->get('foo1'), '::fromGlobals() uses values from $_GET');
  462. $this->assertEquals('bar2', $request->request->get('foo2'), '::fromGlobals() uses values from $_POST');
  463. $this->assertEquals('bar3', $request->cookies->get('foo3'), '::fromGlobals() uses values from $_COOKIE');
  464. $this->assertEquals(array('bar4'), $request->files->get('foo4'), '::fromGlobals() uses values from $_FILES');
  465. $this->assertEquals('bar5', $request->server->get('foo5'), '::fromGlobals() uses values from $_SERVER');
  466. unset($_GET['foo1'], $_POST['foo2'], $_COOKIE['foo3'], $_FILES['foo4'], $_SERVER['foo5']);
  467. $_SERVER['REQUEST_METHOD'] = 'PUT';
  468. $_SERVER['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
  469. $request = RequestContentProxy::createFromGlobals();
  470. $this->assertEquals('PUT', $request->getMethod());
  471. $this->assertEquals('mycontent', $request->request->get('content'));
  472. unset($_SERVER['REQUEST_METHOD'], $_SERVER['CONTENT_TYPE']);
  473. $_POST['_method'] = 'PUT';
  474. $_POST['foo6'] = 'bar6';
  475. $_SERVER['REQUEST_METHOD'] = 'POST';
  476. $request = Request::createFromGlobals();
  477. $this->assertEquals('PUT', $request->getMethod());
  478. $this->assertEquals('bar6', $request->request->get('foo6'));
  479. unset($_POST['_method'], $_POST['foo6'], $_SERVER['REQUEST_METHOD']);
  480. }
  481. public function testOverrideGlobals()
  482. {
  483. $request = new Request();
  484. $request->initialize(array('foo' => 'bar'));
  485. // as the Request::overrideGlobals really work, it erase $_SERVER, so we must backup it
  486. $server = $_SERVER;
  487. $request->overrideGlobals();
  488. $this->assertEquals(array('foo' => 'bar'), $_GET);
  489. $request->initialize(array(), array('foo' => 'bar'));
  490. $request->overrideGlobals();
  491. $this->assertEquals(array('foo' => 'bar'), $_POST);
  492. $this->assertArrayNotHasKey('HTTP_X_FORWARDED_PROTO', $_SERVER);
  493. $request->headers->set('X_FORWARDED_PROTO', 'https');
  494. $this->assertTrue($request->isSecure());
  495. $request->overrideGlobals();
  496. $this->assertArrayHasKey('HTTP_X_FORWARDED_PROTO', $_SERVER);
  497. // restore initial $_SERVER array
  498. $_SERVER = $server;
  499. }
  500. public function testGetScriptName()
  501. {
  502. $request = new Request();
  503. $this->assertEquals('', $request->getScriptName());
  504. $server = array();
  505. $server['SCRIPT_NAME'] = '/index.php';
  506. $request->initialize(array(), array(), array(), array(), array(), $server);
  507. $this->assertEquals('/index.php', $request->getScriptName());
  508. $server = array();
  509. $server['ORIG_SCRIPT_NAME'] = '/frontend.php';
  510. $request->initialize(array(), array(), array(), array(), array(), $server);
  511. $this->assertEquals('/frontend.php', $request->getScriptName());
  512. $server = array();
  513. $server['SCRIPT_NAME'] = '/index.php';
  514. $server['ORIG_SCRIPT_NAME'] = '/frontend.php';
  515. $request->initialize(array(), array(), array(), array(), array(), $server);
  516. $this->assertEquals('/index.php', $request->getScriptName());
  517. }
  518. public function testGetBasePath()
  519. {
  520. $request = new Request();
  521. $this->assertEquals('', $request->getBasePath());
  522. $server = array();
  523. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  524. $request->initialize(array(), array(), array(), array(), array(), $server);
  525. $this->assertEquals('', $request->getBasePath());
  526. $server = array();
  527. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  528. $server['SCRIPT_NAME'] = '/index.php';
  529. $request->initialize(array(), array(), array(), array(), array(), $server);
  530. $this->assertEquals('', $request->getBasePath());
  531. $server = array();
  532. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  533. $server['PHP_SELF'] = '/index.php';
  534. $request->initialize(array(), array(), array(), array(), array(), $server);
  535. $this->assertEquals('', $request->getBasePath());
  536. $server = array();
  537. $server['SCRIPT_FILENAME'] = '/some/where/index.php';
  538. $server['ORIG_SCRIPT_NAME'] = '/index.php';
  539. $request->initialize(array(), array(), array(), array(), array(), $server);
  540. $this->assertEquals('', $request->getBasePath());
  541. }
  542. public function testGetPreferredLanguage()
  543. {
  544. $request = new Request();
  545. $this->assertEquals('', $request->getPreferredLanguage());
  546. $this->assertEquals('fr', $request->getPreferredLanguage(array('fr')));
  547. $this->assertEquals('fr', $request->getPreferredLanguage(array('fr', 'en')));
  548. $this->assertEquals('en', $request->getPreferredLanguage(array('en', 'fr')));
  549. $this->assertEquals('fr-ch', $request->getPreferredLanguage(array('fr-ch', 'fr-fr')));
  550. $request = new Request();
  551. $request->headers->set('Accept-language', 'zh, en-us; q=0.8, en; q=0.6');
  552. $this->assertEquals('en', $request->getPreferredLanguage(array('en', 'en-us')));
  553. $request = new Request();
  554. $request->headers->set('Accept-language', 'zh, en-us; q=0.8, en; q=0.6');
  555. $this->assertEquals('en', $request->getPreferredLanguage(array('fr', 'en')));
  556. }
  557. public function testIsXmlHttpRequest()
  558. {
  559. $request = new Request();
  560. $this->assertFalse($request->isXmlHttpRequest());
  561. $request->headers->set('X-Requested-With', 'XMLHttpRequest');
  562. $this->assertTrue($request->isXmlHttpRequest());
  563. $request->headers->remove('X-Requested-With');
  564. $this->assertFalse($request->isXmlHttpRequest());
  565. }
  566. public function testGetCharsets()
  567. {
  568. $request = new Request();
  569. $this->assertEquals(array(), $request->getCharsets());
  570. $request->headers->set('Accept-Charset', 'ISO-8859-1, US-ASCII, UTF-8; q=0.8, ISO-10646-UCS-2; q=0.6');
  571. $this->assertEquals(array(), $request->getCharsets()); // testing caching
  572. $request = new Request();
  573. $request->headers->set('Accept-Charset', 'ISO-8859-1, US-ASCII, UTF-8; q=0.8, ISO-10646-UCS-2; q=0.6');
  574. $this->assertEquals(array('ISO-8859-1', 'US-ASCII', 'UTF-8', 'ISO-10646-UCS-2'), $request->getCharsets());
  575. $request = new Request();
  576. $request->headers->set('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7');
  577. $this->assertEquals(array('ISO-8859-1', '*', 'utf-8'), $request->getCharsets());
  578. }
  579. public function testGetAcceptableContentTypes()
  580. {
  581. $request = new Request();
  582. $this->assertEquals(array(), $request->getAcceptableContentTypes());
  583. $request->headers->set('Accept', 'application/vnd.wap.wmlscriptc, text/vnd.wap.wml, application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html, multipart/mixed, */*');
  584. $this->assertEquals(array(), $request->getAcceptableContentTypes()); // testing caching
  585. $request = new Request();
  586. $request->headers->set('Accept', 'application/vnd.wap.wmlscriptc, text/vnd.wap.wml, application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html, multipart/mixed, */*');
  587. $this->assertEquals(array('multipart/mixed', '*/*', 'text/html', 'application/xhtml+xml', 'text/vnd.wap.wml', 'application/vnd.wap.xhtml+xml', 'application/vnd.wap.wmlscriptc'), $request->getAcceptableContentTypes());
  588. }
  589. public function testGetLanguages()
  590. {
  591. $request = new Request();
  592. $this->assertEquals(array(), $request->getLanguages());
  593. $request = new Request();
  594. $request->headers->set('Accept-language', 'zh, en-us; q=0.8, en; q=0.6');
  595. $this->assertEquals(array('zh', 'en_US', 'en'), $request->getLanguages());
  596. $this->assertEquals(array('zh', 'en_US', 'en'), $request->getLanguages());
  597. $request = new Request();
  598. $request->headers->set('Accept-language', 'zh, i-cherokee; q=0.6');
  599. $this->assertEquals(array('zh', 'cherokee'), $request->getLanguages());
  600. }
  601. public function testGetRequestFormat()
  602. {
  603. $request = new Request();
  604. $this->assertEquals('html', $request->getRequestFormat());
  605. $request = new Request();
  606. $this->assertEquals(null, $request->getRequestFormat(null));
  607. $request = new Request();
  608. $this->assertEquals(null, $request->setRequestFormat('foo'));
  609. $this->assertEquals('foo', $request->getRequestFormat(null));
  610. }
  611. public function testForwardedSecure()
  612. {
  613. $request = new Request();
  614. $request->headers->set('X-Forwarded-Proto', 'https');
  615. $request->headers->set('X-Forwarded-Port', 443);
  616. $this->assertTrue($request->isSecure());
  617. $this->assertEquals(443, $request->getPort());
  618. }
  619. public function testHasSession()
  620. {
  621. $request = new Request;
  622. $this->assertFalse($request->hasSession());
  623. $request->setSession(new Session(new ArraySessionStorage()));
  624. $this->assertTrue($request->hasSession());
  625. }
  626. public function testHasPreviousSession()
  627. {
  628. $request = new Request;
  629. $this->assertFalse($request->hasPreviousSession());
  630. $request->cookies->set(session_name(), 'foo');
  631. $this->assertFalse($request->hasPreviousSession());
  632. $request->setSession(new Session(new ArraySessionStorage()));
  633. $this->assertTrue($request->hasPreviousSession());
  634. }
  635. public function testToString()
  636. {
  637. $request = new Request();
  638. $request->headers->set('Accept-language', 'zh, en-us; q=0.8, en; q=0.6');
  639. $this->assertContains('Accept-Language: zh, en-us; q=0.8, en; q=0.6', $request->__toString());
  640. }
  641. }
  642. class RequestContentProxy extends Request
  643. {
  644. public function getContent($asResource = false)
  645. {
  646. return http_build_query(array('_method' => 'PUT', 'content' => 'mycontent'));
  647. }
  648. }