#!/usr/bin/env perl
BEGIN { use_ok(
'Geo::Coder::GeoApify'
) }
{
our
$REQUEST_COUNT
= 0;
our
@REQUEST_TIMES
;
sub
get {
my
(
$self
,
$url
) =
@_
;
push
@REQUEST_TIMES
,
time
();
$REQUEST_COUNT
++;
my
$content
=
'{"features": {"geometry": [{"coordinates": [0.01, 0.02]}]}}'
;
return
HTTP::Response->new(200,
'OK'
, [],
$content
);
}
}
{
no
warnings
'once'
;
$MyTestUA::REQUEST_COUNT
= 0;
@MyTestUA::REQUEST_TIMES
= ();
}
my
$min_interval
= 1;
my
$cache
= CHI->new(
driver
=>
'Memory'
,
global
=> 1,
expires_in
=>
'1 hour'
,
);
my
$ua
= MyTestUA->new();
my
$geo
= Geo::Coder::GeoApify->new(
apiKey
=>
'dummy'
,
ua
=>
$ua
,
cache
=>
$cache
,
min_interval
=>
$min_interval
,
);
my
$res1
=
$geo
->geocode(
location
=>
'4600 Silver Hill Rd., Suitland, MD'
);
ok(
$res1
&&
ref
(
$res1
) eq
'HASH'
,
'First geocode call returns hashref'
);
ok(
exists
$res1
->{features},
"Result contains key 'features'"
);
ok(
ref
(
$res1
->{features}{geometry}) eq
'ARRAY'
,
"Result contains 'geometry' array"
);
my
$res2
=
$geo
->geocode(
location
=>
'4600 Silver Hill Rd., Suitland, MD'
);
is_deeply(
$res1
,
$res2
,
'Second call returns cached features (same as first)'
);
is(
$MyTestUA::REQUEST_COUNT
, 1,
'Only one API request made for duplicate queries'
);
my
$res3
=
$geo
->geocode(
location
=>
'1600 Pennsylvania Avenue NW, Washington, DC'
);
ok(
$res3
&&
ref
(
$res3
) eq
'HASH'
,
'Third geocode call (different address) returns hashref'
);
my
$num_requests
=
scalar
@MyTestUA::REQUEST_TIMES
;
ok(
$num_requests
>= 2,
'At least two API requests have been made'
);
cmp_ok(
$num_requests
,
'=='
,
$MyTestUA::REQUEST_COUNT
);
if
(
$num_requests
>= 2) {
my
$elapsed
=
$MyTestUA::REQUEST_TIMES
[1] -
$MyTestUA::REQUEST_TIMES
[0];
cmp_ok(
$elapsed
,
'>='
,
$min_interval
,
"Rate limiting enforced: elapsed time >= $min_interval sec"
);
}
done_testing();