APISchema
view release on metacpan or search on metacpan
t/Plack-Middleware-APISchema-RequestValidator.t view on Meta::CPAN
route => '/bmi_by_header',
request_resource => {
header => 'figure_header',
},
);
my $middleware = Plack::Middleware::APISchema::RequestValidator->new(schema => $schema);
$middleware->wrap(sub {
[200, [ 'Content-Type' => 'text/plain' ], [ 'dummy' ] ]
});
subtest 'when valid request' => sub {
test_psgi $middleware => sub {
my $server = shift;
my $res = $server->(
POST '/bmi',
Content_Type => 'application/json',
Content => encode_json({weight => 50, height => 1.6}),
);
is $res->code, 200;
done_testing;
}
};
subtest 'when valid utf8 request' => sub {
test_psgi $middleware => sub {
my $server = shift;
my $res = $server->(
POST '/bmi',
Content_Type => 'application/json; charset=UTF-8',
Content => encode_json({weight => 50, height => 1.6}),
);
is $res->code, 200;
done_testing;
}
};
subtest 'when invalid request' => sub {
test_psgi $middleware => sub {
my $server = shift;
my $res = $server->(
POST '/bmi',
Content_Type => 'application/json',
Content => encode_json({}),
);
is $res->code, HTTP_UNPROCESSABLE_ENTITY;
cmp_deeply $res->content, json({
body => {
attribute => 'Valiemon::Attributes::Required',
position => '/$ref/required',
message => "Contents do not match resource 'figure'",
encoding => 'json',
actual => {},
expected => $schema->get_resource_by_name('figure')->definition,
},
});
done_testing;
}
};
subtest 'other endpoints are not affected' => sub {
test_psgi $middleware => sub {
my $server = shift;
my $res = $server->(GET '/other/');
is $res->code, 200;
}
};
subtest 'when request is not a JSON' => sub {
test_psgi $middleware => sub {
my $server = shift;
my $res = $server->(
POST '/bmi',
Content_Type => 'application/json',
Content => 'aaa',
);
is $res->code, HTTP_UNPROCESSABLE_ENTITY;
cmp_deeply $res->content, json({
body => {
message => "Failed to parse json",
encoding => 'json',
},
});
done_testing;
}
};
subtest 'when content-type is incorrect' => sub {
test_psgi $middleware => sub {
my $server = shift;
my $content_type = 'application/x-www-form-urlencoded';
my $res = $server->(
POST '/bmi',
Content_Type => $content_type,
Content => encode_json({weight => 50, height => 1.6}),
);
is $res->code, HTTP_UNPROCESSABLE_ENTITY;
cmp_deeply $res->content, json({
body => {
attribute => 'Valiemon::Attributes::Required',
position => '/$ref/required',
message => "Contents do not match resource 'figure'",
encoding => 'url_parameter',
actual => isa('HASH'),
# XXX: Hash order randomization
# actual => { "{\"weight\":50,\"height\":1.6}" => undef }
expected => $schema->get_resource_by_name('figure')->definition,
},
});
done_testing;
}
};
subtest 'when content-type is incorrect with forced encoding' => sub {
test_psgi $middleware => sub {
my $server = shift;
my $content_type = 'application/x-www-form-urlencoded';
my $res = $server->(
POST '/bmi_force_json',
Content_Type => $content_type,
Content => encode_json({weight => 50, height => 1.6}),
( run in 1.129 second using v1.01-cache-2.11-cpan-f56aa216473 )