GeoIP2

 view release on metacpan or  search on metacpan

t/GeoIP2/WebService/Client.t  view on Meta::CPAN

    );
};

subtest 'user_id backwards-compatibility' => sub {
    my $client = GeoIP2::WebService::Client->new(
        user_id     => 42,
        license_key => 'abcdef123456',
        ua          => $ua,
    );

    my $e = exception { $client->country( ip => '1.2.3.14' ) };
    isa_ok(
        $e,
        'GeoIP2::Error::IPAddressNotFound',
        'error thrown when IP address cannot be found'
    );

    is(
        $e->ip_address,
        '1.2.3.14',
        'exception ip_address() method returns the IP address'
    );
};

done_testing();

{
    package Mock::LWP::UserAgent;

    use strict;
    use warnings;

    use base 'LWP::UserAgent';

    sub new {
        my $class        = shift;
        my $request_meth = shift;

        my $self = $class->SUPER::new();

        $self->{__request_meth__} = $request_meth;

        return $self;
    }

    sub request {
        my $self = shift;

        my $meth = $self->{__request_meth__};

        return $self->$meth(@_);
    }

    sub VERSION {
        return 1;
    }
}

## no critic (Subroutines::ProhibitManyArgs)
sub _response {
    my $endpoint     = shift;
    my $status       = shift;
    my $body         = shift;
    my $bad          = shift;
    my $content_type = shift;
    my $gzip         = shift;

    my $headers = HTTP::Headers->new;

    if ($content_type) {
        $headers->header( 'Content-Type' => $content_type );
    }
    elsif ( $status == 200 || ( $status >= 400 && $status < 500 ) ) {
        $headers->header( 'Content-Type' => 'application/vnd.maxmind.com-'
                . $endpoint
                . '+json; charset=UTF-8; version=1.0' );
    }

    my $encoded_body = q{};
    if ($bad) {
        $encoded_body = '{ invalid: }';
    }
    elsif ($body) {
        $encoded_body = ref $body ? $json->encode($body) : $body;
    }

    if ($gzip) {
        $headers->header( 'Content-Encoding', 'gzip' );

        my $gzipped;
        gzip( \$encoded_body => \$gzipped )
            or die "gzip failed: $GzipError";
        $encoded_body = $gzipped;
    }

    return HTTP::Response->new(
        $status,
        status_message($status),
        $headers,
        $encoded_body,
    );
}
## use critic



( run in 1.893 second using v1.01-cache-2.11-cpan-39bf76dae61 )