123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825 |
- <?php
-
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
- namespace Symfony\Tests\Component\HttpFoundation;
-
- use Symfony\Component\HttpFoundation\HeaderBag;
-
- use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
-
- use Symfony\Component\HttpFoundation\Session;
-
- use Symfony\Component\HttpFoundation\Request;
-
- class RequestTest extends \PHPUnit_Framework_TestCase
- {
- public function setUp()
- {
- Request::trustProxyData();
- }
-
- /**
- * @covers Symfony\Component\HttpFoundation\Request::__construct
- */
- public function testConstructor()
- {
- $this->testInitialize();
- }
-
- /**
- * @covers Symfony\Component\HttpFoundation\Request::initialize
- */
- public function testInitialize()
- {
- $request = new Request();
-
- $request->initialize(array('foo' => 'bar'));
- $this->assertEquals('bar', $request->query->get('foo'), '->initialize() takes an array of query parameters as its first argument');
-
- $request->initialize(array(), array('foo' => 'bar'));
- $this->assertEquals('bar', $request->request->get('foo'), '->initialize() takes an array of request parameters as its second argument');
-
- $request->initialize(array(), array(), array('foo' => 'bar'));
- $this->assertEquals('bar', $request->attributes->get('foo'), '->initialize() takes an array of attributes as its third argument');
-
- $request->initialize(array(), array(), array(), array(), array(), array('HTTP_FOO' => 'bar'));
- $this->assertEquals('bar', $request->headers->get('FOO'), '->initialize() takes an array of HTTP headers as its fourth argument');
- }
-
- /**
- * @covers Symfony\Component\HttpFoundation\Request::create
- */
- public function testCreate()
- {
- $request = Request::create('http://test.com/foo?bar=baz');
- $this->assertEquals('http://test.com/foo?bar=baz', $request->getUri());
- $this->assertEquals('/foo', $request->getPathInfo());
- $this->assertEquals('bar=baz', $request->getQueryString());
- $this->assertEquals(80, $request->getPort());
- $this->assertEquals('test.com', $request->getHttpHost());
- $this->assertFalse($request->isSecure());
-
- $request = Request::create('https://test.com/foo?bar=baz');
- $this->assertEquals('https://test.com/foo?bar=baz', $request->getUri());
- $this->assertEquals('/foo', $request->getPathInfo());
- $this->assertEquals('bar=baz', $request->getQueryString());
- $this->assertEquals(443, $request->getPort());
- $this->assertEquals('test.com', $request->getHttpHost());
- $this->assertTrue($request->isSecure());
-
- $request = Request::create('test.com:90/foo');
- $this->assertEquals('http://test.com:90/foo', $request->getUri());
- $this->assertEquals('/foo', $request->getPathInfo());
- $this->assertEquals('test.com', $request->getHost());
- $this->assertEquals('test.com:90', $request->getHttpHost());
- $this->assertEquals(90, $request->getPort());
- $this->assertFalse($request->isSecure());
-
- $request = Request::create('https://test.com:90/foo');
- $this->assertEquals('https://test.com:90/foo', $request->getUri());
- $this->assertEquals('/foo', $request->getPathInfo());
- $this->assertEquals('test.com', $request->getHost());
- $this->assertEquals('test.com:90', $request->getHttpHost());
- $this->assertEquals(90, $request->getPort());
- $this->assertTrue($request->isSecure());
-
- $request = Request::create('https://127.0.0.1:90/foo');
- $this->assertEquals('https://127.0.0.1:90/foo', $request->getUri());
- $this->assertEquals('/foo', $request->getPathInfo());
- $this->assertEquals('127.0.0.1', $request->getHost());
- $this->assertEquals('127.0.0.1:90', $request->getHttpHost());
- $this->assertEquals(90, $request->getPort());
- $this->assertTrue($request->isSecure());
-
- $request = Request::create('https://[::1]:90/foo');
- $this->assertEquals('https://[::1]:90/foo', $request->getUri());
- $this->assertEquals('/foo', $request->getPathInfo());
- $this->assertEquals('[::1]', $request->getHost());
- $this->assertEquals('[::1]:90', $request->getHttpHost());
- $this->assertEquals(90, $request->getPort());
- $this->assertTrue($request->isSecure());
-
- $json = '{"jsonrpc":"2.0","method":"echo","id":7,"params":["Hello World"]}';
- $request = Request::create('http://example.com/jsonrpc', 'POST', array(), array(), array(), array(), $json);
- $this->assertEquals($json, $request->getContent());
- $this->assertFalse($request->isSecure());
-
- $request = Request::create('http://test.com');
- $this->assertEquals('http://test.com/', $request->getUri());
- $this->assertEquals('/', $request->getPathInfo());
- $this->assertEquals('', $request->getQueryString());
- $this->assertEquals(80, $request->getPort());
- $this->assertEquals('test.com', $request->getHttpHost());
- $this->assertFalse($request->isSecure());
-
- $request = Request::create('http://test.com:90/?test=1');
- $this->assertEquals('http://test.com:90/?test=1', $request->getUri());
- $this->assertEquals('/', $request->getPathInfo());
- $this->assertEquals('test=1', $request->getQueryString());
- $this->assertEquals(90, $request->getPort());
- $this->assertEquals('test.com:90', $request->getHttpHost());
- $this->assertFalse($request->isSecure());
-
- }
-
- /**
- * @covers Symfony\Component\HttpFoundation\Request::duplicate
- */
- public function testDuplicate()
- {
- $request = new Request(array('foo' => 'bar'), array('foo' => 'bar'), array('foo' => 'bar'), array(), array(), array('HTTP_FOO' => 'bar'));
- $dup = $request->duplicate();
-
- $this->assertEquals($request->query->all(), $dup->query->all(), '->duplicate() duplicates a request an copy the current query parameters');
- $this->assertEquals($request->request->all(), $dup->request->all(), '->duplicate() duplicates a request an copy the current request parameters');
- $this->assertEquals($request->attributes->all(), $dup->attributes->all(), '->duplicate() duplicates a request an copy the current attributes');
- $this->assertEquals($request->headers->all(), $dup->headers->all(), '->duplicate() duplicates a request an copy the current HTTP headers');
-
- $dup = $request->duplicate(array('foo' => 'foobar'), array('foo' => 'foobar'), array('foo' => 'foobar'), array(), array(), array('HTTP_FOO' => 'foobar'));
-
- $this->assertEquals(array('foo' => 'foobar'), $dup->query->all(), '->duplicate() overrides the query parameters if provided');
- $this->assertEquals(array('foo' => 'foobar'), $dup->request->all(), '->duplicate() overrides the request parameters if provided');
- $this->assertEquals(array('foo' => 'foobar'), $dup->attributes->all(), '->duplicate() overrides the attributes if provided');
- $this->assertEquals(array('foo' => array('foobar')), $dup->headers->all(), '->duplicate() overrides the HTTP header if provided');
- }
-
- /**
- * @covers Symfony\Component\HttpFoundation\Request::getFormat
- * @covers Symfony\Component\HttpFoundation\Request::setFormat
- * @dataProvider getFormatToMimeTypeMapProvider
- */
- public function testGetFormatFromMimeType($format, $mimeTypes)
- {
- $request = new Request();
- foreach ($mimeTypes as $mime) {
- $this->assertEquals($format, $request->getFormat($mime));
- }
- $request->setFormat($format, $mimeTypes);
- foreach ($mimeTypes as $mime) {
- $this->assertEquals($format, $request->getFormat($mime));
- }
- }
-
- /**
- * @covers Symfony\Component\HttpFoundation\Request::getFormat
- */
- public function testGetFormatFromMimeTypeWithParameters()
- {
- $request = new Request();
- $this->assertEquals('json', $request->getFormat('application/json; charset=utf-8'));
- }
-
- /**
- * @covers Symfony\Component\HttpFoundation\Request::getMimeType
- * @dataProvider getFormatToMimeTypeMapProvider
- */
- public function testGetMimeTypeFromFormat($format, $mimeTypes)
- {
- if (null !== $format) {
- $request = new Request();
- $this->assertEquals($mimeTypes[0], $request->getMimeType($format));
- }
- }
-
- public function getFormatToMimeTypeMapProvider()
- {
- return array(
- array(null, array(null, 'unexistent-mime-type')),
- array('txt', array('text/plain')),
- array('js', array('application/javascript', 'application/x-javascript', 'text/javascript')),
- array('css', array('text/css')),
- array('json', array('application/json', 'application/x-json')),
- array('xml', array('text/xml', 'application/xml', 'application/x-xml')),
- array('rdf', array('application/rdf+xml')),
- array('atom',array('application/atom+xml')),
- );
- }
-
- /**
- * @covers Symfony\Component\HttpFoundation\Request::getUri
- */
- public function testGetUri()
- {
- $server = array();
-
- // Standard Request on non default PORT
- // http://hostname:8080/index.php/path/info?query=string
-
- $server['HTTP_HOST'] = 'hostname:8080';
- $server['SERVER_NAME'] = 'servername';
- $server['SERVER_PORT'] = '8080';
-
- $server['QUERY_STRING'] = 'query=string';
- $server['REQUEST_URI'] = '/index.php/path/info?query=string';
- $server['SCRIPT_NAME'] = '/index.php';
- $server['PATH_INFO'] = '/path/info';
- $server['PATH_TRANSLATED'] = 'redirect:/index.php/path/info';
- $server['PHP_SELF'] = '/index_dev.php/path/info';
- $server['SCRIPT_FILENAME'] = '/some/where/index.php';
-
- $request = new Request();
-
- $request->initialize(array(), array(), array(), array(), array(), $server);
-
- $this->assertEquals('http://hostname:8080/index.php/path/info?query=string', $request->getUri(), '->getUri() with non default port');
-
- // Use std port number
- $server['HTTP_HOST'] = 'hostname';
- $server['SERVER_NAME'] = 'servername';
- $server['SERVER_PORT'] = '80';
-
- $request->initialize(array(), array(), array(), array(), array(), $server);
-
- $this->assertEquals('http://hostname/index.php/path/info?query=string', $request->getUri(), '->getUri() with default port');
-
- // Without HOST HEADER
- unset($server['HTTP_HOST']);
- $server['SERVER_NAME'] = 'servername';
- $server['SERVER_PORT'] = '80';
-
- $request->initialize(array(), array(), array(), array(), array(), $server);
-
- $this->assertEquals('http://servername/index.php/path/info?query=string', $request->getUri(), '->getUri() with default port without HOST_HEADER');
-
- // Request with URL REWRITING (hide index.php)
- // RewriteCond %{REQUEST_FILENAME} !-f
- // RewriteRule ^(.*)$ index.php [QSA,L]
- // http://hostname:8080/path/info?query=string
- $server = array();
- $server['HTTP_HOST'] = 'hostname:8080';
- $server['SERVER_NAME'] = 'servername';
- $server['SERVER_PORT'] = '8080';
-
- $server['REDIRECT_QUERY_STRING'] = 'query=string';
- $server['REDIRECT_URL'] = '/path/info';
- $server['SCRIPT_NAME'] = '/index.php';
- $server['QUERY_STRING'] = 'query=string';
- $server['REQUEST_URI'] = '/path/info?toto=test&1=1';
- $server['SCRIPT_NAME'] = '/index.php';
- $server['PHP_SELF'] = '/index.php';
- $server['SCRIPT_FILENAME'] = '/some/where/index.php';
-
- $request->initialize(array(), array(), array(), array(), array(), $server);
- $this->assertEquals('http://hostname:8080/path/info?query=string', $request->getUri(), '->getUri() with rewrite');
-
- // Use std port number
- // http://hostname/path/info?query=string
- $server['HTTP_HOST'] = 'hostname';
- $server['SERVER_NAME'] = 'servername';
- $server['SERVER_PORT'] = '80';
-
- $request->initialize(array(), array(), array(), array(), array(), $server);
-
- $this->assertEquals('http://hostname/path/info?query=string', $request->getUri(), '->getUri() with rewrite and default port');
-
- // Without HOST HEADER
- unset($server['HTTP_HOST']);
- $server['SERVER_NAME'] = 'servername';
- $server['SERVER_PORT'] = '80';
-
- $request->initialize(array(), array(), array(), array(), array(), $server);
-
- $this->assertEquals('http://servername/path/info?query=string', $request->getUri(), '->getUri() with rewrite, default port without HOST_HEADER');
- }
-
- /**
- * @covers Symfony\Component\HttpFoundation\Request::getUriForPath
- */
- public function testGetUriForPath()
- {
- $request = Request::create('http://test.com/foo?bar=baz');
- $this->assertEquals('http://test.com/some/path', $request->getUriForPath('/some/path'));
-
- $request = Request::create('http://test.com:90/foo?bar=baz');
- $this->assertEquals('http://test.com:90/some/path', $request->getUriForPath('/some/path'));
-
- $request = Request::create('https://test.com/foo?bar=baz');
- $this->assertEquals('https://test.com/some/path', $request->getUriForPath('/some/path'));
-
- $request = Request::create('https://test.com:90/foo?bar=baz');
- $this->assertEquals('https://test.com:90/some/path', $request->getUriForPath('/some/path'));
-
- $server = array();
-
- // Standard Request on non default PORT
- // http://hostname:8080/index.php/path/info?query=string
-
- $server['HTTP_HOST'] = 'hostname:8080';
- $server['SERVER_NAME'] = 'servername';
- $server['SERVER_PORT'] = '8080';
-
- $server['QUERY_STRING'] = 'query=string';
- $server['REQUEST_URI'] = '/index.php/path/info?query=string';
- $server['SCRIPT_NAME'] = '/index.php';
- $server['PATH_INFO'] = '/path/info';
- $server['PATH_TRANSLATED'] = 'redirect:/index.php/path/info';
- $server['PHP_SELF'] = '/index_dev.php/path/info';
- $server['SCRIPT_FILENAME'] = '/some/where/index.php';
-
- $request = new Request();
-
- $request->initialize(array(), array(), array(), array(), array(),$server);
-
- $this->assertEquals('http://hostname:8080/index.php/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with non default port');
-
- // Use std port number
- $server['HTTP_HOST'] = 'hostname';
- $server['SERVER_NAME'] = 'servername';
- $server['SERVER_PORT'] = '80';
-
- $request->initialize(array(), array(), array(), array(), array(), $server);
-
- $this->assertEquals('http://hostname/index.php/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with default port');
-
- // Without HOST HEADER
- unset($server['HTTP_HOST']);
- $server['SERVER_NAME'] = 'servername';
- $server['SERVER_PORT'] = '80';
-
- $request->initialize(array(), array(), array(), array(), array(), $server);
-
- $this->assertEquals('http://servername/index.php/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with default port without HOST_HEADER');
-
- // Request with URL REWRITING (hide index.php)
- // RewriteCond %{REQUEST_FILENAME} !-f
- // RewriteRule ^(.*)$ index.php [QSA,L]
- // http://hostname:8080/path/info?query=string
- $server = array();
- $server['HTTP_HOST'] = 'hostname:8080';
- $server['SERVER_NAME'] = 'servername';
- $server['SERVER_PORT'] = '8080';
-
- $server['REDIRECT_QUERY_STRING'] = 'query=string';
- $server['REDIRECT_URL'] = '/path/info';
- $server['SCRIPT_NAME'] = '/index.php';
- $server['QUERY_STRING'] = 'query=string';
- $server['REQUEST_URI'] = '/path/info?toto=test&1=1';
- $server['SCRIPT_NAME'] = '/index.php';
- $server['PHP_SELF'] = '/index.php';
- $server['SCRIPT_FILENAME'] = '/some/where/index.php';
-
- $request->initialize(array(), array(), array(), array(), array(), $server);
- $this->assertEquals('http://hostname:8080/some/path', $request->getUriForPath('/some/path'), '->getUri() with rewrite');
-
- // Use std port number
- // http://hostname/path/info?query=string
- $server['HTTP_HOST'] = 'hostname';
- $server['SERVER_NAME'] = 'servername';
- $server['SERVER_PORT'] = '80';
-
- $request->initialize(array(), array(), array(), array(), array(), $server);
-
- $this->assertEquals('http://hostname/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with rewrite and default port');
-
- // Without HOST HEADER
- unset($server['HTTP_HOST']);
- $server['SERVER_NAME'] = 'servername';
- $server['SERVER_PORT'] = '80';
-
- $request->initialize(array(), array(), array(), array(), array(), $server);
-
- $this->assertEquals('http://servername/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with rewrite, default port without HOST_HEADER');
- $this->assertEquals('servername', $request->getHttpHost());
- }
-
- /**
- * @covers Symfony\Component\HttpFoundation\Request::getQueryString
- */
- public function testGetQueryString()
- {
- $request = new Request();
-
- $request->server->set('QUERY_STRING', 'foo');
- $this->assertEquals('foo', $request->getQueryString(), '->getQueryString() works with valueless parameters');
-
- $request->server->set('QUERY_STRING', 'foo=');
- $this->assertEquals('foo=', $request->getQueryString(), '->getQueryString() includes a dangling equal sign');
-
- $request->server->set('QUERY_STRING', 'bar=&foo=bar');
- $this->assertEquals('bar=&foo=bar', $request->getQueryString(), '->getQueryString() works when empty parameters');
-
- $request->server->set('QUERY_STRING', 'foo=bar&bar=');
- $this->assertEquals('bar=&foo=bar', $request->getQueryString(), '->getQueryString() sorts keys alphabetically');
-
- $request->server->set('QUERY_STRING', 'him=John%20Doe&her=Jane+Doe');
- $this->assertEquals('her=Jane%2BDoe&him=John%20Doe', $request->getQueryString(), '->getQueryString() normalizes encoding');
-
- $request->server->set('QUERY_STRING', 'foo[]=1&foo[]=2');
- $this->assertEquals('foo%5B%5D=1&foo%5B%5D=2', $request->getQueryString(), '->getQueryString() allows array notation');
-
- $request->server->set('QUERY_STRING', 'foo=1&foo=2');
- $this->assertEquals('foo=1&foo=2', $request->getQueryString(), '->getQueryString() allows repeated parameters');
- }
-
- /**
- * @covers Symfony\Component\HttpFoundation\Request::getHost
- */
- public function testGetHost()
- {
- $request = new Request();
-
- $request->initialize(array('foo' => 'bar'));
- $this->assertEquals('', $request->getHost(), '->getHost() return empty string if not initialized');
-
- $request->initialize(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'www.exemple.com'));
- $this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from Host Header');
-
- // Host header with port number.
- $request->initialize(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'www.exemple.com:8080'));
- $this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from Host Header with port number');
-
- // Server values.
- $request->initialize(array(), array(), array(), array(), array(), array('SERVER_NAME' => 'www.exemple.com'));
- $this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from server name');
-
- // X_FORWARDED_HOST.
- $request->initialize(array(), array(), array(), array(), array(), array('HTTP_X_FORWARDED_HOST' => 'www.exemple.com'));
- $this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from X_FORWARDED_HOST');
-
- // X_FORWARDED_HOST
- $request->initialize(array(), array(), array(), array(), array(), array('HTTP_X_FORWARDED_HOST' => 'www.exemple.com, www.second.com'));
- $this->assertEquals('www.second.com', $request->getHost(), '->getHost() value from X_FORWARDED_HOST use last value');
-
- // X_FORWARDED_HOST with port number
- $request->initialize(array(), array(), array(), array(), array(), array('HTTP_X_FORWARDED_HOST' => 'www.exemple.com, www.second.com:8080'));
- $this->assertEquals('www.second.com', $request->getHost(), '->getHost() value from X_FORWARDED_HOST with port number');
-
- $request->initialize(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'www.exemple.com', 'HTTP_X_FORWARDED_HOST' => 'www.forward.com'));
- $this->assertEquals('www.forward.com', $request->getHost(), '->getHost() value from X_FORWARDED_HOST has priority over Host');
-
- $request->initialize(array(), array(), array(), array(), array(), array('SERVER_NAME' => 'www.exemple.com', 'HTTP_X_FORWARDED_HOST' => 'www.forward.com'));
- $this->assertEquals('www.forward.com', $request->getHost(), '->getHost() value from X_FORWARDED_HOST has priority over SERVER_NAME ');
-
- $request->initialize(array(), array(), array(), array(), array(), array('SERVER_NAME' => 'www.exemple.com', 'HTTP_HOST' => 'www.host.com'));
- $this->assertEquals('www.host.com', $request->getHost(), '->getHost() value from Host header has priority over SERVER_NAME ');
- }
-
- /**
- * @covers Symfony\Component\HttpFoundation\Request::setMethod
- * @covers Symfony\Component\HttpFoundation\Request::getMethod
- */
- public function testGetSetMethod()
- {
- $request = new Request();
-
- $this->assertEquals('GET', $request->getMethod(), '->getMethod() returns GET if no method is defined');
-
- $request->setMethod('get');
- $this->assertEquals('GET', $request->getMethod(), '->getMethod() returns an uppercased string');
-
- $request->setMethod('PURGE');
- $this->assertEquals('PURGE', $request->getMethod(), '->getMethod() returns the method even if it is not a standard one');
-
- $request->setMethod('POST');
- $this->assertEquals('POST', $request->getMethod(), '->getMethod() returns the method POST if no _method is defined');
-
- $request->setMethod('POST');
- $request->request->set('_method', 'purge');
- $this->assertEquals('PURGE', $request->getMethod(), '->getMethod() returns the method from _method if defined and POST');
-
- $request->setMethod('POST');
- $request->headers->set('X-HTTP-METHOD-OVERRIDE', 'delete');
- $this->assertEquals('DELETE', $request->getMethod(), '->getMethod() returns the method from X-HTTP-Method-Override even though _method is set if defined and POST');
-
- $request = new Request();
- $request->setMethod('POST');
- $request->headers->set('X-HTTP-METHOD-OVERRIDE', 'delete');
- $this->assertEquals('DELETE', $request->getMethod(), '->getMethod() returns the method from X-HTTP-Method-Override if defined and POST');
- }
-
- /**
- * @dataProvider testGetClientIpProvider
- */
- public function testGetClientIp($expected, $proxy, $remoteAddr, $httpClientIp, $httpForwardedFor)
- {
- $request = new Request;
- $this->assertEquals('', $request->getClientIp());
- $this->assertEquals('', $request->getClientIp(true));
-
- $server = array('REMOTE_ADDR' => $remoteAddr);
- if (null !== $httpClientIp) {
- $server['HTTP_CLIENT_IP'] = $httpClientIp;
- }
- if (null !== $httpForwardedFor) {
- $server['HTTP_X_FORWARDED_FOR'] = $httpForwardedFor;
- }
-
- $request->initialize(array(), array(), array(), array(), array(), $server);
- $this->assertEquals($expected, $request->getClientIp($proxy));
- }
-
- public function testGetClientIpProvider()
- {
- return array(
- array('88.88.88.88', false, '88.88.88.88', null, null),
- array('127.0.0.1', false, '127.0.0.1', '88.88.88.88', null),
- array('88.88.88.88', true, '127.0.0.1', '88.88.88.88', null),
- array('127.0.0.1', false, '127.0.0.1', null, '88.88.88.88'),
- array('88.88.88.88', true, '127.0.0.1', null, '88.88.88.88'),
- array('::1', false, '::1', null, null),
- array('2620:0:1cfe:face:b00c::3', true, '::1', '2620:0:1cfe:face:b00c::3', null),
- );
- }
-
- public function testGetContentWorksTwiceInDefaultMode()
- {
- $req = new Request;
- $this->assertEquals('', $req->getContent());
- $this->assertEquals('', $req->getContent());
- }
-
- public function testGetContentReturnsResource()
- {
- $req = new Request;
- $retval = $req->getContent(true);
- $this->assertInternalType('resource', $retval);
- $this->assertEquals("", fread($retval, 1));
- $this->assertTrue(feof($retval));
- }
-
- /**
- * @expectedException LogicException
- * @dataProvider getContentCantBeCalledTwiceWithResourcesProvider
- */
- public function testGetContentCantBeCalledTwiceWithResources($first, $second)
- {
- $req = new Request;
- $req->getContent($first);
- $req->getContent($second);
- }
-
- public function getContentCantBeCalledTwiceWithResourcesProvider()
- {
- return array(
- 'Resource then fetch' => array(true, false),
- 'Resource then resource' => array(true, true),
- 'Fetch then resource' => array(false, true),
- );
- }
-
- public function testCreateFromGlobals()
- {
- $_GET['foo1'] = 'bar1';
- $_POST['foo2'] = 'bar2';
- $_COOKIE['foo3'] = 'bar3';
- $_FILES['foo4'] = array('bar4');
- $_SERVER['foo5'] = 'bar5';
-
- $request = Request::createFromGlobals();
- $this->assertEquals('bar1', $request->query->get('foo1'), '::fromGlobals() uses values from $_GET');
- $this->assertEquals('bar2', $request->request->get('foo2'), '::fromGlobals() uses values from $_POST');
- $this->assertEquals('bar3', $request->cookies->get('foo3'), '::fromGlobals() uses values from $_COOKIE');
- $this->assertEquals(array('bar4'), $request->files->get('foo4'), '::fromGlobals() uses values from $_FILES');
- $this->assertEquals('bar5', $request->server->get('foo5'), '::fromGlobals() uses values from $_SERVER');
-
- unset($_GET['foo1'], $_POST['foo2'], $_COOKIE['foo3'], $_FILES['foo4'], $_SERVER['foo5']);
-
- $_SERVER['REQUEST_METHOD'] = 'PUT';
- $_SERVER['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
- $request = RequestContentProxy::createFromGlobals();
- $this->assertEquals('PUT', $request->getMethod());
- $this->assertEquals('mycontent', $request->request->get('content'));
-
- unset($_SERVER['REQUEST_METHOD'], $_SERVER['CONTENT_TYPE']);
-
- $_POST['_method'] = 'PUT';
- $_POST['foo6'] = 'bar6';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $request = Request::createFromGlobals();
- $this->assertEquals('PUT', $request->getMethod());
- $this->assertEquals('bar6', $request->request->get('foo6'));
-
- unset($_POST['_method'], $_POST['foo6'], $_SERVER['REQUEST_METHOD']);
- }
-
- public function testOverrideGlobals()
- {
- $request = new Request();
- $request->initialize(array('foo' => 'bar'));
-
- // as the Request::overrideGlobals really work, it erase $_SERVER, so we must backup it
- $server = $_SERVER;
-
- $request->overrideGlobals();
-
- $this->assertEquals(array('foo' => 'bar'), $_GET);
-
- $request->initialize(array(), array('foo' => 'bar'));
- $request->overrideGlobals();
-
- $this->assertEquals(array('foo' => 'bar'), $_POST);
-
- $this->assertArrayNotHasKey('HTTP_X_FORWARDED_PROTO', $_SERVER);
-
- $request->headers->set('X_FORWARDED_PROTO', 'https');
-
- $this->assertTrue($request->isSecure());
-
- $request->overrideGlobals();
-
- $this->assertArrayHasKey('HTTP_X_FORWARDED_PROTO', $_SERVER);
-
- // restore initial $_SERVER array
- $_SERVER = $server;
- }
-
- public function testGetScriptName()
- {
- $request = new Request();
- $this->assertEquals('', $request->getScriptName());
-
- $server = array();
- $server['SCRIPT_NAME'] = '/index.php';
-
- $request->initialize(array(), array(), array(), array(), array(), $server);
-
- $this->assertEquals('/index.php', $request->getScriptName());
-
- $server = array();
- $server['ORIG_SCRIPT_NAME'] = '/frontend.php';
- $request->initialize(array(), array(), array(), array(), array(), $server);
-
- $this->assertEquals('/frontend.php', $request->getScriptName());
-
- $server = array();
- $server['SCRIPT_NAME'] = '/index.php';
- $server['ORIG_SCRIPT_NAME'] = '/frontend.php';
- $request->initialize(array(), array(), array(), array(), array(), $server);
-
- $this->assertEquals('/index.php', $request->getScriptName());
- }
-
- public function testGetBasePath()
- {
- $request = new Request();
- $this->assertEquals('', $request->getBasePath());
-
- $server = array();
- $server['SCRIPT_FILENAME'] = '/some/where/index.php';
- $request->initialize(array(), array(), array(), array(), array(), $server);
- $this->assertEquals('', $request->getBasePath());
-
- $server = array();
- $server['SCRIPT_FILENAME'] = '/some/where/index.php';
- $server['SCRIPT_NAME'] = '/index.php';
- $request->initialize(array(), array(), array(), array(), array(), $server);
-
- $this->assertEquals('', $request->getBasePath());
-
- $server = array();
- $server['SCRIPT_FILENAME'] = '/some/where/index.php';
- $server['PHP_SELF'] = '/index.php';
- $request->initialize(array(), array(), array(), array(), array(), $server);
-
- $this->assertEquals('', $request->getBasePath());
-
- $server = array();
- $server['SCRIPT_FILENAME'] = '/some/where/index.php';
- $server['ORIG_SCRIPT_NAME'] = '/index.php';
- $request->initialize(array(), array(), array(), array(), array(), $server);
-
- $this->assertEquals('', $request->getBasePath());
- }
-
- public function testGetPreferredLanguage()
- {
- $request = new Request();
- $this->assertEquals('', $request->getPreferredLanguage());
- $this->assertEquals('fr', $request->getPreferredLanguage(array('fr')));
- $this->assertEquals('fr', $request->getPreferredLanguage(array('fr', 'en')));
- $this->assertEquals('en', $request->getPreferredLanguage(array('en', 'fr')));
- $this->assertEquals('fr-ch', $request->getPreferredLanguage(array('fr-ch', 'fr-fr')));
-
- $request = new Request();
- $request->headers->set('Accept-language', 'zh, en-us; q=0.8, en; q=0.6');
- $this->assertEquals('en', $request->getPreferredLanguage(array('en', 'en-us')));
-
- $request = new Request();
- $request->headers->set('Accept-language', 'zh, en-us; q=0.8, en; q=0.6');
- $this->assertEquals('en', $request->getPreferredLanguage(array('fr', 'en')));
- }
-
- public function testIsXmlHttpRequest()
- {
- $request = new Request();
- $this->assertFalse($request->isXmlHttpRequest());
-
- $request->headers->set('X-Requested-With', 'XMLHttpRequest');
- $this->assertTrue($request->isXmlHttpRequest());
-
- $request->headers->remove('X-Requested-With');
- $this->assertFalse($request->isXmlHttpRequest());
- }
-
- public function testGetCharsets()
- {
- $request = new Request();
- $this->assertEquals(array(), $request->getCharsets());
- $request->headers->set('Accept-Charset', 'ISO-8859-1, US-ASCII, UTF-8; q=0.8, ISO-10646-UCS-2; q=0.6');
- $this->assertEquals(array(), $request->getCharsets()); // testing caching
-
- $request = new Request();
- $request->headers->set('Accept-Charset', 'ISO-8859-1, US-ASCII, UTF-8; q=0.8, ISO-10646-UCS-2; q=0.6');
- $this->assertEquals(array('ISO-8859-1', 'US-ASCII', 'UTF-8', 'ISO-10646-UCS-2'), $request->getCharsets());
-
- $request = new Request();
- $request->headers->set('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7');
- $this->assertEquals(array('ISO-8859-1', '*', 'utf-8'), $request->getCharsets());
- }
-
- public function testGetAcceptableContentTypes()
- {
- $request = new Request();
- $this->assertEquals(array(), $request->getAcceptableContentTypes());
- $request->headers->set('Accept', 'application/vnd.wap.wmlscriptc, text/vnd.wap.wml, application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html, multipart/mixed, */*');
- $this->assertEquals(array(), $request->getAcceptableContentTypes()); // testing caching
-
- $request = new Request();
- $request->headers->set('Accept', 'application/vnd.wap.wmlscriptc, text/vnd.wap.wml, application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html, multipart/mixed, */*');
- $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());
- }
-
- public function testGetLanguages()
- {
- $request = new Request();
- $this->assertEquals(array(), $request->getLanguages());
-
- $request = new Request();
- $request->headers->set('Accept-language', 'zh, en-us; q=0.8, en; q=0.6');
- $this->assertEquals(array('zh', 'en_US', 'en'), $request->getLanguages());
- $this->assertEquals(array('zh', 'en_US', 'en'), $request->getLanguages());
-
- $request = new Request();
- $request->headers->set('Accept-language', 'zh, i-cherokee; q=0.6');
- $this->assertEquals(array('zh', 'cherokee'), $request->getLanguages());
- }
-
- public function testGetRequestFormat()
- {
- $request = new Request();
- $this->assertEquals('html', $request->getRequestFormat());
-
- $request = new Request();
- $this->assertEquals(null, $request->getRequestFormat(null));
-
- $request = new Request();
- $this->assertEquals(null, $request->setRequestFormat('foo'));
- $this->assertEquals('foo', $request->getRequestFormat(null));
- }
-
- public function testForwardedSecure()
- {
- $request = new Request();
- $request->headers->set('X-Forwarded-Proto', 'https');
- $request->headers->set('X-Forwarded-Port', 443);
-
- $this->assertTrue($request->isSecure());
- $this->assertEquals(443, $request->getPort());
- }
-
- public function testHasSession()
- {
- $request = new Request;
-
- $this->assertFalse($request->hasSession());
- $request->setSession(new Session(new ArraySessionStorage()));
- $this->assertTrue($request->hasSession());
- }
-
- public function testHasPreviousSession()
- {
- $request = new Request;
-
- $this->assertFalse($request->hasPreviousSession());
- $request->cookies->set(session_name(), 'foo');
- $this->assertFalse($request->hasPreviousSession());
- $request->setSession(new Session(new ArraySessionStorage()));
- $this->assertTrue($request->hasPreviousSession());
- }
-
- public function testToString()
- {
- $request = new Request();
-
- $request->headers->set('Accept-language', 'zh, en-us; q=0.8, en; q=0.6');
-
- $this->assertContains('Accept-Language: zh, en-us; q=0.8, en; q=0.6', $request->__toString());
- }
- }
-
- class RequestContentProxy extends Request
- {
- public function getContent($asResource = false)
- {
- return http_build_query(array('_method' => 'PUT', 'content' => 'mycontent'));
- }
- }
|