Skip to content

Commit 41bf849

Browse files
pborrelifabpot
authored andcommitted
[HttpFoundation] Request coverage
1 parent f3b6e1a commit 41bf849

File tree

1 file changed

+191
-1
lines changed

1 file changed

+191
-1
lines changed

tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php

Lines changed: 191 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,37 @@ public function testCreate()
5454
$this->assertEquals('/foo', $request->getPathInfo());
5555
$this->assertEquals('bar=baz', $request->getQueryString());
5656
$this->assertEquals(80, $request->getPort());
57+
$this->assertEquals('test.com', $request->getHttpHost());
58+
$this->assertFalse($request->isSecure());
5759

5860
$request = Request::create('https://test.com/foo?bar=baz');
5961
$this->assertEquals('https://test.com/foo?bar=baz', $request->getUri());
6062
$this->assertEquals('/foo', $request->getPathInfo());
6163
$this->assertEquals('bar=baz', $request->getQueryString());
6264
$this->assertEquals(443, $request->getPort());
65+
$this->assertEquals('test.com', $request->getHttpHost());
66+
$this->assertTrue($request->isSecure());
6367

6468
$request = Request::create('test.com:90/foo');
6569
$this->assertEquals('http://test.com:90/foo', $request->getUri());
6670
$this->assertEquals('/foo', $request->getPathInfo());
6771
$this->assertEquals('test.com', $request->getHost());
72+
$this->assertEquals('test.com:90', $request->getHttpHost());
6873
$this->assertEquals(90, $request->getPort());
74+
$this->assertFalse($request->isSecure());
6975

7076
$request = Request::create('https://test.com:90/foo');
7177
$this->assertEquals('https://test.com:90/foo', $request->getUri());
7278
$this->assertEquals('/foo', $request->getPathInfo());
7379
$this->assertEquals('test.com', $request->getHost());
80+
$this->assertEquals('test.com:90', $request->getHttpHost());
7481
$this->assertEquals(90, $request->getPort());
82+
$this->assertTrue($request->isSecure());
7583

7684
$json = '{"jsonrpc":"2.0","method":"echo","id":7,"params":["Hello World"]}';
7785
$request = Request::create('http://example.com/jsonrpc', 'POST', array(), array(), array(), array(), $json);
7886
$this->assertEquals($json, $request->getContent());
87+
$this->assertFalse($request->isSecure());
7988
}
8089

8190
/**
@@ -101,6 +110,7 @@ public function testDuplicate()
101110

102111
/**
103112
* @covers Symfony\Component\HttpFoundation\Request::getFormat
113+
* @covers Symfony\Component\HttpFoundation\Request::setFormat
104114
* @dataProvider getFormatToMimeTypeMapProvider
105115
*/
106116
public function testGetFormatFromMimeType($format, $mimeTypes)
@@ -109,7 +119,10 @@ public function testGetFormatFromMimeType($format, $mimeTypes)
109119
foreach ($mimeTypes as $mime) {
110120
$this->assertEquals($format, $request->getFormat($mime));
111121
}
112-
122+
$request->setFormat($format, $mimeTypes);
123+
foreach ($mimeTypes as $mime) {
124+
$this->assertEquals($format, $request->getFormat($mime));
125+
}
113126
}
114127

115128
/**
@@ -322,6 +335,7 @@ public function testGetUriForPath()
322335
$request->initialize(array(), array(), array(), array(), array(), $server);
323336

324337
$this->assertEquals('http://hostname/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with rewrite, default port without HOST_HEADER');
338+
$this->assertEquals('hostname', $request->getHttpHost());
325339
}
326340

327341
/**
@@ -420,6 +434,23 @@ public function testGetSetMethod()
420434
$this->assertEquals('PURGE', $request->getMethod(), '->getMethod() returns the method from _method if defined and POST');
421435
}
422436

437+
public function testGetClientIp()
438+
{
439+
$request = new Request;
440+
$this->assertEquals('', $request->getClientIp());
441+
$this->assertEquals('', $request->getClientIp(true));
442+
$request->initialize(array(), array(), array(), array(), array(), array('REMOTE_ADDR' => '88.88.88.88'));
443+
$this->assertEquals('88.88.88.88', $request->getClientIp());
444+
$request->initialize(array(), array(), array(), array(), array(), array('REMOTE_ADDR' => '127.0.0.1', 'HTTP_CLIENT_IP' => '88.88.88.88'));
445+
$this->assertEquals('127.0.0.1', $request->getClientIp());
446+
$request->initialize(array(), array(), array(), array(), array(), array('REMOTE_ADDR' => '127.0.0.1', 'HTTP_CLIENT_IP' => '88.88.88.88'));
447+
$this->assertEquals('88.88.88.88', $request->getClientIp(true));
448+
$request->initialize(array(), array(), array(), array(), array(), array('REMOTE_ADDR' => '127.0.0.1', 'HTTP_X_FORWARDED_FOR' => '88.88.88.88'));
449+
$this->assertEquals('127.0.0.1', $request->getClientIp());
450+
$request->initialize(array(), array(), array(), array(), array(), array('REMOTE_ADDR' => '127.0.0.1', 'HTTP_X_FORWARDED_FOR' => '88.88.88.88'));
451+
$this->assertEquals('88.88.88.88', $request->getClientIp(true));
452+
}
453+
423454
public function testGetContentWorksTwiceInDefaultMode()
424455
{
425456
$req = new Request;
@@ -473,4 +504,163 @@ public function testCreateFromGlobals()
473504

474505
unset($_GET['foo1'], $_POST['foo2'], $_COOKIE['foo3'], $_FILES['foo4'], $_SERVER['foo5']);
475506
}
507+
508+
public function testOverrideGlobals()
509+
{
510+
$time = $_SERVER['REQUEST_TIME']; // fix for phpunit timer
511+
512+
$request = new Request();
513+
$request->initialize(array('foo' => 'bar'));
514+
$request->overrideGlobals();
515+
516+
$this->assertEquals(array('foo' => 'bar'), $_GET);
517+
518+
$request->initialize(array(), array('foo' => 'bar'));
519+
$request->overrideGlobals();
520+
521+
$this->assertEquals(array('foo' => 'bar'), $_POST);
522+
523+
$this->assertArrayNotHasKey('HTTP_X_FORWARDED_PROTO', $_SERVER);
524+
525+
$request->headers->set('X_FORWARDED_PROTO', 'https');
526+
527+
$this->assertTrue($request->isSecure());
528+
529+
$request->overrideGlobals();
530+
531+
$this->assertArrayHasKey('HTTP_X_FORWARDED_PROTO', $_SERVER);
532+
533+
$_SERVER['REQUEST_TIME'] = $time; // fix for phpunit timer
534+
}
535+
536+
public function testGetScriptName()
537+
{
538+
$request = new Request();
539+
$this->assertEquals('', $request->getScriptName());
540+
541+
$server = array();
542+
$server['SCRIPT_NAME'] = '/index.php';
543+
544+
$request->initialize(array(), array(), array(), array(), array(), $server);
545+
546+
$this->assertEquals('/index.php', $request->getScriptName());
547+
548+
$server = array();
549+
$server['ORIG_SCRIPT_NAME'] = '/frontend.php';
550+
$request->initialize(array(), array(), array(), array(), array(), $server);
551+
552+
$this->assertEquals('/frontend.php', $request->getScriptName());
553+
554+
$server = array();
555+
$server['SCRIPT_NAME'] = '/index.php';
556+
$server['ORIG_SCRIPT_NAME'] = '/frontend.php';
557+
$request->initialize(array(), array(), array(), array(), array(), $server);
558+
559+
$this->assertEquals('/index.php', $request->getScriptName());
560+
}
561+
562+
public function testGetBasePath()
563+
{
564+
$request = new Request();
565+
$this->assertEquals('', $request->getBasePath());
566+
567+
$server = array();
568+
$server['SCRIPT_FILENAME'] = '/some/where/index.php';
569+
$request->initialize(array(), array(), array(), array(), array(), $server);
570+
$this->assertEquals('', $request->getBasePath());
571+
572+
$server = array();
573+
$server['SCRIPT_FILENAME'] = '/some/where/index.php';
574+
$server['SCRIPT_NAME'] = '/index.php';
575+
$request->initialize(array(), array(), array(), array(), array(), $server);
576+
577+
$this->assertEquals('', $request->getBasePath());
578+
579+
$server = array();
580+
$server['SCRIPT_FILENAME'] = '/some/where/index.php';
581+
$server['PHP_SELF'] = '/index.php';
582+
$request->initialize(array(), array(), array(), array(), array(), $server);
583+
584+
$this->assertEquals('', $request->getBasePath());
585+
586+
$server = array();
587+
$server['SCRIPT_FILENAME'] = '/some/where/index.php';
588+
$server['ORIG_SCRIPT_NAME'] = '/index.php';
589+
$request->initialize(array(), array(), array(), array(), array(), $server);
590+
591+
$this->assertEquals('', $request->getBasePath());
592+
}
593+
594+
public function testGetPreferredLanguage()
595+
{
596+
$request = new Request();
597+
$this->assertEquals('', $request->getPreferredLanguage());
598+
$this->assertEquals('fr', $request->getPreferredLanguage(array('fr')));
599+
$this->assertEquals('fr', $request->getPreferredLanguage(array('fr', 'en')));
600+
$this->assertEquals('en', $request->getPreferredLanguage(array('en', 'fr')));
601+
$this->assertEquals('fr-ch', $request->getPreferredLanguage(array('fr-ch', 'fr-fr')));
602+
603+
$request = new Request();
604+
$request->headers->set('Accept-language', 'zh, en-us; q=0.8, en; q=0.6');
605+
$this->assertEquals('en', $request->getPreferredLanguage(array('en', 'en-us')));
606+
607+
$request = new Request();
608+
$request->headers->set('Accept-language', 'zh, en-us; q=0.8, en; q=0.6');
609+
$this->assertEquals('en', $request->getPreferredLanguage(array('fr', 'en')));
610+
}
611+
612+
public function testIsXmlHttpRequest()
613+
{
614+
$request = new Request();
615+
$this->assertFalse($request->isXmlHttpRequest());
616+
617+
$request->headers->set('X-Requested-With', 'XMLHttpRequest');
618+
$this->assertTrue($request->isXmlHttpRequest());
619+
620+
$request->headers->remove('X-Requested-With');
621+
$this->assertFalse($request->isXmlHttpRequest());
622+
}
623+
624+
public function testGetCharsets()
625+
{
626+
$request = new Request();
627+
$this->assertEquals(array(), $request->getCharsets());
628+
$request->headers->set('Accept-Charset', 'ISO-8859-1, US-ASCII, UTF-8; q=0.8, ISO-10646-UCS-2; q=0.6');
629+
$this->assertEquals(array(), $request->getCharsets()); // testing caching
630+
631+
$request = new Request();
632+
$request->headers->set('Accept-Charset', 'ISO-8859-1, US-ASCII, UTF-8; q=0.8, ISO-10646-UCS-2; q=0.6');
633+
$this->assertEquals(array('ISO-8859-1', 'US-ASCII', 'UTF-8', 'ISO-10646-UCS-2'), $request->getCharsets());
634+
635+
$request = new Request();
636+
$request->headers->set('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7');
637+
$this->assertEquals(array('ISO-8859-1', '*', 'utf-8'), $request->getCharsets());
638+
}
639+
640+
public function testGetAcceptableContentTypes()
641+
{
642+
$request = new Request();
643+
$this->assertEquals(array(), $request->getAcceptableContentTypes());
644+
$request->headers->set('Accept', 'application/vnd.wap.wmlscriptc, text/vnd.wap.wml, application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html, multipart/mixed, */*');
645+
$this->assertEquals(array(), $request->getAcceptableContentTypes()); // testing caching
646+
647+
$request = new Request();
648+
$request->headers->set('Accept', 'application/vnd.wap.wmlscriptc, text/vnd.wap.wml, application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html, multipart/mixed, */*');
649+
$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());
650+
}
651+
652+
public function testGetLanguages()
653+
{
654+
$request = new Request();
655+
$this->assertNull($request->getLanguages());
656+
657+
$request = new Request();
658+
$request->headers->set('Accept-language', 'zh, en-us; q=0.8, en; q=0.6');
659+
$this->assertEquals(array('zh', 'en_US', 'en'), $request->getLanguages());
660+
$this->assertEquals(array('zh', 'en_US', 'en'), $request->getLanguages());
661+
662+
$request = new Request();
663+
$request->headers->set('Accept-language', 'zh, i-cherokee; q=0.6');
664+
$this->assertEquals(array('zh', 'cherokee'), $request->getLanguages());
665+
}
476666
}

0 commit comments

Comments
 (0)