14 use PHPUnit\Framework\TestCase;
32 $request->initialize(array(
'foo' =>
'bar'));
33 $this->assertEquals(
'bar', $request->query->get(
'foo'),
'->initialize() takes an array of query parameters as its first argument');
35 $request->initialize(array(), array(
'foo' =>
'bar'));
36 $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');
41 $request->initialize(array(), array(), array(), array(), array(), array(
'HTTP_FOO' =>
'bar'));
42 $this->assertEquals(
'bar', $request->headers->get(
'FOO'),
'->initialize() takes an array of HTTP headers as its sixth argument');
48 $request->setLocale(
'pl');
49 $locale = $request->getLocale();
50 $this->assertEquals(
'pl', $locale);
56 $user = $request->getUser();
58 $this->assertEquals(
'user_test', $user);
64 $password = $request->getPassword();
66 $this->assertEquals(
'password_test', $password);
72 $isNoCache = $request->isNoCache();
74 $this->assertFalse($isNoCache);
80 $contentType = $request->getContentType();
82 $this->assertNull($contentType);
88 $request->setDefaultLocale(
'pl');
89 $locale = $request->getLocale();
91 $this->assertEquals(
'pl', $locale);
97 $this->assertEquals(
'http://test.com/foo?bar=baz', $request->getUri());
98 $this->assertEquals(
'/foo', $request->getPathInfo());
99 $this->assertEquals(
'bar=baz', $request->getQueryString());
100 $this->assertEquals(80, $request->getPort());
101 $this->assertEquals(
'test.com', $request->getHttpHost());
102 $this->assertFalse($request->isSecure());
104 $request =
Request::create(
'http://test.com/foo',
'GET', array(
'bar' =>
'baz'));
105 $this->assertEquals(
'http://test.com/foo?bar=baz', $request->getUri());
106 $this->assertEquals(
'/foo', $request->getPathInfo());
107 $this->assertEquals(
'bar=baz', $request->getQueryString());
108 $this->assertEquals(80, $request->getPort());
109 $this->assertEquals(
'test.com', $request->getHttpHost());
110 $this->assertFalse($request->isSecure());
112 $request =
Request::create(
'http://test.com/foo?bar=foo',
'GET', array(
'bar' =>
'baz'));
113 $this->assertEquals(
'http://test.com/foo?bar=baz', $request->getUri());
114 $this->assertEquals(
'/foo', $request->getPathInfo());
115 $this->assertEquals(
'bar=baz', $request->getQueryString());
116 $this->assertEquals(80, $request->getPort());
117 $this->assertEquals(
'test.com', $request->getHttpHost());
118 $this->assertFalse($request->isSecure());
121 $this->assertEquals(
'https://test.com/foo?bar=baz', $request->getUri());
122 $this->assertEquals(
'/foo', $request->getPathInfo());
123 $this->assertEquals(
'bar=baz', $request->getQueryString());
124 $this->assertEquals(443, $request->getPort());
125 $this->assertEquals(
'test.com', $request->getHttpHost());
126 $this->assertTrue($request->isSecure());
129 $this->assertEquals(
'http://test.com:90/foo', $request->getUri());
130 $this->assertEquals(
'/foo', $request->getPathInfo());
131 $this->assertEquals(
'test.com', $request->getHost());
132 $this->assertEquals(
'test.com:90', $request->getHttpHost());
133 $this->assertEquals(90, $request->getPort());
134 $this->assertFalse($request->isSecure());
137 $this->assertEquals(
'https://test.com:90/foo', $request->getUri());
138 $this->assertEquals(
'/foo', $request->getPathInfo());
139 $this->assertEquals(
'test.com', $request->getHost());
140 $this->assertEquals(
'test.com:90', $request->getHttpHost());
141 $this->assertEquals(90, $request->getPort());
142 $this->assertTrue($request->isSecure());
145 $this->assertEquals(
'https://127.0.0.1:90/foo', $request->getUri());
146 $this->assertEquals(
'/foo', $request->getPathInfo());
147 $this->assertEquals(
'127.0.0.1', $request->getHost());
148 $this->assertEquals(
'127.0.0.1:90', $request->getHttpHost());
149 $this->assertEquals(90, $request->getPort());
150 $this->assertTrue($request->isSecure());
153 $this->assertEquals(
'https://[::1]:90/foo', $request->getUri());
154 $this->assertEquals(
'/foo', $request->getPathInfo());
155 $this->assertEquals(
'[::1]', $request->getHost());
156 $this->assertEquals(
'[::1]:90', $request->getHttpHost());
157 $this->assertEquals(90, $request->getPort());
158 $this->assertTrue($request->isSecure());
161 $this->assertEquals(
'https://[::1]/foo', $request->getUri());
162 $this->assertEquals(
'/foo', $request->getPathInfo());
163 $this->assertEquals(
'[::1]', $request->getHost());
164 $this->assertEquals(
'[::1]', $request->getHttpHost());
165 $this->assertEquals(443, $request->getPort());
166 $this->assertTrue($request->isSecure());
168 $json =
'{"jsonrpc":"2.0","method":"echo","id":7,"params":["Hello World"]}';
169 $request =
Request::create(
'http://example.com/jsonrpc',
'POST', array(), array(), array(), array(), $json);
170 $this->assertEquals($json, $request->getContent());
171 $this->assertFalse($request->isSecure());
174 $this->assertEquals(
'http://test.com/', $request->getUri());
175 $this->assertEquals(
'/', $request->getPathInfo());
176 $this->assertEquals(
'', $request->getQueryString());
177 $this->assertEquals(80, $request->getPort());
178 $this->assertEquals(
'test.com', $request->getHttpHost());
179 $this->assertFalse($request->isSecure());
182 $this->assertEquals(
'http://test.com/?test=1', $request->getUri());
183 $this->assertEquals(
'/', $request->getPathInfo());
184 $this->assertEquals(
'test=1', $request->getQueryString());
185 $this->assertEquals(80, $request->getPort());
186 $this->assertEquals(
'test.com', $request->getHttpHost());
187 $this->assertFalse($request->isSecure());
190 $this->assertEquals(
'http://test.com:90/?test=1', $request->getUri());
191 $this->assertEquals(
'/', $request->getPathInfo());
192 $this->assertEquals(
'test=1', $request->getQueryString());
193 $this->assertEquals(90, $request->getPort());
194 $this->assertEquals(
'test.com:90', $request->getHttpHost());
195 $this->assertFalse($request->isSecure());
198 $this->assertEquals(
'http://test.com/', $request->getUri());
199 $this->assertEquals(
'/', $request->getPathInfo());
200 $this->assertEquals(
'', $request->getQueryString());
201 $this->assertEquals(80, $request->getPort());
202 $this->assertEquals(
'test.com', $request->getHttpHost());
203 $this->assertEquals(
'username', $request->getUser());
204 $this->assertEquals(
'password', $request->getPassword());
205 $this->assertFalse($request->isSecure());
208 $this->assertEquals(
'http://test.com/', $request->getUri());
209 $this->assertEquals(
'/', $request->getPathInfo());
210 $this->assertEquals(
'', $request->getQueryString());
211 $this->assertEquals(80, $request->getPort());
212 $this->assertEquals(
'test.com', $request->getHttpHost());
213 $this->assertEquals(
'username', $request->getUser());
214 $this->assertSame(
'', $request->getPassword());
215 $this->assertFalse($request->isSecure());
218 $this->assertEquals(
'/?foo', $request->getRequestUri());
219 $this->assertEquals(array(
'foo' =>
''), $request->query->all());
222 $request =
Request::create(
'http://test.com/apparthotel-1234',
'GET', array(), array(), array(),
224 'DOCUMENT_ROOT' =>
'/var/www/www.test.com',
225 'SCRIPT_FILENAME' =>
'/var/www/www.test.com/app/app.php',
226 'SCRIPT_NAME' =>
'/app/app.php',
227 'PHP_SELF' =>
'/app/app.php/apparthotel-1234',
229 $this->assertEquals(
'http://test.com/apparthotel-1234', $request->getUri());
230 $this->assertEquals(
'/apparthotel-1234', $request->getPathInfo());
231 $this->assertEquals(
'', $request->getQueryString());
232 $this->assertEquals(80, $request->getPort());
233 $this->assertEquals(
'test.com', $request->getHttpHost());
234 $this->assertFalse($request->isSecure());
240 $request =
Request::create(
'/',
'DELETE', array(), array(), array(), array(
241 'HTTP_HOST' =>
'example.com',
243 'SERVER_PORT' => 443,
244 'PHP_AUTH_USER' =>
'fabien',
245 'PHP_AUTH_PW' =>
'pa$$',
246 'QUERY_STRING' =>
'foo=bar',
247 'CONTENT_TYPE' =>
'application/json',
249 $this->assertEquals(
'example.com', $request->getHost());
250 $this->assertEquals(443, $request->getPort());
251 $this->assertTrue($request->isSecure());
252 $this->assertEquals(
'fabien', $request->getUser());
253 $this->assertEquals(
'pa$$', $request->getPassword());
254 $this->assertEquals(
'', $request->getQueryString());
255 $this->assertEquals(
'application/json', $request->headers->get(
'CONTENT_TYPE'));
258 $request =
Request::create(
'http://thomas:pokemon@example.net:8080/?foo=bar',
'GET', array(), array(), array(), array(
259 'HTTP_HOST' =>
'example.com',
261 'SERVER_PORT' => 443,
263 $this->assertEquals(
'example.net', $request->getHost());
264 $this->assertEquals(8080, $request->getPort());
265 $this->assertFalse($request->isSecure());
266 $this->assertEquals(
'thomas', $request->getUser());
267 $this->assertEquals(
'pokemon', $request->getPassword());
268 $this->assertEquals(
'foo=bar', $request->getQueryString());
273 $request =
new Request(array(
'foo' =>
'bar'), array(
'foo' =>
'bar'), array(
'foo' =>
'bar'), array(), array(), array(
'HTTP_FOO' =>
'bar'));
274 $dup = $request->duplicate();
276 $this->assertEquals($request->query->all(), $dup->query->all(),
'->duplicate() duplicates a request an copy the current query parameters');
277 $this->assertEquals($request->request->all(), $dup->request->all(),
'->duplicate() duplicates a request an copy the current request parameters');
278 $this->assertEquals($request->attributes->all(), $dup->attributes->all(),
'->duplicate() duplicates a request an copy the current attributes');
279 $this->assertEquals($request->headers->all(), $dup->headers->all(),
'->duplicate() duplicates a request an copy the current HTTP headers');
281 $dup = $request->duplicate(array(
'foo' =>
'foobar'), array(
'foo' =>
'foobar'), array(
'foo' =>
'foobar'), array(), array(), array(
'HTTP_FOO' =>
'foobar'));
283 $this->assertEquals(array(
'foo' =>
'foobar'), $dup->query->all(),
'->duplicate() overrides the query parameters if provided');
284 $this->assertEquals(array(
'foo' =>
'foobar'), $dup->request->all(),
'->duplicate() overrides the request parameters if provided');
285 $this->assertEquals(array(
'foo' =>
'foobar'), $dup->attributes->all(),
'->duplicate() overrides the attributes if provided');
286 $this->assertEquals(array(
'foo' => array(
'foobar')), $dup->headers->all(),
'->duplicate() overrides the HTTP header if provided');
291 $request =
new Request(array(), array(), array(
'_format' =>
'json'));
292 $dup = $request->duplicate();
294 $this->assertEquals(
'json', $dup->getRequestFormat());
295 $this->assertEquals(
'json', $dup->attributes->get(
'_format'));
298 $request->setRequestFormat(
'xml');
299 $dup = $request->duplicate();
301 $this->assertEquals(
'xml', $dup->getRequestFormat());
310 foreach ($mimeTypes as $mime) {
311 $this->assertEquals($format, $request->getFormat($mime));
313 $request->setFormat($format, $mimeTypes);
314 foreach ($mimeTypes as $mime) {
315 $this->assertEquals($format, $request->getFormat($mime));
317 if (
null !== $format) {
318 $this->assertEquals($mimeTypes[0], $request->getMimeType($format));
326 array(array(
null, array(
null,
'unexistent-mime-type'))),
334 $this->assertEquals(
'json', $request->getFormat(
'application/json; charset=utf-8'));
343 $this->assertEquals($mimeTypes[0], $request->getMimeType($format));
357 $this->assertNull($request->getMimeType(
'foo'));
364 $request->setFormat(
'custom',
'application/vnd.foo.api;myversion=2.3');
365 $this->assertEquals(
'custom', $request->getFormat(
'application/vnd.foo.api;myversion=2.3'));
371 array(
'txt', array(
'text/plain')),
372 array(
'js', array(
'application/javascript',
'application/x-javascript',
'text/javascript')),
373 array(
'css', array(
'text/css')),
374 array(
'json', array(
'application/json',
'application/x-json')),
375 array(
'xml', array(
'text/xml',
'application/xml',
'application/x-xml')),
376 array(
'rdf', array(
'application/rdf+xml')),
377 array(
'atom', array(
'application/atom+xml')),
388 $server[
'HTTP_HOST'] =
'host:8080';
389 $server[
'SERVER_NAME'] =
'servername';
390 $server[
'SERVER_PORT'] =
'8080';
392 $server[
'QUERY_STRING'] =
'query=string';
393 $server[
'REQUEST_URI'] =
'/index.php/path/info?query=string';
394 $server[
'SCRIPT_NAME'] =
'/index.php';
395 $server[
'PATH_INFO'] =
'/path/info';
396 $server[
'PATH_TRANSLATED'] =
'redirect:/index.php/path/info';
397 $server[
'PHP_SELF'] =
'/index_dev.php/path/info';
398 $server[
'SCRIPT_FILENAME'] =
'/some/where/index.php';
402 $request->initialize(array(), array(), array(), array(), array(), $server);
404 $this->assertEquals(
'http://host:8080/index.php/path/info?query=string', $request->getUri(),
'->getUri() with non default port');
407 $server[
'HTTP_HOST'] =
'host';
408 $server[
'SERVER_NAME'] =
'servername';
409 $server[
'SERVER_PORT'] =
'80';
411 $request->initialize(array(), array(), array(), array(), array(), $server);
413 $this->assertEquals(
'http://host/index.php/path/info?query=string', $request->getUri(),
'->getUri() with default port');
416 unset($server[
'HTTP_HOST']);
417 $server[
'SERVER_NAME'] =
'servername';
418 $server[
'SERVER_PORT'] =
'80';
420 $request->initialize(array(), array(), array(), array(), array(), $server);
422 $this->assertEquals(
'http://servername/index.php/path/info?query=string', $request->getUri(),
'->getUri() with default port without HOST_HEADER');
429 $server[
'HTTP_HOST'] =
'host:8080';
430 $server[
'SERVER_NAME'] =
'servername';
431 $server[
'SERVER_PORT'] =
'8080';
433 $server[
'REDIRECT_QUERY_STRING'] =
'query=string';
434 $server[
'REDIRECT_URL'] =
'/path/info';
435 $server[
'SCRIPT_NAME'] =
'/index.php';
436 $server[
'QUERY_STRING'] =
'query=string';
437 $server[
'REQUEST_URI'] =
'/path/info?toto=test&1=1';
438 $server[
'SCRIPT_NAME'] =
'/index.php';
439 $server[
'PHP_SELF'] =
'/index.php';
440 $server[
'SCRIPT_FILENAME'] =
'/some/where/index.php';
442 $request->initialize(array(), array(), array(), array(), array(), $server);
443 $this->assertEquals(
'http://host:8080/path/info?query=string', $request->getUri(),
'->getUri() with rewrite');
447 $server[
'HTTP_HOST'] =
'host';
448 $server[
'SERVER_NAME'] =
'servername';
449 $server[
'SERVER_PORT'] =
'80';
451 $request->initialize(array(), array(), array(), array(), array(), $server);
453 $this->assertEquals(
'http://host/path/info?query=string', $request->getUri(),
'->getUri() with rewrite and default port');
456 unset($server[
'HTTP_HOST']);
457 $server[
'SERVER_NAME'] =
'servername';
458 $server[
'SERVER_PORT'] =
'80';
460 $request->initialize(array(), array(), array(), array(), array(), $server);
462 $this->assertEquals(
'http://servername/path/info?query=string', $request->getUri(),
'->getUri() with rewrite, default port without HOST_HEADER');
467 'HTTP_HOST' =>
'host:8080',
468 'SERVER_NAME' =>
'servername',
469 'SERVER_PORT' =>
'8080',
470 'QUERY_STRING' =>
'query=string',
471 'REQUEST_URI' =>
'/ba%20se/index_dev.php/foo%20bar/in+fo?query=string',
472 'SCRIPT_NAME' =>
'/ba se/index_dev.php',
473 'PATH_TRANSLATED' =>
'redirect:/index.php/foo bar/in+fo',
474 'PHP_SELF' =>
'/ba se/index_dev.php/path/info',
475 'SCRIPT_FILENAME' =>
'/some/where/ba se/index_dev.php',
478 $request->initialize(array(), array(), array(), array(), array(), $server);
481 'http://host:8080/ba%20se/index_dev.php/foo%20bar/in+fo?query=string',
487 $server[
'PHP_AUTH_USER'] =
'fabien';
488 $request->initialize(array(), array(), array(), array(), array(), $server);
489 $this->assertEquals(
'http://host:8080/ba%20se/index_dev.php/foo%20bar/in+fo?query=string', $request->getUri());
491 $server[
'PHP_AUTH_PW'] =
'symfony';
492 $request->initialize(array(), array(), array(), array(), array(), $server);
493 $this->assertEquals(
'http://host:8080/ba%20se/index_dev.php/foo%20bar/in+fo?query=string', $request->getUri());
499 $this->assertEquals(
'http://test.com/some/path', $request->getUriForPath(
'/some/path'));
502 $this->assertEquals(
'http://test.com:90/some/path', $request->getUriForPath(
'/some/path'));
505 $this->assertEquals(
'https://test.com/some/path', $request->getUriForPath(
'/some/path'));
508 $this->assertEquals(
'https://test.com:90/some/path', $request->getUriForPath(
'/some/path'));
515 $server[
'HTTP_HOST'] =
'host:8080';
516 $server[
'SERVER_NAME'] =
'servername';
517 $server[
'SERVER_PORT'] =
'8080';
519 $server[
'QUERY_STRING'] =
'query=string';
520 $server[
'REQUEST_URI'] =
'/index.php/path/info?query=string';
521 $server[
'SCRIPT_NAME'] =
'/index.php';
522 $server[
'PATH_INFO'] =
'/path/info';
523 $server[
'PATH_TRANSLATED'] =
'redirect:/index.php/path/info';
524 $server[
'PHP_SELF'] =
'/index_dev.php/path/info';
525 $server[
'SCRIPT_FILENAME'] =
'/some/where/index.php';
529 $request->initialize(array(), array(), array(), array(), array(), $server);
531 $this->assertEquals(
'http://host:8080/index.php/some/path', $request->getUriForPath(
'/some/path'),
'->getUriForPath() with non default port');
534 $server[
'HTTP_HOST'] =
'host';
535 $server[
'SERVER_NAME'] =
'servername';
536 $server[
'SERVER_PORT'] =
'80';
538 $request->initialize(array(), array(), array(), array(), array(), $server);
540 $this->assertEquals(
'http://host/index.php/some/path', $request->getUriForPath(
'/some/path'),
'->getUriForPath() with default port');
543 unset($server[
'HTTP_HOST']);
544 $server[
'SERVER_NAME'] =
'servername';
545 $server[
'SERVER_PORT'] =
'80';
547 $request->initialize(array(), array(), array(), array(), array(), $server);
549 $this->assertEquals(
'http://servername/index.php/some/path', $request->getUriForPath(
'/some/path'),
'->getUriForPath() with default port without HOST_HEADER');
556 $server[
'HTTP_HOST'] =
'host:8080';
557 $server[
'SERVER_NAME'] =
'servername';
558 $server[
'SERVER_PORT'] =
'8080';
560 $server[
'REDIRECT_QUERY_STRING'] =
'query=string';
561 $server[
'REDIRECT_URL'] =
'/path/info';
562 $server[
'SCRIPT_NAME'] =
'/index.php';
563 $server[
'QUERY_STRING'] =
'query=string';
564 $server[
'REQUEST_URI'] =
'/path/info?toto=test&1=1';
565 $server[
'SCRIPT_NAME'] =
'/index.php';
566 $server[
'PHP_SELF'] =
'/index.php';
567 $server[
'SCRIPT_FILENAME'] =
'/some/where/index.php';
569 $request->initialize(array(), array(), array(), array(), array(), $server);
570 $this->assertEquals(
'http://host:8080/some/path', $request->getUriForPath(
'/some/path'),
'->getUri() with rewrite');
574 $server[
'HTTP_HOST'] =
'host';
575 $server[
'SERVER_NAME'] =
'servername';
576 $server[
'SERVER_PORT'] =
'80';
578 $request->initialize(array(), array(), array(), array(), array(), $server);
580 $this->assertEquals(
'http://host/some/path', $request->getUriForPath(
'/some/path'),
'->getUriForPath() with rewrite and default port');
583 unset($server[
'HTTP_HOST']);
584 $server[
'SERVER_NAME'] =
'servername';
585 $server[
'SERVER_PORT'] =
'80';
587 $request->initialize(array(), array(), array(), array(), array(), $server);
589 $this->assertEquals(
'http://servername/some/path', $request->getUriForPath(
'/some/path'),
'->getUriForPath() with rewrite, default port without HOST_HEADER');
590 $this->assertEquals(
'servername', $request->getHttpHost());
594 $server[
'PHP_AUTH_USER'] =
'fabien';
595 $request->initialize(array(), array(), array(), array(), array(), $server);
596 $this->assertEquals(
'http://servername/some/path', $request->getUriForPath(
'/some/path'));
598 $server[
'PHP_AUTH_PW'] =
'symfony';
599 $request->initialize(array(), array(), array(), array(), array(), $server);
600 $this->assertEquals(
'http://servername/some/path', $request->getUriForPath(
'/some/path'));
608 $this->assertEquals($expected,
Request::create($pathinfo)->getRelativeUriForPath($path));
614 array(
'me.png',
'/foo',
'/me.png'),
615 array(
'../me.png',
'/foo/bar',
'/me.png'),
616 array(
'me.png',
'/foo/bar',
'/foo/me.png'),
617 array(
'../baz/me.png',
'/foo/bar/b',
'/foo/baz/me.png'),
618 array(
'../../fooz/baz/me.png',
'/foo/bar/b',
'/fooz/baz/me.png'),
619 array(
'baz/me.png',
'/foo/bar/b',
'baz/me.png'),
627 $server = array(
'PHP_AUTH_USER' =>
'fabien');
628 $request->initialize(array(), array(), array(), array(), array(), $server);
629 $this->assertEquals(
'fabien', $request->getUserInfo());
631 $server[
'PHP_AUTH_USER'] =
'0';
632 $request->initialize(array(), array(), array(), array(), array(), $server);
633 $this->assertEquals(
'0', $request->getUserInfo());
635 $server[
'PHP_AUTH_PW'] =
'0';
636 $request->initialize(array(), array(), array(), array(), array(), $server);
637 $this->assertEquals(
'0:0', $request->getUserInfo());
645 $server[
'SERVER_NAME'] =
'servername';
646 $server[
'SERVER_PORT'] =
'90';
647 $request->initialize(array(), array(), array(), array(), array(), $server);
648 $this->assertEquals(
'http://servername:90', $request->getSchemeAndHttpHost());
650 $server[
'PHP_AUTH_USER'] =
'fabien';
651 $request->initialize(array(), array(), array(), array(), array(), $server);
652 $this->assertEquals(
'http://servername:90', $request->getSchemeAndHttpHost());
654 $server[
'PHP_AUTH_USER'] =
'0';
655 $request->initialize(array(), array(), array(), array(), array(), $server);
656 $this->assertEquals(
'http://servername:90', $request->getSchemeAndHttpHost());
658 $server[
'PHP_AUTH_PW'] =
'0';
659 $request->initialize(array(), array(), array(), array(), array(), $server);
660 $this->assertEquals(
'http://servername:90', $request->getSchemeAndHttpHost());
670 $request->server->set(
'QUERY_STRING', $query);
671 $this->assertSame($expectedQuery, $request->getQueryString(), $msg);
677 array(
'foo',
'foo',
'works with valueless parameters'),
678 array(
'foo=',
'foo=',
'includes a dangling equal sign'),
679 array(
'bar=&foo=bar',
'bar=&foo=bar',
'->works with empty parameters'),
680 array(
'foo=bar&bar=',
'bar=&foo=bar',
'sorts keys alphabetically'),
684 array(
'him=John%20Doe&her=Jane+Doe',
'her=Jane%20Doe&him=John%20Doe',
'normalizes spaces in both encodings "%20" and "+"'),
686 array(
'foo[]=1&foo[]=2',
'foo%5B%5D=1&foo%5B%5D=2',
'allows array notation'),
687 array(
'foo=1&foo=2',
'foo=1&foo=2',
'allows repeated parameters'),
688 array(
'pa%3Dram=foo%26bar%3Dbaz&test=test',
'pa%3Dram=foo%26bar%3Dbaz&test=test',
'works with encoded delimiters'),
689 array(
'0',
'0',
'allows "0"'),
690 array(
'Jane Doe&John%20Doe',
'Jane%20Doe&John%20Doe',
'normalizes encoding in keys'),
691 array(
'her=Jane Doe&him=John%20Doe',
'her=Jane%20Doe&him=John%20Doe',
'normalizes encoding in values'),
692 array(
'foo=bar&&&test&&',
'foo=bar&test',
'removes unneeded delimiters'),
693 array(
'formula=e=m*c^2',
'formula=e%3Dm%2Ac%5E2',
'correctly treats only the first "=" as delimiter and the next as value'),
697 array(
'foo=bar&=a=b&=x=y',
'foo=bar',
'removes params with empty key'),
705 $this->assertNull($request->getQueryString(),
'->getQueryString() returns null for non-existent query string');
707 $request->server->set(
'QUERY_STRING',
'');
708 $this->assertNull($request->getQueryString(),
'->getQueryString() returns null for empty query string');
715 $request->initialize(array(
'foo' =>
'bar'));
716 $this->assertEquals(
'', $request->getHost(),
'->getHost() return empty string if not initialized');
718 $request->initialize(array(), array(), array(), array(), array(), array(
'HTTP_HOST' =>
'www.example.com'));
719 $this->assertEquals(
'www.example.com', $request->getHost(),
'->getHost() from Host Header');
722 $request->initialize(array(), array(), array(), array(), array(), array(
'HTTP_HOST' =>
'www.example.com:8080'));
723 $this->assertEquals(
'www.example.com', $request->getHost(),
'->getHost() from Host Header with port number');
726 $request->initialize(array(), array(), array(), array(), array(), array(
'SERVER_NAME' =>
'www.example.com'));
727 $this->assertEquals(
'www.example.com', $request->getHost(),
'->getHost() from server name');
729 $request->initialize(array(), array(), array(), array(), array(), array(
'SERVER_NAME' =>
'www.example.com',
'HTTP_HOST' =>
'www.host.com'));
730 $this->assertEquals(
'www.host.com', $request->getHost(),
'->getHost() value from Host header has priority over SERVER_NAME ');
735 $request =
Request::create(
'http://example.com',
'GET', array(), array(), array(), array(
736 'HTTP_X_FORWARDED_PROTO' =>
'https',
737 'HTTP_X_FORWARDED_PORT' =>
'443',
739 $port = $request->getPort();
741 $this->assertEquals(80, $port,
'Without trusted proxies FORWARDED_PROTO and FORWARDED_PORT are ignored.');
744 $request =
Request::create(
'http://example.com',
'GET', array(), array(), array(), array(
745 'HTTP_X_FORWARDED_PROTO' =>
'https',
746 'HTTP_X_FORWARDED_PORT' =>
'8443',
748 $this->assertEquals(80, $request->getPort(),
'With PROTO and PORT on untrusted connection server value takes precedence.');
749 $request->server->set(
'REMOTE_ADDR',
'1.1.1.1');
750 $this->assertEquals(8443, $request->getPort(),
'With PROTO and PORT set PORT takes precedence.');
752 $request =
Request::create(
'http://example.com',
'GET', array(), array(), array(), array(
753 'HTTP_X_FORWARDED_PROTO' =>
'https',
755 $this->assertEquals(80, $request->getPort(),
'With only PROTO set getPort() ignores trusted headers on untrusted connection.');
756 $request->server->set(
'REMOTE_ADDR',
'1.1.1.1');
757 $this->assertEquals(443, $request->getPort(),
'With only PROTO set getPort() defaults to 443.');
759 $request =
Request::create(
'http://example.com',
'GET', array(), array(), array(), array(
760 'HTTP_X_FORWARDED_PROTO' =>
'http',
762 $this->assertEquals(80, $request->getPort(),
'If X_FORWARDED_PROTO is set to HTTP getPort() ignores trusted headers on untrusted connection.');
763 $request->server->set(
'REMOTE_ADDR',
'1.1.1.1');
764 $this->assertEquals(80, $request->getPort(),
'If X_FORWARDED_PROTO is set to HTTP getPort() returns port of the original request.');
766 $request =
Request::create(
'http://example.com',
'GET', array(), array(), array(), array(
767 'HTTP_X_FORWARDED_PROTO' =>
'On',
769 $this->assertEquals(80, $request->getPort(),
'With only PROTO set and value is On, getPort() ignores trusted headers on untrusted connection.');
770 $request->server->set(
'REMOTE_ADDR',
'1.1.1.1');
771 $this->assertEquals(443, $request->getPort(),
'With only PROTO set and value is On, getPort() defaults to 443.');
773 $request =
Request::create(
'http://example.com',
'GET', array(), array(), array(), array(
774 'HTTP_X_FORWARDED_PROTO' =>
'1',
776 $this->assertEquals(80, $request->getPort(),
'With only PROTO set and value is 1, getPort() ignores trusted headers on untrusted connection.');
777 $request->server->set(
'REMOTE_ADDR',
'1.1.1.1');
778 $this->assertEquals(443, $request->getPort(),
'With only PROTO set and value is 1, getPort() defaults to 443.');
780 $request =
Request::create(
'http://example.com',
'GET', array(), array(), array(), array(
781 'HTTP_X_FORWARDED_PROTO' =>
'something-else',
783 $port = $request->getPort();
784 $this->assertEquals(80, $port,
'With only PROTO set and value is not recognized, getPort() defaults to 80.');
793 $request->initialize(array(), array(), array(), array(), array(), array(
'HTTP_HOST' =>
'www.host.com?query=string'));
801 $this->assertEquals(
'GET', $request->getMethod(),
'->getMethod() returns GET if no method is defined');
803 $request->setMethod(
'get');
804 $this->assertEquals(
'GET', $request->getMethod(),
'->getMethod() returns an uppercased string');
806 $request->setMethod(
'PURGE');
807 $this->assertEquals(
'PURGE', $request->getMethod(),
'->getMethod() returns the method even if it is not a standard one');
809 $request->setMethod(
'POST');
810 $this->assertEquals(
'POST', $request->getMethod(),
'->getMethod() returns the method POST if no _method is defined');
812 $request->setMethod(
'POST');
813 $request->request->set(
'_method',
'purge');
814 $this->assertEquals(
'POST', $request->getMethod(),
'->getMethod() does not return the method from _method if defined and POST but support not enabled');
817 $request->setMethod(
'POST');
818 $request->request->set(
'_method',
'purge');
826 $this->assertEquals(
'PURGE', $request->getMethod(),
'->getMethod() returns the method from _method if defined and POST');
827 $this->disableHttpMethodParameterOverride();
830 $request->setMethod(
'POST');
831 $request->query->set(
'_method',
'purge');
832 $this->assertEquals(
'POST', $request->getMethod(),
'->getMethod() does not return the method from _method if defined and POST but support not enabled');
835 $request->setMethod(
'POST');
836 $request->query->set(
'_method',
'purge');
838 $this->assertEquals(
'PURGE', $request->getMethod(),
'->getMethod() returns the method from _method if defined and POST');
839 $this->disableHttpMethodParameterOverride();
842 $request->setMethod(
'POST');
843 $request->headers->set(
'X-HTTP-METHOD-OVERRIDE',
'delete');
844 $this->assertEquals(
'DELETE', $request->getMethod(),
'->getMethod() returns the method from X-HTTP-Method-Override even though _method is set if defined and POST');
847 $request->setMethod(
'POST');
848 $request->headers->set(
'X-HTTP-METHOD-OVERRIDE',
'delete');
849 $this->assertEquals(
'DELETE', $request->getMethod(),
'->getMethod() returns the method from X-HTTP-Method-Override if defined and POST');
855 public function testGetClientIp($expected, $remoteAddr, $httpForwardedFor, $trustedProxies)
857 $request = $this->getRequestInstanceForClientIpTests($remoteAddr, $httpForwardedFor, $trustedProxies);
859 $this->assertEquals($expected[0], $request->getClientIp());
865 public function testGetClientIps($expected, $remoteAddr, $httpForwardedFor, $trustedProxies)
867 $request = $this->getRequestInstanceForClientIpTests($remoteAddr, $httpForwardedFor, $trustedProxies);
869 $this->assertEquals($expected, $request->getClientIps());
877 $request = $this->getRequestInstanceForClientIpsForwardedTests($remoteAddr, $httpForwarded, $trustedProxies);
879 $this->assertEquals($expected, $request->getClientIps());
886 array(array(
'127.0.0.1'),
'127.0.0.1',
'for="_gazonk"',
null),
887 array(array(
'127.0.0.1'),
'127.0.0.1',
'for="_gazonk"', array(
'127.0.0.1')),
888 array(array(
'88.88.88.88'),
'127.0.0.1',
'for="88.88.88.88:80"', array(
'127.0.0.1')),
889 array(array(
'192.0.2.60'),
'::1',
'for=192.0.2.60;proto=http;by=203.0.113.43', array(
'::1')),
890 array(array(
'2620:0:1cfe:face:b00c::3',
'192.0.2.43'),
'::1',
'for=192.0.2.43, for=2620:0:1cfe:face:b00c::3', array(
'::1')),
891 array(array(
'2001:db8:cafe::17'),
'::1',
'for="[2001:db8:cafe::17]:4711', array(
'::1')),
900 array(array(
'88.88.88.88'),
'88.88.88.88',
null,
null),
902 array(array(
'88.88.88.88'),
'88.88.88.88',
null, array(
'88.88.88.88')),
905 array(array(
'::1'),
'::1',
null,
null),
907 array(array(
'::1'),
'::1',
null, array(
'::1')),
910 array(array(
'127.0.0.1'),
'127.0.0.1',
'88.88.88.88',
null),
912 array(array(
'88.88.88.88'),
'127.0.0.1',
'88.88.88.88', array(
'127.0.0.1')),
914 array(array(
'88.88.88.88'),
'127.0.0.1',
'88.88.88.88', array(
'127.0.0.1',
'88.88.88.88')),
916 array(array(
'88.88.88.88'),
'123.45.67.89',
'88.88.88.88', array(
'123.45.67.0/24')),
919 array(array(
'1620:0:1cfe:face:b00c::3'),
'1620:0:1cfe:face:b00c::3',
'2620:0:1cfe:face:b00c::3',
null),
921 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')),
923 array(array(
'88.88.88.88'),
'2a01:198:603:0:396e:4789:8e99:890f',
'88.88.88.88', array(
'2a01:198:603:0::/65')),
926 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')),
928 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')),
930 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')),
932 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')),
935 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')),
937 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')),
939 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')),
942 array(array(
'88.88.88.88'),
'127.0.0.1',
'88.88.88.88:12345, 127.0.0.1', array(
'127.0.0.1')),
945 array(array(
'88.88.88.88'),
'127.0.0.1',
'unknown,88.88.88.88', array(
'127.0.0.1')),
946 array(array(
'88.88.88.88'),
'127.0.0.1',
'}__test|O:21:"JDatabaseDriverMysqli":3:{s:2,88.88.88.88', array(
'127.0.0.1')),
959 'REMOTE_ADDR' =>
'88.88.88.88',
960 'HTTP_FORWARDED' => $httpForwarded,
961 'HTTP_X_FORWARDED_FOR' => $httpXForwardedFor,
966 $request->initialize(array(), array(), array(), array(), array(), $server);
968 $request->getClientIps();
975 array(
'for=87.65.43.21',
'192.0.2.60'),
976 array(
'for=87.65.43.21, for=192.0.2.60',
'192.0.2.60'),
977 array(
'for=192.0.2.60',
'192.0.2.60,87.65.43.21'),
978 array(
'for="::face", for=192.0.2.60',
'192.0.2.60,192.0.2.43'),
979 array(
'for=87.65.43.21, for=192.0.2.60',
'192.0.2.60,87.65.43.21'),
991 'REMOTE_ADDR' =>
'88.88.88.88',
992 'HTTP_FORWARDED' => $httpForwarded,
993 'HTTP_X_FORWARDED_FOR' => $httpXForwardedFor,
998 $request->initialize(array(), array(), array(), array(), array(), $server);
1000 $clientIps = $request->getClientIps();
1002 $this->assertSame($expectedIps, $clientIps);
1009 array(
'for="192.0.2.60"',
'192.0.2.60', array(
'192.0.2.60')),
1010 array(
'for=192.0.2.60, for=87.65.43.21',
'192.0.2.60,87.65.43.21', array(
'87.65.43.21',
'192.0.2.60')),
1011 array(
'for="[::face]", for=192.0.2.60',
'::face,192.0.2.60', array(
'192.0.2.60',
'::face')),
1012 array(
'for="192.0.2.60:80"',
'192.0.2.60', array(
'192.0.2.60')),
1013 array(
'for=192.0.2.60;proto=http;by=203.0.113.43',
'192.0.2.60', array(
'192.0.2.60')),
1014 array(
'for="[2001:db8:cafe::17]:4711"',
'2001:db8:cafe::17', array(
'2001:db8:cafe::17')),
1021 $this->assertEquals(
'', $req->getContent());
1022 $this->assertEquals(
'', $req->getContent());
1028 $retval = $req->getContent(
true);
1029 $this->assertInternalType(
'resource', $retval);
1030 $this->assertEquals(
'', fread($retval, 1));
1031 $this->assertTrue(feof($retval));
1036 $req =
new Request(array(), array(), array(), array(), array(), array(),
'MyContent');
1037 $resource = $req->getContent(
true);
1039 $this->assertInternalType(
'resource', $resource);
1040 $this->assertEquals(
'MyContent', stream_get_contents($resource));
1045 $resource = fopen(
'php://memory',
'r+');
1046 fwrite($resource,
'My other content');
1049 $req =
new Request(array(), array(), array(), array(), array(), array(), $resource);
1050 $this->assertEquals(
'My other content', stream_get_contents($req->getContent(
true)));
1051 $this->assertEquals(
'My other content', $req->getContent());
1060 if (\PHP_VERSION_ID >= 50600) {
1061 $this->markTestSkipped(
'PHP >= 5.6 allows to open php://input several times.');
1065 $req->getContent($first);
1066 $req->getContent($second);
1072 'Resource then fetch' => array(
true,
false),
1073 'Resource then resource' => array(
true,
true),
1084 $a = $req->getContent($first);
1085 $b = $req->getContent($second);
1088 $a = stream_get_contents($a);
1092 $b = stream_get_contents($b);
1095 $this->assertSame($a, $b);
1101 'Fetch then fetch' => array(
false,
false),
1102 'Fetch then resource' => array(
false,
true),
1103 'Resource then fetch' => array(
true,
false),
1104 'Resource then resource' => array(
true,
true),
1125 $normalizedMethod = strtoupper($method);
1127 $_GET[
'foo1'] =
'bar1';
1128 $_POST[
'foo2'] =
'bar2';
1129 $_COOKIE[
'foo3'] =
'bar3';
1130 $_FILES[
'foo4'] = array(
'bar4');
1131 $_SERVER[
'foo5'] =
'bar5';
1134 $this->assertEquals(
'bar1', $request->query->get(
'foo1'),
'::fromGlobals() uses values from $_GET');
1135 $this->assertEquals(
'bar2', $request->request->get(
'foo2'),
'::fromGlobals() uses values from $_POST');
1136 $this->assertEquals(
'bar3', $request->cookies->get(
'foo3'),
'::fromGlobals() uses values from $_COOKIE');
1137 $this->assertEquals(array(
'bar4'), $request->files->get(
'foo4'),
'::fromGlobals() uses values from $_FILES');
1138 $this->assertEquals(
'bar5', $request->server->get(
'foo5'),
'::fromGlobals() uses values from $_SERVER');
1140 unset($_GET[
'foo1'], $_POST[
'foo2'], $_COOKIE[
'foo3'], $_FILES[
'foo4'], $_SERVER[
'foo5']);
1142 $_SERVER[
'REQUEST_METHOD'] = $method;
1143 $_SERVER[
'CONTENT_TYPE'] =
'application/x-www-form-urlencoded';
1145 $this->assertEquals($normalizedMethod, $request->getMethod());
1146 $this->assertEquals(
'mycontent', $request->request->get(
'content'));
1148 unset($_SERVER[
'REQUEST_METHOD'], $_SERVER[
'CONTENT_TYPE']);
1152 $_POST[
'_method'] = $method;
1153 $_POST[
'foo6'] =
'bar6';
1154 $_SERVER[
'REQUEST_METHOD'] =
'PoSt';
1156 $this->assertEquals($normalizedMethod, $request->getMethod());
1157 $this->assertEquals(
'POST', $request->getRealMethod());
1158 $this->assertEquals(
'bar6', $request->request->get(
'foo6'));
1160 unset($_POST[
'_method'], $_POST[
'foo6'], $_SERVER[
'REQUEST_METHOD']);
1161 $this->disableHttpMethodParameterOverride();
1167 $request->initialize(array(
'foo' =>
'bar'));
1172 $request->overrideGlobals();
1174 $this->assertEquals(array(
'foo' =>
'bar'), $_GET);
1176 $request->initialize(array(), array(
'foo' =>
'bar'));
1177 $request->overrideGlobals();
1179 $this->assertEquals(array(
'foo' =>
'bar'), $_POST);
1181 $this->assertArrayNotHasKey(
'HTTP_X_FORWARDED_PROTO', $_SERVER);
1183 $request->headers->set(
'X_FORWARDED_PROTO',
'https');
1186 $this->assertFalse($request->isSecure());
1187 $request->server->set(
'REMOTE_ADDR',
'1.1.1.1');
1188 $this->assertTrue($request->isSecure());
1190 $request->overrideGlobals();
1192 $this->assertArrayHasKey(
'HTTP_X_FORWARDED_PROTO', $_SERVER);
1194 $request->headers->set(
'CONTENT_TYPE',
'multipart/form-data');
1195 $request->headers->set(
'CONTENT_LENGTH', 12345);
1197 $request->overrideGlobals();
1199 $this->assertArrayHasKey(
'CONTENT_TYPE', $_SERVER);
1200 $this->assertArrayHasKey(
'CONTENT_LENGTH', $_SERVER);
1202 $request->initialize(array(
'foo' =>
'bar',
'baz' =>
'foo'));
1203 $request->query->remove(
'baz');
1205 $request->overrideGlobals();
1207 $this->assertEquals(array(
'foo' =>
'bar'), $_GET);
1208 $this->assertEquals(
'foo=bar', $_SERVER[
'QUERY_STRING']);
1209 $this->assertEquals(
'foo=bar', $request->server->get(
'QUERY_STRING'));
1218 $this->assertEquals(
'', $request->getScriptName());
1221 $server[
'SCRIPT_NAME'] =
'/index.php';
1223 $request->initialize(array(), array(), array(), array(), array(), $server);
1225 $this->assertEquals(
'/index.php', $request->getScriptName());
1228 $server[
'ORIG_SCRIPT_NAME'] =
'/frontend.php';
1229 $request->initialize(array(), array(), array(), array(), array(), $server);
1231 $this->assertEquals(
'/frontend.php', $request->getScriptName());
1234 $server[
'SCRIPT_NAME'] =
'/index.php';
1235 $server[
'ORIG_SCRIPT_NAME'] =
'/frontend.php';
1236 $request->initialize(array(), array(), array(), array(), array(), $server);
1238 $this->assertEquals(
'/index.php', $request->getScriptName());
1244 $this->assertEquals(
'', $request->getBasePath());
1247 $server[
'SCRIPT_FILENAME'] =
'/some/where/index.php';
1248 $request->initialize(array(), array(), array(), array(), array(), $server);
1249 $this->assertEquals(
'', $request->getBasePath());
1252 $server[
'SCRIPT_FILENAME'] =
'/some/where/index.php';
1253 $server[
'SCRIPT_NAME'] =
'/index.php';
1254 $request->initialize(array(), array(), array(), array(), array(), $server);
1256 $this->assertEquals(
'', $request->getBasePath());
1259 $server[
'SCRIPT_FILENAME'] =
'/some/where/index.php';
1260 $server[
'PHP_SELF'] =
'/index.php';
1261 $request->initialize(array(), array(), array(), array(), array(), $server);
1263 $this->assertEquals(
'', $request->getBasePath());
1266 $server[
'SCRIPT_FILENAME'] =
'/some/where/index.php';
1267 $server[
'ORIG_SCRIPT_NAME'] =
'/index.php';
1268 $request->initialize(array(), array(), array(), array(), array(), $server);
1270 $this->assertEquals(
'', $request->getBasePath());
1276 $this->assertEquals(
'/', $request->getPathInfo());
1279 $server[
'REQUEST_URI'] =
'/path/info';
1280 $request->initialize(array(), array(), array(), array(), array(), $server);
1282 $this->assertEquals(
'/path/info', $request->getPathInfo());
1285 $server[
'REQUEST_URI'] =
'/path%20test/info';
1286 $request->initialize(array(), array(), array(), array(), array(), $server);
1288 $this->assertEquals(
'/path%20test/info', $request->getPathInfo());
1294 $request->attributes->set(
'foo',
'attr');
1295 $request->query->set(
'foo',
'query');
1296 $request->request->set(
'foo',
'body');
1298 $this->assertSame(
'attr', $request->get(
'foo'));
1300 $request->attributes->remove(
'foo');
1301 $this->assertSame(
'query', $request->get(
'foo'));
1303 $request->query->remove(
'foo');
1304 $this->assertSame(
'body', $request->get(
'foo'));
1306 $request->request->remove(
'foo');
1307 $this->assertNull($request->get(
'foo'));
1313 $this->assertNull($request->getPreferredLanguage());
1314 $this->assertNull($request->getPreferredLanguage(array()));
1315 $this->assertEquals(
'fr', $request->getPreferredLanguage(array(
'fr')));
1316 $this->assertEquals(
'fr', $request->getPreferredLanguage(array(
'fr',
'en')));
1317 $this->assertEquals(
'en', $request->getPreferredLanguage(array(
'en',
'fr')));
1318 $this->assertEquals(
'fr-ch', $request->getPreferredLanguage(array(
'fr-ch',
'fr-fr')));
1321 $request->headers->set(
'Accept-language',
'zh, en-us; q=0.8, en; q=0.6');
1322 $this->assertEquals(
'en', $request->getPreferredLanguage(array(
'en',
'en-us')));
1325 $request->headers->set(
'Accept-language',
'zh, en-us; q=0.8, en; q=0.6');
1326 $this->assertEquals(
'en', $request->getPreferredLanguage(array(
'fr',
'en')));
1329 $request->headers->set(
'Accept-language',
'zh, en-us; q=0.8');
1330 $this->assertEquals(
'en', $request->getPreferredLanguage(array(
'fr',
'en')));
1333 $request->headers->set(
'Accept-language',
'zh, en-us; q=0.8, fr-fr; q=0.6, fr; q=0.5');
1334 $this->assertEquals(
'en', $request->getPreferredLanguage(array(
'fr',
'en')));
1340 $this->assertFalse($request->isXmlHttpRequest());
1342 $request->headers->set(
'X-Requested-With',
'XMLHttpRequest');
1343 $this->assertTrue($request->isXmlHttpRequest());
1345 $request->headers->remove(
'X-Requested-With');
1346 $this->assertFalse($request->isXmlHttpRequest());
1356 $request->setDefaultLocale(
'fr');
1357 $this->assertEquals(
'fr', $request->getLocale());
1358 $this->assertEquals(
'fr', \Locale::getDefault());
1360 $request->setLocale(
'en');
1361 $this->assertEquals(
'en', $request->getLocale());
1362 $this->assertEquals(
'en', \Locale::getDefault());
1364 $request->setDefaultLocale(
'de');
1365 $this->assertEquals(
'en', $request->getLocale());
1366 $this->assertEquals(
'en', \Locale::getDefault());
1372 $this->assertEquals(array(), $request->getCharsets());
1373 $request->headers->set(
'Accept-Charset',
'ISO-8859-1, US-ASCII, UTF-8; q=0.8, ISO-10646-UCS-2; q=0.6');
1374 $this->assertEquals(array(), $request->getCharsets());
1377 $request->headers->set(
'Accept-Charset',
'ISO-8859-1, US-ASCII, UTF-8; q=0.8, ISO-10646-UCS-2; q=0.6');
1378 $this->assertEquals(array(
'ISO-8859-1',
'US-ASCII',
'UTF-8',
'ISO-10646-UCS-2'), $request->getCharsets());
1381 $request->headers->set(
'Accept-Charset',
'ISO-8859-1,utf-8;q=0.7,*;q=0.7');
1382 $this->assertEquals(array(
'ISO-8859-1',
'utf-8',
'*'), $request->getCharsets());
1388 $this->assertEquals(array(), $request->getEncodings());
1389 $request->headers->set(
'Accept-Encoding',
'gzip,deflate,sdch');
1390 $this->assertEquals(array(), $request->getEncodings());
1393 $request->headers->set(
'Accept-Encoding',
'gzip,deflate,sdch');
1394 $this->assertEquals(array(
'gzip',
'deflate',
'sdch'), $request->getEncodings());
1397 $request->headers->set(
'Accept-Encoding',
'gzip;q=0.4,deflate;q=0.9,compress;q=0.7');
1398 $this->assertEquals(array(
'deflate',
'compress',
'gzip'), $request->getEncodings());
1404 $this->assertEquals(array(), $request->getAcceptableContentTypes());
1405 $request->headers->set(
'Accept',
'application/vnd.wap.wmlscriptc, text/vnd.wap.wml, application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html, multipart/mixed, */*');
1406 $this->assertEquals(array(), $request->getAcceptableContentTypes());
1409 $request->headers->set(
'Accept',
'application/vnd.wap.wmlscriptc, text/vnd.wap.wml, application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html, multipart/mixed, */*');
1410 $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());
1416 $this->assertEquals(array(), $request->getLanguages());
1419 $request->headers->set(
'Accept-language',
'zh, en-us; q=0.8, en; q=0.6');
1420 $this->assertEquals(array(
'zh',
'en_US',
'en'), $request->getLanguages());
1421 $this->assertEquals(array(
'zh',
'en_US',
'en'), $request->getLanguages());
1424 $request->headers->set(
'Accept-language',
'zh, en-us; q=0.6, en; q=0.8');
1425 $this->assertEquals(array(
'zh',
'en',
'en_US'), $request->getLanguages());
1428 $request->headers->set(
'Accept-language',
'zh, en, en-us');
1429 $this->assertEquals(array(
'zh',
'en',
'en_US'), $request->getLanguages());
1432 $request->headers->set(
'Accept-language',
'zh; q=0.6, en, en-us; q=0.6');
1433 $this->assertEquals(array(
'en',
'zh',
'en_US'), $request->getLanguages());
1436 $request->headers->set(
'Accept-language',
'zh, i-cherokee; q=0.6');
1437 $this->assertEquals(array(
'zh',
'cherokee'), $request->getLanguages());
1443 $this->assertEquals(
'html', $request->getRequestFormat());
1447 $this->assertEquals(
'json', $request->getRequestFormat(
'json'));
1448 $this->assertEquals(
'html', $request->getRequestFormat(
'html'));
1451 $this->assertNull($request->getRequestFormat(
null));
1454 $this->assertNull($request->setRequestFormat(
'foo'));
1455 $this->assertEquals(
'foo', $request->getRequestFormat(
null));
1457 $request =
new Request(array(
'_format' =>
'foo'));
1458 $this->assertEquals(
'html', $request->getRequestFormat());
1465 $this->assertFalse($request->hasSession());
1467 $this->assertTrue($request->hasSession());
1475 $this->assertTrue($request->hasSession());
1477 $session = $request->getSession();
1478 $this->assertObjectHasAttribute(
'storage', $session);
1479 $this->assertObjectHasAttribute(
'flashName', $session);
1480 $this->assertObjectHasAttribute(
'attributeName', $session);
1487 $this->assertFalse($request->hasPreviousSession());
1488 $request->cookies->set(
'MOCKSESSID',
'foo');
1489 $this->assertFalse($request->hasPreviousSession());
1491 $this->assertTrue($request->hasPreviousSession());
1498 $request->headers->set(
'Accept-language',
'zh, en-us; q=0.8, en; q=0.6');
1500 $this->assertContains(
'Accept-Language: zh, en-us; q=0.8, en; q=0.6', $request->__toString());
1506 $request->setMethod(
'POST');
1507 $this->assertTrue($request->isMethod(
'POST'));
1508 $this->assertTrue($request->isMethod(
'post'));
1509 $this->assertFalse($request->isMethod(
'GET'));
1510 $this->assertFalse($request->isMethod(
'get'));
1512 $request->setMethod(
'GET');
1513 $this->assertTrue($request->isMethod(
'GET'));
1514 $this->assertTrue($request->isMethod(
'get'));
1515 $this->assertFalse($request->isMethod(
'POST'));
1516 $this->assertFalse($request->isMethod(
'post'));
1524 $request =
Request::create($uri,
'GET', array(), array(), array(), $server);
1526 $this->assertSame($expectedBaseUrl, $request->getBaseUrl(),
'baseUrl');
1527 $this->assertSame($expectedPathInfo, $request->getPathInfo(),
'pathInfo');
1534 '/fruit/strawberry/1234index.php/blah',
1536 'SCRIPT_FILENAME' =>
'E:/Sites/cc-new/public_html/fruit/index.php',
1537 'SCRIPT_NAME' =>
'/fruit/index.php',
1538 'PHP_SELF' =>
'/fruit/index.php',
1541 '/strawberry/1234index.php/blah',
1544 '/fruit/strawberry/1234index.php/blah',
1546 'SCRIPT_FILENAME' =>
'E:/Sites/cc-new/public_html/index.php',
1547 'SCRIPT_NAME' =>
'/index.php',
1548 'PHP_SELF' =>
'/index.php',
1551 '/fruit/strawberry/1234index.php/blah',
1556 'SCRIPT_FILENAME' =>
'/home/John Doe/public_html/foo bar/app.php',
1557 'SCRIPT_NAME' =>
'/foo bar/app.php',
1558 'PHP_SELF' =>
'/foo bar/app.php',
1566 'SCRIPT_FILENAME' =>
'/home/John Doe/public_html/foo bar/app.php',
1567 'SCRIPT_NAME' =>
'/foo bar/app.php',
1568 'PHP_SELF' =>
'/foo bar/app.php',
1574 '/foo%20bar/app.php/home',
1576 'SCRIPT_FILENAME' =>
'/home/John Doe/public_html/foo bar/app.php',
1577 'SCRIPT_NAME' =>
'/foo bar/app.php',
1578 'PHP_SELF' =>
'/foo bar/app.php',
1580 '/foo%20bar/app.php',
1584 '/foo%20bar/app.php/home%3Dbaz',
1586 'SCRIPT_FILENAME' =>
'/home/John Doe/public_html/foo bar/app.php',
1587 'SCRIPT_NAME' =>
'/foo bar/app.php',
1588 'PHP_SELF' =>
'/foo bar/app.php',
1590 '/foo%20bar/app.php',
1596 'SCRIPT_FILENAME' =>
'/home/John Doe/public_html/foo/app.php',
1597 'SCRIPT_NAME' =>
'/foo/app.php',
1598 'PHP_SELF' =>
'/foo/app.php',
1613 $me = new \ReflectionMethod($request,
'getUrlencodedPrefix');
1614 $me->setAccessible(
true);
1616 $this->assertSame($expect, $me->invoke($request, $string, $prefix));
1622 array(
'foo',
'foo',
'foo'),
1623 array(
'fo%6f',
'foo',
'fo%6f'),
1624 array(
'foo/bar',
'foo',
'foo'),
1625 array(
'fo%6f/bar',
'foo',
'fo%6f'),
1626 array(
'f%6f%6f/bar',
'foo',
'f%6f%6f'),
1627 array(
'%66%6F%6F/bar',
'foo',
'%66%6F%6F'),
1628 array(
'fo+o/bar',
'fo+o',
'fo+o'),
1629 array(
'fo%2Bo/bar',
'fo+o',
'fo%2Bo'),
1633 private function disableHttpMethodParameterOverride()
1635 $class = new \ReflectionClass(
'Symfony\\Component\\HttpFoundation\\Request');
1636 $property = $class->getProperty(
'httpMethodParameterOverride');
1637 $property->setAccessible(
true);
1638 $property->setValue(
false);
1641 private function getRequestInstanceForClientIpTests($remoteAddr, $httpForwardedFor, $trustedProxies)
1645 $server = array(
'REMOTE_ADDR' => $remoteAddr);
1646 if (
null !== $httpForwardedFor) {
1647 $server[
'HTTP_X_FORWARDED_FOR'] = $httpForwardedFor;
1650 if ($trustedProxies) {
1654 $request->initialize(array(), array(), array(), array(), array(), $server);
1659 private function getRequestInstanceForClientIpsForwardedTests($remoteAddr, $httpForwarded, $trustedProxies)
1661 $request =
new Request();
1663 $server = array(
'REMOTE_ADDR' => $remoteAddr);
1665 if (
null !== $httpForwarded) {
1666 $server[
'HTTP_FORWARDED'] = $httpForwarded;
1669 if ($trustedProxies) {
1673 $request->initialize(array(), array(), array(), array(), array(), $server);
1681 $request->server->set(
'REMOTE_ADDR',
'3.3.3.3');
1682 $request->headers->set(
'X_FORWARDED_FOR',
'1.1.1.1, 2.2.2.2');
1683 $request->headers->set(
'X_FORWARDED_HOST',
'foo.example.com:1234, real.example.com:8080');
1684 $request->headers->set(
'X_FORWARDED_PROTO',
'https');
1685 $request->headers->set(
'X_FORWARDED_PORT', 443);
1688 $this->assertEquals(
'3.3.3.3', $request->getClientIp());
1689 $this->assertEquals(
'example.com', $request->getHost());
1690 $this->assertEquals(80, $request->getPort());
1691 $this->assertFalse($request->isSecure());
1695 $this->assertEquals(
'3.3.3.3', $request->getClientIp());
1696 $this->assertEquals(
'example.com', $request->getHost());
1697 $this->assertEquals(80, $request->getPort());
1698 $this->assertFalse($request->isSecure());
1702 $this->assertEquals(
'3.3.3.3', $request->getClientIp());
1703 $this->assertEquals(
'example.com', $request->getHost());
1704 $this->assertEquals(80, $request->getPort());
1705 $this->assertFalse($request->isSecure());
1709 $this->assertEquals(
'1.1.1.1', $request->getClientIp());
1710 $this->assertEquals(
'foo.example.com', $request->getHost());
1711 $this->assertEquals(443, $request->getPort());
1712 $this->assertTrue($request->isSecure());
1716 $this->assertEquals(
'3.3.3.3', $request->getClientIp());
1717 $this->assertEquals(
'example.com', $request->getHost());
1718 $this->assertEquals(80, $request->getPort());
1719 $this->assertFalse($request->isSecure());
1723 $request->headers->set(
'X_FORWARDED_PROTO',
'ssl');
1724 $this->assertTrue($request->isSecure());
1726 $request->headers->set(
'X_FORWARDED_PROTO',
'https, http');
1727 $this->assertTrue($request->isSecure());
1737 $request->server->set(
'REMOTE_ADDR',
'3.3.3.3');
1738 $request->headers->set(
'X_FORWARDED_FOR',
'1.1.1.1, 2.2.2.2');
1739 $request->headers->set(
'X_FORWARDED_HOST',
'foo.example.com, real.example.com:8080');
1740 $request->headers->set(
'X_FORWARDED_PROTO',
'https');
1741 $request->headers->set(
'X_FORWARDED_PORT', 443);
1742 $request->headers->set(
'X_MY_FOR',
'3.3.3.3, 4.4.4.4');
1743 $request->headers->set(
'X_MY_HOST',
'my.example.com');
1744 $request->headers->set(
'X_MY_PROTO',
'http');
1745 $request->headers->set(
'X_MY_PORT', 81);
1754 $this->assertEquals(
'4.4.4.4', $request->getClientIp());
1755 $this->assertEquals(
'my.example.com', $request->getHost());
1756 $this->assertEquals(81, $request->getPort());
1757 $this->assertFalse($request->isSecure());
1764 $this->assertEquals(
'3.3.3.3', $request->getClientIp());
1765 $this->assertEquals(
'example.com', $request->getHost());
1766 $this->assertEquals(80, $request->getPort());
1767 $this->assertFalse($request->isSecure());
1780 $request->server->set(
'REMOTE_ADDR',
'3.3.3.3');
1781 $request->headers->set(
'FORWARDED',
'for=1.1.1.1, host=foo.example.com:8080, proto=https, for=2.2.2.2, host=real.example.com:8080');
1784 $this->assertEquals(
'3.3.3.3', $request->getClientIp());
1785 $this->assertEquals(
'example.com', $request->getHost());
1786 $this->assertEquals(80, $request->getPort());
1787 $this->assertFalse($request->isSecure());
1791 $this->assertEquals(
'3.3.3.3', $request->getClientIp());
1792 $this->assertEquals(
'example.com', $request->getHost());
1793 $this->assertEquals(80, $request->getPort());
1794 $this->assertFalse($request->isSecure());
1798 $this->assertEquals(
'3.3.3.3', $request->getClientIp());
1799 $this->assertEquals(
'example.com', $request->getHost());
1800 $this->assertEquals(80, $request->getPort());
1801 $this->assertFalse($request->isSecure());
1805 $this->assertEquals(
'1.1.1.1', $request->getClientIp());
1806 $this->assertEquals(
'foo.example.com', $request->getHost());
1807 $this->assertEquals(8080, $request->getPort());
1808 $this->assertTrue($request->isSecure());
1812 $this->assertEquals(
'3.3.3.3', $request->getClientIp());
1813 $this->assertEquals(
'example.com', $request->getHost());
1814 $this->assertEquals(80, $request->getPort());
1815 $this->assertFalse($request->isSecure());
1819 $request->headers->set(
'FORWARDED',
'proto=ssl');
1820 $this->assertTrue($request->isSecure());
1822 $request->headers->set(
'FORWARDED',
'proto=https, proto=http');
1823 $this->assertTrue($request->isSecure());
1852 $request->headers->replace($headers);
1853 $request->server->replace($server);
1855 $this->assertEquals($expectedRequestUri, $request->getRequestUri(),
'->getRequestUri() is correct');
1857 $subRequestUri =
'/bar/foo';
1858 $subRequest =
Request::create($subRequestUri,
'get', array(), array(), array(), $request->server->all());
1859 $this->assertEquals($subRequestUri, $subRequest->getRequestUri(),
'->getRequestUri() is correct in sub request');
1867 'X_ORIGINAL_URL' =>
'/foo/bar',
1874 'X_REWRITE_URL' =>
'/foo/bar',
1882 'IIS_WasUrlRewritten' =>
'1',
1883 'UNENCODED_URL' =>
'/foo/bar',
1889 'X_ORIGINAL_URL' =>
'/foo/bar',
1892 'HTTP_X_ORIGINAL_URL' =>
'/foo/bar',
1898 'X_ORIGINAL_URL' =>
'/foo/bar',
1901 'IIS_WasUrlRewritten' =>
'1',
1902 'UNENCODED_URL' =>
'/foo/bar',
1908 'X_ORIGINAL_URL' =>
'/foo/bar',
1911 'HTTP_X_ORIGINAL_URL' =>
'/foo/bar',
1912 'IIS_WasUrlRewritten' =>
'1',
1913 'UNENCODED_URL' =>
'/foo/bar',
1920 'ORIG_PATH_INFO' =>
'/foo/bar',
1927 'ORIG_PATH_INFO' =>
'/foo/bar',
1928 'QUERY_STRING' =>
'foo=bar',
1941 $request->headers->set(
'host',
'evil.com');
1942 $this->assertEquals(
'evil.com', $request->getHost());
1948 $request->headers->set(
'host',
'evil.com');
1950 $request->getHost();
1951 $this->fail(
'Request::getHost() should throw an exception when host is not trusted.');
1953 $this->assertEquals(
'Untrusted Host "evil.com".', $e->getMessage());
1957 $request->headers->set(
'host',
'trusted.com');
1958 $this->assertEquals(
'trusted.com', $request->getHost());
1959 $this->assertEquals(80, $request->getPort());
1961 $request->server->set(
'HTTPS',
true);
1962 $request->headers->set(
'host',
'trusted.com');
1963 $this->assertEquals(
'trusted.com', $request->getHost());
1964 $this->assertEquals(443, $request->getPort());
1965 $request->server->set(
'HTTPS',
false);
1967 $request->headers->set(
'host',
'trusted.com:8000');
1968 $this->assertEquals(
'trusted.com', $request->getHost());
1969 $this->assertEquals(8000, $request->getPort());
1971 $request->headers->set(
'host',
'subdomain.trusted.com');
1972 $this->assertEquals(
'subdomain.trusted.com', $request->getHost());
1980 Request::setFactory(
function (array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content =
null) {
1994 $start = microtime(
true);
1997 $request->headers->set(
'host', $host);
1998 $this->assertEquals($host, $request->getHost());
1999 $this->assertLessThan(5, microtime(
true) - $start);
2008 $request->headers->set(
'host', $host);
2011 $this->assertSame($expectedHost ?: $host, $request->getHost());
2012 if ($expectedPort) {
2013 $this->assertSame($expectedPort, $request->getPort());
2016 if (method_exists($this,
'expectException')) {
2017 $this->expectException(SuspiciousOperationException::class);
2018 $this->expectExceptionMessage(
'Invalid Host');
2020 $this->setExpectedException(SuspiciousOperationException::class,
'Invalid Host');
2023 $request->getHost();
2031 array(
'a..',
false),
2033 array(
"\xE9",
false),
2034 array(
'[::1]',
true),
2035 array(
'[::1]:80',
true,
'[::1]', 80),
2036 array(str_repeat(
'.', 101),
false),
2043 array(
'a'.str_repeat(
'.a', 40000)),
2044 array(str_repeat(
':', 101)),
2054 $request->setMethod($method);
2055 $this->assertEquals($idempotent, $request->isMethodIdempotent());
2061 array(
'HEAD',
true),
2063 array(
'POST',
false),
2065 array(
'PATCH',
false),
2066 array(
'DELETE',
true),
2067 array(
'PURGE',
true),
2068 array(
'OPTIONS',
true),
2069 array(
'TRACE',
true),
2070 array(
'CONNECT',
false),
2080 $request->setMethod($method);
2081 $this->assertEquals($safe, $request->isMethodSafe(
false));
2087 array(
'HEAD',
true),
2089 array(
'POST',
false),
2090 array(
'PUT',
false),
2091 array(
'PATCH',
false),
2092 array(
'DELETE',
false),
2093 array(
'PURGE',
false),
2094 array(
'OPTIONS',
true),
2095 array(
'TRACE',
true),
2096 array(
'CONNECT',
false),
2107 $request->setMethod(
'OPTIONS');
2108 $this->assertFalse($request->isMethodSafe());
2117 $request->setMethod($method);
2118 $this->assertEquals($chacheable, $request->isMethodCacheable());
2124 array(
'HEAD',
true),
2126 array(
'POST',
false),
2127 array(
'PUT',
false),
2128 array(
'PATCH',
false),
2129 array(
'DELETE',
false),
2130 array(
'PURGE',
false),
2131 array(
'OPTIONS',
false),
2132 array(
'TRACE',
false),
2133 array(
'CONNECT',
false),
2197 return http_build_query(array(
'_method' =>
'PUT',
'content' =>
'mycontent'));