Permalink
Browse files

add examples and another test

  • Loading branch information...
1 parent 0493c92 commit b13f33b6e96881760fbd4011257596345785931f @sherwind committed Mar 26, 2012
Showing with 176 additions and 9 deletions.
  1. +3 −0 Changes
  2. +24 −0 examples/dancer_env.pl
  3. +20 −0 examples/mojo_env.pl
  4. +2 −2 lib/Plack/Middleware/GeoIP.pm
  5. +7 −7 t/{lookup.t → country.t}
  6. +120 −0 t/country_city.t
View
@@ -1,5 +1,8 @@
Revision history for Plack-Middleware-GeoIP
+0.02 Mon Mar 26 15:05:24 PHT 2012
+ - Added examples and another test.
+
0.01 Mon Mar 26 13:22:15 PHT 2012
- First version, released on an unsuspecting world.
View
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Dancer;
+use Data::Dump qw( dump );
+use Plack::Builder;
+
+get '/env' => sub {
+ return '<pre>' . dump( \%ENV ) . '<hr>' . dump( request->env ) . '</pre>';
+};
+
+my $app = sub {
+ my $env = shift;
+ my $request = Dancer::Request->new( env => $env );
+ Dancer->dance($request);
+};
+
+builder {
+ enable sub {
+ my $app = shift;
+ sub { $_[0]->{GEO_TEST} = 'TEST VALUE'; $app->($_[0]) }; # set PSGI environment variable
+ };
+ $app;
+};
View
@@ -0,0 +1,20 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Mojolicious::Lite;
+use Plack::Builder;
+use Data::Dump qw( dump );
+
+get '/env' => sub {
+ my $self = shift;
+ my $text = '<pre>' . dump( \%ENV ) . '<hr>' . dump( $self->req->env ) . '</pre>';
+ $self->render(text => $text);
+};
+
+builder {
+ enable sub {
+ my $app = shift;
+ sub { $_[0]->{GEO_TEST} = 'TEST VALUE'; $app->($_[0]) }; # set PSGI environment variable
+ };
+ app->start;
+};
@@ -2,7 +2,7 @@ package Plack::Middleware::GeoIP;
use strict;
use warnings;
use 5.008;
-our $VERSION = 0.01;
+our $VERSION = 0.02;
use parent qw/Plack::Middleware/;
use Geo::IP;
@@ -66,8 +66,8 @@ sub call {
$env->{GEOIP_COUNTRY_NAME} = $record->country_name;
$env->{GEOIP_LATITUDE} = $record->latitude;
$env->{GEOIP_LONGITUDE} = $record->longitude;
- $env->{GEOIP_TIME_ZONE} = $record->time_zone;
$env->{GEOIP_CONTINENT_CODE} = $record->continent_code;
+ $env->{GEOIP_TIME_ZONE} = $record->time_zone if $record->time_zone;
$env->{GEOIP_REGION} = $record->region if $record->region;
$env->{GEOIP_REGION_NAME} = $record->region_name if $record->region and $record->region_name;
$env->{GEOIP_CITY} = $record->city if $record->city;
View
@@ -6,12 +6,12 @@ use Plack::Builder;
use Plack::Request;
use HTTP::Request::Common;
-my $Geoip_database;
+my $Country_db;
foreach my $file ('GeoIP.dat', '/usr/share/GeoIP/GeoIP.dat', '/var/lib/GeoIP/GeoIP.dat', '/usr/local/share/GeoIP/GeoIP.dat') {
- $Geoip_database = $file, last if -f $file;
+ $Country_db = $file, last if -f $file;
}
-unless ($Geoip_database) {
+unless ($Country_db) {
plan skip_all => 'No GeoIP.dat found';
}
@@ -24,7 +24,7 @@ sub run_scalar {
sub { $_[0]->{REMOTE_ADDR} = $remote_addr; $app->($_[0]) }; # fake remote address
};
enable 'Plack::Middleware::GeoIP',
- GeoIPDBFile => $Geoip_database;
+ GeoIPDBFile => $Country_db;
sub { return [ 200, [ 'Content-Type' => 'text/plain' ], [ $_[0]->{GEOIP_COUNTRY_CODE} ] ] };
};
@@ -45,7 +45,7 @@ sub run_noflag {
};
enable 'Plack::Middleware::GeoIP',
GeoIPDBFile => [
- $Geoip_database,
+ $Country_db,
];
sub { return [ 200, [ 'Content-Type' => 'text/plain' ], [ $_[0]->{GEOIP_COUNTRY_CODE} ] ] };
};
@@ -67,7 +67,7 @@ sub run_oneflag {
};
enable 'Plack::Middleware::GeoIP',
GeoIPDBFile => [
- [ $Geoip_database, 'MemoryCache' ],
+ [ $Country_db, 'MemoryCache' ],
];
sub { return [ 200, [ 'Content-Type' => 'text/plain' ], [ $_[0]->{GEOIP_COUNTRY_CODE} ] ] };
};
@@ -89,7 +89,7 @@ sub run_multiflag {
};
enable 'Plack::Middleware::GeoIP',
GeoIPDBFile => [
- [ $Geoip_database, [ qw(MemoryCache CheckCache) ] ],
+ [ $Country_db, [ qw(MemoryCache CheckCache) ] ],
],
GeoIPEnableUTF8 => 1;
sub { return [ 200, [ 'Content-Type' => 'text/plain' ], [ $_[0]->{GEOIP_COUNTRY_CODE} ] ] };
View
@@ -0,0 +1,120 @@
+#!/usr/bin/perl
+use strict;
+use Test::More;
+use Plack::Test;
+use Plack::Builder;
+use Plack::Request;
+use HTTP::Request::Common;
+
+my ($Country_db, $City_db);
+foreach my $dir ('.', '/usr/share/GeoIP', '/var/lib/GeoIP', '/usr/local/share/GeoIP') {
+ my $country_file = "$dir/GeoIP.dat";
+ my $city_file = "$dir/GeoIPCity.dat";
+
+ $Country_db = $country_file if -f $country_file;
+ $City_db = $city_file if -f $city_file;
+
+ last if $Country_db and $City_db;
+}
+
+unless ($Country_db and $City_db) {
+ plan skip_all => 'No GeoIP.dat or GeoIPCity.dat found';
+}
+
+sub run_noflag {
+ my ($remote_addr, $expected_country) = @_;
+
+ my $app = builder {
+ enable sub {
+ my $app = shift;
+ sub { $_[0]->{REMOTE_ADDR} = $remote_addr; $app->($_[0]) }; # fake remote address
+ };
+ enable 'Plack::Middleware::GeoIP',
+ GeoIPDBFile => [
+ $Country_db, $City_db
+ ];
+ sub { return [ 200, [ 'Content-Type' => 'text/plain' ], [ defined $_[0]->{GEOIP_LATITUDE} ? $_[0]->{GEOIP_COUNTRY_CODE} : '' ] ] };
+ };
+
+ test_psgi $app, sub {
+ my $cb = shift;
+ my $res = $cb->(GET '/');
+ is $res->content, $expected_country;
+ };
+}
+
+sub run_oneflag {
+ my ($remote_addr, $expected_country) = @_;
+
+ my $app = builder {
+ enable sub {
+ my $app = shift;
+ sub { $_[0]->{REMOTE_ADDR} = $remote_addr; $app->($_[0]) }; # fake remote address
+ };
+ enable 'Plack::Middleware::GeoIP',
+ GeoIPDBFile => [
+ $Country_db, [ $City_db, 'MemoryCache' ],
+ ];
+ sub { return [ 200, [ 'Content-Type' => 'text/plain' ], [ defined $_[0]->{GEOIP_LATITUDE} ? $_[0]->{GEOIP_COUNTRY_CODE} : '' ] ] };
+ };
+
+ test_psgi $app, sub {
+ my $cb = shift;
+ my $res = $cb->(GET '/');
+ is $res->content, $expected_country;
+ };
+}
+
+sub run_multiflag {
+ my ($remote_addr, $expected_country) = @_;
+
+ my $app = builder {
+ enable sub {
+ my $app = shift;
+ sub { $_[0]->{REMOTE_ADDR} = $remote_addr; $app->($_[0]) }; # fake remote address
+ };
+ enable 'Plack::Middleware::GeoIP',
+ GeoIPDBFile => [
+ $Country_db, [ $City_db, [ qw(MemoryCache CheckCache) ] ],
+ ],
+ GeoIPEnableUTF8 => 1;
+ sub { return [ 200, [ 'Content-Type' => 'text/plain' ], [ defined $_[0]->{GEOIP_LATITUDE} ? $_[0]->{GEOIP_COUNTRY_CODE} : '' ] ] };
+ };
+
+ test_psgi $app, sub {
+ my $cb = shift;
+ my $res = $cb->(GET '/');
+ is $res->content, $expected_country;
+ };
+}
+
+while (<DATA>) {
+ chomp;
+ my ($ip_addr, $expected_country) = split /\s+/;
+
+ run_noflag($ip_addr, $expected_country);
+ run_oneflag($ip_addr, $expected_country);
+ run_multiflag($ip_addr, $expected_country);
+}
+
+done_testing;
+
+__DATA__
+203.174.65.12 JP
+212.208.74.140 FR
+200.219.192.106 BR
+134.102.101.18 DE
+193.75.148.28 BE
+134.102.101.18 DE
+147.251.48.1 CZ
+194.244.83.2 IT
+203.15.106.23 AU
+196.31.1.1 ZA
+210.54.22.1 NZ
+210.25.5.5 CN
+210.54.122.1 NZ
+210.25.15.5 CN
+192.37.51.100 CH
+192.37.150.150 CH
+192.106.51.100 IT
+192.106.150.150 IT

0 comments on commit b13f33b

Please sign in to comment.