2

How to simulate different location while testing localhost ?

I am using the java play framework, where I get the users IP Address when he posts a form. While testing, I save the address and all I get is the localhost 0:0:0:0:0:0:0:1%0. I want to be able to test this in a variety of locations, and use different IP addresses to test this and get the user's location from the IP address.

What would be the best way to go about this ?

Legolas
  • 12,145
  • 12
  • 79
  • 132

2 Answers2

1

If you know the structure the IP is read from you can simulate this and test it with different IPS you set. But why do you need that? if your testing localhost and its working it seems you can read the IP in the right way.

If you are doing anything with the IP like determining the country its comming from for example, you can test this using constant IPs, but testing the IP with different hosts is to test the function to get the IP which is actually a basic thing i wouldnt personally test.

From the comments:

You can use something like this: hostip.info/use.html

Legolas
  • 12,145
  • 12
  • 79
  • 132
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
  • I need the IP to pin-point to the location. For example San Fransisco, Atlanta, or Dallas. – Legolas Sep 11 '12 at 05:51
  • How do you locate the IPs? Is it your own code? if not i dont think its your responsibility to test it too, if yes then test your logic using dummy IPs. – CloudyMarble Sep 11 '12 at 06:08
  • 1
    Testing 3rd party libraries would coast to match the tests eachtime they change their logic, you can try for example to locate a google server or any known server which doesnt change its location, but dont get into testing each location as the logic is the same. – CloudyMarble Sep 11 '12 at 06:09
  • Thats a valid point. How do Twitter / Facebook get your location with your post ? – Legolas Sep 11 '12 at 06:23
  • 1
    They use geolocation systems. http://compnetworking.about.com/od/traceipaddresses/f/ip_location.htm – CloudyMarble Sep 11 '12 at 06:34
  • Take alook at this http://stackoverflow.com/questions/1996106/how-does-ip-geolocating-work – CloudyMarble Sep 11 '12 at 06:34
  • Thats awesome. Can I just use geobytes and parse the tags ? – Legolas Sep 11 '12 at 07:45
1

The answer to the question depends on how you are retrieving the IP address in the first place. If you are behind a reverse proxy such as a load balancer, the original IP might be in the HTTP header. If so, you can test as follows:

Result result = callAction(routes.ref.YourController.yourMethod(),
            new FakeRequest().withHeader(IP_ADDRESS, testIPAddress));
Sean Brady
  • 23
  • 3