Business-GoCardless
view release on metacpan or search on metacpan
t/business/gocardless/client.t view on Meta::CPAN
#!perl
use strict;
use warnings;
use utf8;
use Test::Most;
use Test::Deep;
use Test::Exception;
use HTTP::Response;
use HTTP::Date qw/ time2str /;
use JSON ();
use Encode ();
use POSIX ();
use_ok( 'Business::GoCardless::Client' );
isa_ok(
my $Client = Business::GoCardless::Client->new(
token => 'MvYX0i6snRh/1PXfPoc6',
app_id => 'some application',
app_secret => 'setec astronomy',
merchant_id => '1DJUN3H1I2',
),
'Business::GoCardless::Client'
);
can_ok(
$Client,
qw/
token
base_url
api_path
app_id
app_secret
merchant_id
max_retries
max_retry_wait
raise_on_idempotency_conflict
rate_limit
api_get
api_post
api_put
api_delete
/,
);
is( $Client->max_retries,3,'max_retries default' );
is( $Client->max_retry_wait,60,'max_retry_wait default' );
ok( ! $Client->raise_on_idempotency_conflict,'raise_on_idempotency_conflict default' );
ok( ! $Client->rate_limit,'no rate_limit before any requests' );
# monkey patching LWP here to make this test work without having to actually
# hit the endpoints - each request gets the next response in @responses
no warnings 'redefine';
no warnings 'once';
my ( @responses,@requests,@sleeps );
*LWP::UserAgent::request = sub {
my ( $ua,$req ) = @_;
push( @requests,$req );
return shift( @responses ) // die "No more responses";
};
*Business::GoCardless::Client::_sleep = sub { push( @sleeps,$_[1] ) };
sub _response {
my ( $code,$data,%headers ) = @_;
my $res = HTTP::Response->new( $code );
$res->header( %headers ) if %headers;
$res->content( Encode::encode_utf8( JSON->new->encode( $data ) ) )
if defined $data;
return $res;
}
sub _error {
my ( $code,$type,@errors ) = @_;
return _response( $code,{ error => {
type => $type,
code => $code,
message => "An error ($type)",
request_id => 'REQ123',
documentation_url => 'https://developer.gocardless.com/api-reference#' . $type,
errors => [ @errors ],
} } );
}
sub _reset {
@responses = @_;
@requests = ();
@sleeps = ();
}
my $uuid_re = qr/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
my $Pro = Business::GoCardless::Client->new(
token => 'foo',
api_version => 2,
);
note( "Idempotency keys" );
_reset( _response( 201,{ payments => { id => 'PM123' } } ) );
cmp_deeply(
scalar( $Pro->api_post( '/payments',{ payments => { amount => 100 } } ) ),
{ payments => { id => 'PM123' } },
'->api_post (create)'
);
like(
$requests[0]->header( 'Idempotency-Key' ),
$uuid_re,
( run in 0.959 second using v1.01-cache-2.11-cpan-99fa8243a42 )