Created
April 9, 2016 16:29
-
-
Save adamtaylor/68a6a2c9a7e468895815c6ef718fcc0a to your computer and use it in GitHub Desktop.
dancer override ip address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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