19
19
20
20
class RequestTest extends \PHPUnit_Framework_TestCase
21
21
{
22
- public function setUp ()
23
- {
24
- Request::trustProxyData ();
25
- }
26
-
27
22
/**
28
23
* @covers Symfony\Component\HttpFoundation\Request::__construct
29
24
*/
@@ -430,15 +425,17 @@ public function testGetHost()
430
425
$ request ->initialize (array (), array (), array (), array (), array (), array ('HTTP_HOST ' => 'www.exemple.com ' ));
431
426
$ this ->assertEquals ('www.exemple.com ' , $ request ->getHost (), '->getHost() from Host Header ' );
432
427
433
- // Host header with port number.
428
+ // Host header with port number
434
429
$ request ->initialize (array (), array (), array (), array (), array (), array ('HTTP_HOST ' => 'www.exemple.com:8080 ' ));
435
430
$ this ->assertEquals ('www.exemple.com ' , $ request ->getHost (), '->getHost() from Host Header with port number ' );
436
431
437
- // Server values.
432
+ // Server values
438
433
$ request ->initialize (array (), array (), array (), array (), array (), array ('SERVER_NAME ' => 'www.exemple.com ' ));
439
434
$ this ->assertEquals ('www.exemple.com ' , $ request ->getHost (), '->getHost() from server name ' );
440
435
441
- // X_FORWARDED_HOST.
436
+ Request::setTrustedProxies (array ('1.1.1.1 ' ));
437
+
438
+ // X_FORWARDED_HOST
442
439
$ request ->initialize (array (), array (), array (), array (), array (), array ('HTTP_X_FORWARDED_HOST ' => 'www.exemple.com ' ));
443
440
$ this ->assertEquals ('www.exemple.com ' , $ request ->getHost (), '->getHost() from X_FORWARDED_HOST ' );
444
441
@@ -458,6 +455,8 @@ public function testGetHost()
458
455
459
456
$ request ->initialize (array (), array (), array (), array (), array (), array ('SERVER_NAME ' => 'www.exemple.com ' , 'HTTP_HOST ' => 'www.host.com ' ));
460
457
$ this ->assertEquals ('www.host.com ' , $ request ->getHost (), '->getHost() value from Host header has priority over SERVER_NAME ' );
458
+
459
+ Request::setTrustedProxies (array ());
461
460
}
462
461
463
462
/**
@@ -496,31 +495,35 @@ public function testGetSetMethod()
496
495
/**
497
496
* @dataProvider testGetClientIpProvider
498
497
*/
499
- public function testGetClientIp ($ expected , $ proxy , $ remoteAddr , $ httpForwardedFor )
498
+ public function testGetClientIp ($ expected , $ proxy , $ remoteAddr , $ httpForwardedFor, $ trustedProxies )
500
499
{
501
- $ request = new Request ;
502
- $ this ->assertEquals ('' , $ request ->getClientIp ());
503
- $ this ->assertEquals ('' , $ request ->getClientIp (true ));
500
+ $ request = new Request ();
504
501
505
502
$ server = array ('REMOTE_ADDR ' => $ remoteAddr );
506
503
if (null !== $ httpForwardedFor ) {
507
504
$ server ['HTTP_X_FORWARDED_FOR ' ] = $ httpForwardedFor ;
508
505
}
509
506
507
+ if ($ proxy || $ trustedProxies ) {
508
+ Request::setTrustedProxies (null === $ trustedProxies ? array ($ remoteAddr ) : $ trustedProxies );
509
+ }
510
+
510
511
$ request ->initialize (array (), array (), array (), array (), array (), $ server );
511
512
$ this ->assertEquals ($ expected , $ request ->getClientIp ($ proxy ));
512
513
}
513
514
514
515
public function testGetClientIpProvider ()
515
516
{
516
517
return array (
517
- array ('88.88.88.88 ' , false , '88.88.88.88 ' , null ),
518
- array ('127.0.0.1 ' , false , '127.0.0.1 ' , null ),
519
- array ('127.0.0.1 ' , false , '127.0.0.1 ' , '88.88.88.88 ' ),
520
- array ('88.88.88.88 ' , true , '127.0.0.1 ' , '88.88.88.88 ' ),
521
- array ('::1 ' , false , '::1 ' , null ),
522
- array ('2620:0:1cfe:face:b00c::3 ' , true , '::1 ' , '2620:0:1cfe:face:b00c::3, ::1 ' ),
523
- array ('88.88.88.88 ' , true , '123.45.67.89 ' , '88.88.88.88, 87.65.43.21, 127.0.0.1 ' ),
518
+ array ('88.88.88.88 ' , false , '88.88.88.88 ' , null , null ),
519
+ array ('127.0.0.1 ' , false , '127.0.0.1 ' , null , null ),
520
+ array ('::1 ' , false , '::1 ' , null , null ),
521
+ array ('127.0.0.1 ' , false , '127.0.0.1 ' , '88.88.88.88 ' , null ),
522
+ array ('88.88.88.88 ' , true , '127.0.0.1 ' , '88.88.88.88 ' , null ),
523
+ array ('2620:0:1cfe:face:b00c::3 ' , true , '::1 ' , '2620:0:1cfe:face:b00c::3 ' , null ),
524
+ array ('88.88.88.88 ' , true , '123.45.67.89 ' , '127.0.0.1, 87.65.43.21, 88.88.88.88 ' , null ),
525
+ array ('87.65.43.21 ' , true , '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 ' )),
526
+ array ('87.65.43.21 ' , false , '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 ' )),
524
527
);
525
528
}
526
529
0 commit comments