Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

[Solved] Testing Dancer2 - simulating requests from different IP addresses

by davies (Parson)
on Apr 09, 2016 at 13:48 UTC ( #1160000=perlquestion: print w/replies, xml ) Need Help??
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.

Replies are listed 'Best First'.
Re: Testing Dancer2 - simulating requests from different IP addresses
by beech (Vicar) on Apr 09, 2016 at 18:38 UTC
Re: Testing Dancer2 - simulating requests from different IP addresses
by Discipulus (Monsignor) on Apr 09, 2016 at 19:01 UTC
    I only guessing but: probably you cannot manipulate the source address at that level ie HTTP::Request or LWP::UserAgent In fact due to the aim of these modules i hope they make you able to choose what IP to use to make the request if that IP is one of yours (guessing again).

    I think that probably you need to make up a fake IP packet instead, perhaps using Net::RawIP

    See also How can I simulate multiple IP/MAC combinations on one computer?

    I'm interested in your result

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: [Solved] Testing Dancer2 - simulating requests from different IP addresses
by Discipulus (Monsignor) on Apr 09, 2016 at 20:33 UTC
    can yo be so kind to explain how the code by Adam Taylor you linked to works? Is this just a way to override the package variable $Dancer2::Core::Request::address with an hardcoded value?

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Sorry to take so long to reply. I may not be the best person, either, as this is a technique I have never used before. As a result, everything here is "I believe" and may not be reliable. I'd be interested in any corrections.

      I think you are right that it is simply the overriding of Dancer's existing code. I was getting data out of Dancer by a far more complicated route, but eventually there were only four lines that I incorporated. The first was the call in the code I am developing that became request->address instead of ${request->env}{REMOTE_ADDR}. The second was the three lines

      local *Dancer2::Core::Request::address = sub { return '127.0.0.2'; };

      that went into the test file. This resulted in Dancer behaving according to the hard coded IP addresses in the test file instead of the IP address of the machine running the tests. Everything in MY garden is roses, but I offer no warranties regarding yours!

      Regards,

      John Davies

        Dont worry and thanks! what "you believe" and you tested is anyway relevant and useful for others.

        L*

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Testing Dancer2 - simulating requests from different IP addresses
by BrowserUk (Pope) on Apr 09, 2016 at 18:40 UTC

    My guess, and it's nothing more, is that they set the Host: header in the HTTP::Response to be either LocalHost or 127.0.0.1.

    Worth a look.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Testing Dancer2 - simulating requests from different IP addresses
by 1nickt (Prior) on Apr 09, 2016 at 16:19 UTC

    Perhaps you could add logic to your app that reads an additional attribute or environment variable ( e.g. $args{'test_ip_addr'}, $ENV{'MY_APP_TEST_IP'} ) and uses it, if present, instead of the real IP.

    Hope this helps!


    The way forward always starts with a minimal test.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1160000]
Approved by stevieb
Front-paged by Discipulus
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2017-11-18 13:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (277 votes). Check out past polls.

    Notices?