APISchema
view release on metacpan or search on metacpan
t/Plack-Middleware-APISchema-ResponseValidator.t view on Meta::CPAN
} elsif ($req->parameters->{undefined}) {
return [ 599, [ 'Content-Type' => 'application/json', ], [
encode_json({ status => 599, message => 'Something wrong' }),
] ];
} else {
return [ 400, [ 'Content-Type' => 'application/json', ], [
encode_json({ status => 400, message => 'Bad Request' }),
] ];
}
});
subtest 'Status 200 with valid body' => sub {
test_psgi $middleware_ok => sub {
my $server = shift;
my $res = $server->(GET '/get?success=1');
is $res->code, 200;
done_testing;
};
};
subtest 'Status 400 with valid body' => sub {
test_psgi $middleware_ok => sub {
my $server = shift;
my $res = $server->(GET '/get');
is $res->code, 400;
done_testing;
};
};
subtest 'Undefined status' => sub {
test_psgi $middleware_ok => sub {
my $server = shift;
my $res = $server->(GET '/get?undefined=1');
is $res->code, 599;
done_testing;
};
};
my $middleware_ng = Plack::Middleware::APISchema::ResponseValidator->new(schema => $schema);
$middleware_ng->wrap(sub {
my $env = shift;
my $req = Plack::Request->new($env);
return [ 400, [ 'Content-Type' => 'text/plain', ], [ 'OK' ] ];
});
subtest 'Status 400 with invalid body' => sub {
test_psgi $middleware_ng => sub {
my $server = shift;
my $res = $server->(GET '/get');
is $res->code, 500;
is $res->header('X-Error-Cause'), 'Plack::Middleware::APISchema::ResponseValidator+Valiemon';
cmp_deeply $res->content, json({ body => {
message => 'Failed to parse json',
encoding => 'json',
} });
done_testing;
};
};
}
sub response_validator_with_utf8 : Tests {
my $schema = t::test::fixtures::prepare_user;
$schema->register_route(
method => 'GET',
route => '/user',
response_resource => {
body => 'user',
},
);
my $middleware = Plack::Middleware::APISchema::ResponseValidator->new(schema => $schema);
$middleware->wrap(sub {
[200, [ 'Content-Type' => 'application/json; charset=utf-8' ], [ encode_json({ first_name => 'Bill', last_name => []}) ] ]
});
subtest 'invalid response with utf8' => sub {
test_psgi $middleware => sub {
my $server = shift;
my $res = $server->(GET '/user');
is $res->code, 500;
cmp_deeply $res->content, json({
body => {
attribute => "Valiemon::Attributes::Type",
position => '/$ref/properties/last_name/type',
expected => $schema->get_resource_by_name('user')->definition->{properties}->{last_name},
actual => [],
message => "Contents do not match resource 'user'",
encoding => 'json',
},
});
done_testing;
};
};
}
( run in 1.102 second using v1.01-cache-2.11-cpan-39bf76dae61 )