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

can yo be so kind to explain how the code by Adam Taylor you linked to works?
# dancer code package ip_address_test; use Dancer2; our $VERSION = '0.1'; get '/' => sub { template 'index'; }; get '/ip' => sub { my $ip = $ENV{REMOTE_ADDR} . ""; return "IP == $ip"; }; get '/test' => sub { return "Hello world"; }; get '/env' => sub { use Data::Dumper; #warn Dumper request->env; warn ref request; warn request->remote_address; return request->address; }; true; # test code with monkey patch use strict; use warnings; use ip_address_test; use Test::More tests => 2; use Plack::Test; use HTTP::Request::Common; my $app = ip_address_test->to_app; is( ref $app, 'CODE', 'Got app' ); my $test = Plack::Test->create($app); # overriding the address() method local *Dancer2::Core::Request::address = sub { return '127.0.0.2'; }; my $res = $test->request( GET '/env' ); ok( $res->is_success, '[GET /] successful' ); note $res->decoded_content;
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.

Replies are listed 'Best First'.
Re^2: [Solved] Testing Dancer2 - simulating requests from different IP addresses
by davies (Monsignor) on Apr 15, 2016 at 12:06 UTC

    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.