@@ -706,7 +706,10 @@ public function testGetIPAddressNormal()
706706 {
707707 $ expected = '123.123.123.123 ' ;
708708 $ _SERVER ['REMOTE_ADDR ' ] = $ expected ;
709- $ this ->request = new Request (new App ());
709+
710+ $ this ->request = new Request (new App ());
711+ $ this ->request ->populateHeaders ();
712+
710713 $ this ->assertSame ($ expected , $ this ->request ->getIPAddress ());
711714 // call a second time to exercise the initial conditional block in getIPAddress()
712715 $ this ->assertSame ($ expected , $ this ->request ->getIPAddress ());
@@ -715,66 +718,75 @@ public function testGetIPAddressNormal()
715718 public function testGetIPAddressThruProxy ()
716719 {
717720 $ expected = '123.123.123.123 ' ;
721+ $ _SERVER ['HTTP_X_FORWARDED_FOR ' ] = $ expected ;
718722 $ _SERVER ['REMOTE_ADDR ' ] = '10.0.1.200 ' ;
719723 $ config = new App ();
720724 $ config ->proxyIPs = '10.0.1.200,192.168.5.0/24 ' ;
721- $ _SERVER ['HTTP_X_FORWARDED_FOR ' ] = $ expected ;
722- $ this ->request = new Request ($ config );
725+
726+ $ this ->request = new Request ($ config );
727+ $ this ->request ->populateHeaders ();
723728
724729 // we should see the original forwarded address
725730 $ this ->assertSame ($ expected , $ this ->request ->getIPAddress ());
726731 }
727732
728733 public function testGetIPAddressThruProxyInvalid ()
729734 {
730- $ expected = '123.456.23.123 ' ;
731- $ _SERVER ['REMOTE_ADDR ' ] = '10.0.1.200 ' ;
735+ $ _SERVER ['HTTP_X_FORWARDED_FOR ' ] = '123.456.23.123 ' ;
736+ $ expected = '10.0.1.200 ' ;
737+ $ _SERVER ['REMOTE_ADDR ' ] = $ expected ;
732738 $ config = new App ();
733739 $ config ->proxyIPs = '10.0.1.200,192.168.5.0/24 ' ;
734- $ _SERVER ['HTTP_X_FORWARDED_FOR ' ] = $ expected ;
735- $ this ->request = new Request ($ config );
740+
741+ $ this ->request = new Request ($ config );
742+ $ this ->request ->populateHeaders ();
736743
737744 // spoofed address invalid
738- $ this ->assertSame (' 10.0.1.200 ' , $ this ->request ->getIPAddress ());
745+ $ this ->assertSame ($ expected , $ this ->request ->getIPAddress ());
739746 }
740747
741748 public function testGetIPAddressThruProxyNotWhitelisted ()
742749 {
743- $ expected = '123.456.23.123 ' ;
744- $ _SERVER ['REMOTE_ADDR ' ] = '10.10.1.200 ' ;
750+ $ expected = '10.10.1.200 ' ;
751+ $ _SERVER ['REMOTE_ADDR ' ] = $ expected ;
752+ $ _SERVER ['HTTP_X_FORWARDED_FOR ' ] = '123.456.23.123 ' ;
745753 $ config = new App ();
746754 $ config ->proxyIPs = '10.0.1.200,192.168.5.0/24 ' ;
747- $ _SERVER ['HTTP_X_FORWARDED_FOR ' ] = $ expected ;
748- $ this ->request = new Request ($ config );
755+
756+ $ this ->request = new Request ($ config );
757+ $ this ->request ->populateHeaders ();
749758
750759 // spoofed address invalid
751- $ this ->assertSame (' 10.10.1.200 ' , $ this ->request ->getIPAddress ());
760+ $ this ->assertSame ($ expected , $ this ->request ->getIPAddress ());
752761 }
753762
754763 public function testGetIPAddressThruProxySubnet ()
755764 {
756765 $ expected = '123.123.123.123 ' ;
766+ $ _SERVER ['HTTP_X_FORWARDED_FOR ' ] = $ expected ;
757767 $ _SERVER ['REMOTE_ADDR ' ] = '192.168.5.21 ' ;
758768 $ config = new App ();
759769 $ config ->proxyIPs = ['192.168.5.0/24 ' ];
760- $ _SERVER ['HTTP_X_FORWARDED_FOR ' ] = $ expected ;
761- $ this ->request = new Request ($ config );
770+
771+ $ this ->request = new Request ($ config );
772+ $ this ->request ->populateHeaders ();
762773
763774 // we should see the original forwarded address
764775 $ this ->assertSame ($ expected , $ this ->request ->getIPAddress ());
765776 }
766777
767778 public function testGetIPAddressThruProxyOutofSubnet ()
768779 {
769- $ expected = '123.123.123.123 ' ;
770- $ _SERVER ['REMOTE_ADDR ' ] = ' 192.168.5.21 ' ;
780+ $ expected = '192.168.5.21 ' ;
781+ $ _SERVER ['REMOTE_ADDR ' ] = $ expected ;
771782 $ config = new App ();
772783 $ config ->proxyIPs = ['192.168.5.0/28 ' ];
773- $ _SERVER ['HTTP_X_FORWARDED_FOR ' ] = $ expected ;
784+ $ _SERVER ['HTTP_X_FORWARDED_FOR ' ] = ' 123.123.123.123 ' ;
774785 $ this ->request = new Request ($ config );
786+ $ this ->request ->populateHeaders ();
775787
776788 // we should see the original forwarded address
777- $ this ->assertSame (' 192.168.5.21 ' , $ this ->request ->getIPAddress ());
789+ $ this ->assertSame ($ expected , $ this ->request ->getIPAddress ());
778790 }
779791
780792 // @TODO getIPAddress should have more testing, to 100% code coverage
0 commit comments