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.


In reply to [Solved] Testing Dancer2 - simulating requests from different IP addresses by davies

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.