Skip to content

Commit 60d1d96

Browse files
committed
Mock resproxy API responses in tests
1 parent 80d723b commit 60d1d96

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

Geo-IPinfo/Makefile.PL

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ WriteMakefile(
1616
},
1717
BUILD_REQUIRES => {
1818
'Test::More' => '0',
19+
'Test::Mock::LWP::Dispatch' => '0',
20+
'HTTP::Response' => '0',
1921
},
2022
PREREQ_PM => {
2123
'LWP::UserAgent' => '0',

Geo-IPinfo/t/05-usage-resproxy.t

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,42 @@ else {
1212
}
1313

1414
use_ok('Geo::IPinfo');
15+
use Test::Mock::LWP::Dispatch;
16+
use HTTP::Response;
1517

1618
my $ip;
1719

18-
$ip = Geo::IPinfo->new( $ENV{IPINFO_TOKEN} );
20+
# Set up mock responses
21+
$mock_ua->map(
22+
qr{https://ipinfo\.io/resproxy/175\.107\.211\.204},
23+
sub {
24+
my $response = HTTP::Response->new(200);
25+
$response->header('Content-Type' => 'application/json');
26+
$response->content('{"ip":"175.107.211.204","last_seen":"2025-01-20","percent_days_seen":0.85,"service":"example_service"}');
27+
return $response;
28+
}
29+
);
30+
31+
$mock_ua->map(
32+
qr{https://ipinfo\.io/resproxy/8\.8\.8\.8},
33+
sub {
34+
my $response = HTTP::Response->new(200);
35+
$response->header('Content-Type' => 'application/json');
36+
$response->content('{}');
37+
return $response;
38+
}
39+
);
40+
41+
$ip = Geo::IPinfo->new("test_token");
1942
isa_ok( $ip, "Geo::IPinfo", '$ip' );
2043

2144
# Test resproxy with known residential proxy IP
2245
my $resproxy = $ip->resproxy("175.107.211.204");
2346
ok( $resproxy, "resproxy() returns data for known residential proxy IP" );
2447
is( $resproxy->{ip}, "175.107.211.204", "IP field is correct" );
25-
ok( defined $resproxy->{last_seen}, "last_seen field is defined" );
26-
ok( defined $resproxy->{percent_days_seen}, "percent_days_seen field is defined" );
27-
ok( defined $resproxy->{service}, "service field is defined" );
48+
is( $resproxy->{last_seen}, "2025-01-20", "last_seen field is correct" );
49+
is( $resproxy->{percent_days_seen}, 0.85, "percent_days_seen field is correct" );
50+
is( $resproxy->{service}, "example_service", "service field is correct" );
2851

2952
# Test resproxy with IP that returns empty response
3053
my $empty_resproxy = $ip->resproxy("8.8.8.8");

0 commit comments

Comments
 (0)