Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamtaylor/68a6a2c9a7e468895815c6ef718fcc0a to your computer and use it in GitHub Desktop.
Save adamtaylor/68a6a2c9a7e468895815c6ef718fcc0a to your computer and use it in GitHub Desktop.
dancer override ip address
# 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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment