http://www.perlmonks.org?node_id=1160000

davies has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a web app that is intended to give different responses depending on the IP address that originates the request. I can't find any documentation on how to do this. The Dancer2 documentation suggests using Plack::Test. This indicates that simulating IP addresses should be possible:

For your convenience, the HTTP::Request given to the callback automatically uses the HTTP protocol and the localhost (127.0.0.1 by default)
Unfortunately, I cannot find anything in the HTTP::Request documentation to indicate how to change this default. The sort of code I have in my test file is taken from the Dancer2 docs:

# "GET" from HTTP::Request::Common creates an HTTP::Request object my $response = $test->request( GET '/' ); ok( $response->is_success, 'Successful request' ); my $html = $response->content;

I am able to work with the response perfectly happily and get passing tests. What I can't do is test the different responses to different IP addresses that I want to achieve. Any pointers would be greatly appreciated.

Regards,

John Davies

Update: I tried $ENV{REMOTE_ADDR} = '127.0.0.254';, which had no effect.

Update 2: Adam Taylor of Adzuna isn't a monk but emailed me a solution involving monkey patching. He has posted it at https://gist.github.com/adamtaylor/68a6a2c9a7e468895815c6ef718fcc0a. It works perfectly for me.