Business-Payr
view release on metacpan or search on metacpan
t/business/payr/request.t view on Meta::CPAN
subtest 'empty body' => sub {
%status = (
code => 200,
body => '',
type => 'application/json',
);
throws_ok(
sub { $Request->api_post( '/onboarding/', {} ) },
qr/Payr POST .* returned 200 with an empty body/,
'empty body throws a meaningful error'
);
};
subtest 'malformed JSON' => sub {
%status = (
code => 200,
body => 'this is not json at all',
type => 'application/json',
);
throws_ok(
sub { $Request->api_post( '/onboarding/', {} ) },
qr/Payr POST .* returned 200 with malformed JSON/,
'malformed JSON throws a meaningful error'
);
};
subtest 'JSON array (not a hash)' => sub {
%status = (
code => 200,
body => '[{"a":1},{"b":2}]',
type => 'application/json',
);
throws_ok(
sub { $Request->api_post( '/onboarding/', {} ) },
qr/Payr POST .* returned 200 JSON ARRAY\(/,
'JSON array response throws a meaningful error'
);
};
subtest 'authentication error - detail key' => sub {
%status = (
code => 401,
body => '{"detail":"Invalid token."}',
type => 'application/json',
);
throws_ok(
sub { $Request->api_post( '/onboarding/', {} ) },
qr/Payr POST .* returned 401: Invalid token\./,
'401 with detail key throws a meaningful error'
);
};
subtest 'authentication error - credentials not provided' => sub {
%status = (
code => 401,
body => '{"detail":"Authentication credentials were not provided."}',
type => 'application/json',
);
throws_ok(
sub { $Request->api_post( '/onboarding/', {} ) },
qr/Payr POST .* returned 401: Authentication credentials were not provided\./,
'401 missing credentials throws a meaningful error'
);
};
subtest 'processing error - error key' => sub {
%status = (
code => 400,
body => '{"error":"Failed to download agreement document from provided URL"}',
type => 'application/json',
);
throws_ok(
sub { $Request->api_post( '/onboarding/', {} ) },
qr/Payr POST .* returned 400: Failed to download agreement document/,
'400 with error key throws a meaningful error'
);
};
subtest 'field-level validation errors' => sub {
%status = (
code => 400,
body => '{"email":["This field is required."],"user_id":["This field is required."]}',
type => 'application/json',
);
throws_ok(
sub { $Request->api_post( '/onboarding/', {} ) },
qr/Payr POST .* returned 400: .*This field is required/,
'400 field-level validation errors throw a meaningful error'
);
};
subtest 'nested validation errors (within tenant array)' => sub {
%status = (
code => 400,
body => '{"tenant":[{"frequency":["\"weekly\" is not a valid choice."]}]}',
type => 'application/json',
);
throws_ok(
sub { $Request->api_post( '/onboarding/', {} ) },
qr/Payr POST .* returned 400: .*tenant\.frequency.*is not a valid choice/,
'400 nested tenant validation errors throw a meaningful error'
);
};
subtest 'unknown JSON keys - fallback error' => sub {
%status = (
code => 500,
body => '{"unexpected_key":"some_value","another_key":"data"}',
type => 'application/json',
message => 'Internal Server Error',
);
throws_ok(
sub { $Request->api_post( '/onboarding/', {} ) },
qr/Payr POST .* returned 500 with JSON keys 'another_key', 'unexpected_key' and status line: Internal Server Error/,
( run in 0.952 second using v1.01-cache-2.11-cpan-007c89162af )