Create an IP address for a specific region for testing

For the project I'm working on, I need to fake an IP address so that I come from another country.

Basically, if an IP address from the UK is found, display pop-up A, if an IP address is found outside the UK, then display pop-up B.

I would like to be able to automate this. Currently, all of our browser automation tests are written in cucumber / watir, and I would like to get a solution using the same structure.

Does anyone know of a possible way to do this?

Thanks!

+7
source share
4 answers

Generally speaking, if you care about returning your HTTP request, IP spoofing is not possible.

http://en.wikipedia.org/wiki/IP_address_spoofing

If you need it for testing, you can run a virtual machine on your local network for each use case "in the UK" and "not in the UK." Assign a local IP address to each virtual machine that does not change.

Launch an HTTP proxy server in each virtual machine and connect your unit test through different proxies depending on the test case.

Add redefinition logic to your geolocation code to review these well-known local IP addresses to be part of their respective test locations. Please note that a proxy server with good behavior will include both the IP address of the proxy server and the source IP address in the request. Make sure that you use the IP address of the proxy server for geolocation for this unit test (typically, you would like to use the source IP address available for geolocation of production).

Please note that everyone who needs to get around this type of control is very easy. It is possible to access an HTTP proxy server in the UK, which intentionally does not forward the original IP address for a very small fee.

+4
source

The easiest solution is to add a magic query parameter that allows you to switch between the UK and others. For example, let's say something like this will allow the "UK regime":

browser.goto "localhost/?country=UK" 

Then on the server side there should be some if-statement to check this parameter. In pseudo code:

 if get_parameters["country"] == "UK" enable_uk_mode end 

This will allow you to simply indicate that the GET parameter to enable the UK mode or some other mode. I usually go along this route, since installing and managing all VM and proxy servers and managing their configuration takes much longer than trying to add some control logic to the development mode of your application.

+1
source

Depending on how the detection is made, in some cases you should use the browser language. This kind of code:

 profile = Selenium::WebDriver::Firefox::Profile.new profile['intl.accept_languages'] = "fr" browser = Watir::Browser.new :firefox, :profile => profile 

For ip, why not consider using a proxy?

 profile = Selenium::WebDriver::Firefox::Profile.new profile.proxy = Selenium::WebDriver::Proxy.new :http => 'my.proxy.com:8080', :ssl => 'my.proxy.com:8080' browser = Watir::Browser.new :firefox, :profile => profile 

I think you should do both: this will be more since the real user used his browser.

0
source

You can add the "FakeIP" cookie, which your HTTP request handler checks, and if the application is in test mode, it overwrites the real IP address with a fake IP address.

Example. I really do this in my Play Framework / Scala application. In my HTTP request class:

 def ip = if (!Play.isTest) underlyingRequest.remoteAddress else underlyingRequest.cookies.get("FakeIP").map(_.value).getOrElse( underlyingRequest.remoteAddress) 

And then at the top of my Selenium E2E test:

 add cookie ("FakeIP", "111.112.113.114") 

There's not much more than that :-) (at least in my case)

0
source

All Articles