APISchema
view release on metacpan or search on metacpan
t/Plack-Middleware-APISchema-ResponseValidator.t view on Meta::CPAN
message => "Failed to parse json",
encoding => 'json',
},
});
done_testing;
}
};
subtest 'when content-type is incorrect' => sub {
test_psgi $middleware => sub {
$content_type = 'application/x-www-form-urlencoded';
$json = encode_json({value => 19.5});
$header = [];
my $server = shift;
my $res = $server->(POST '/bmi');
is $res->code, 500;
is $res->header('X-Error-Cause'), 'Plack::Middleware::APISchema::ResponseValidator+Valiemon';
cmp_deeply $res->content, json({
body => {
attribute => 'Valiemon::Attributes::Required',
position => '/$ref/required',
message => "Contents do not match resource 'bmi'",
encoding => 'url_parameter',
actual => {
'{"value":19.5}' => undef,
},
expected => $schema->get_resource_by_name('bmi')->definition,
},
});
done_testing;
}
};
subtest 'when content-type is incorrect with forced encoding' => sub {
test_psgi $middleware => sub {
$content_type = 'application/x-www-form-urlencoded';
$json = encode_json({value => 19.5});
$header = [];
my $server = shift;
my $res = $server->(POST '/bmi_force_json');
is $res->code, 200;
done_testing;
}
};
subtest 'when content-type is incorrect with strict content-type check' => sub {
test_psgi $middleware => sub {
$content_type = 'application/x-www-form-urlencoded';
$json = encode_json({value => 19.5});
my $server = shift;
my $res = $server->(POST '/bmi_strict');
is $res->code, 500;
is $res->header('X-Error-Cause'), 'Plack::Middleware::APISchema::ResponseValidator+Valiemon';
cmp_deeply $res->content, json({
body => { message => "Wrong content-type: $content_type" },
});
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 valid response by header' => sub {
test_psgi $middleware => sub {
$content_type = 'application/json';
$json = encode_json({});
$header = [ 'X-Value' => 19.5 ];
my $server = shift;
my $res = $server->(POST '/bmi_by_header');
is $res->code, 200;
done_testing;
}
};
subtest 'when invalid response by header' => sub {
test_psgi $middleware => sub {
$content_type = 'application/json';
$json = encode_json({});
$header = [ 'X-Foo' => 1 ];
my $server = shift;
my $res = $server->(POST '/bmi_by_header');
is $res->code, 500;
is $res->header('X-Error-Cause'), 'Plack::Middleware::APISchema::ResponseValidator+Valiemon';
cmp_deeply $res->content, json({
header => {
attribute => "Valiemon::Attributes::Required",
position => '/$ref/required',
message => "Contents do not match resource 'bmi_header'",
encoding => 'perl',
expected => $schema->get_resource_by_name('bmi_header')->definition,
actual => {
content_type => "application/json",
"x_foo" => 1,
},
},
});
done_testing;
}
};
}
sub status : Tests {
my $schema = t::test::fixtures::prepare_status;
my $middleware_ok = Plack::Middleware::APISchema::ResponseValidator->new(schema => $schema);
$middleware_ok->wrap(sub {
my $env = shift;
my $req = Plack::Request->new($env);
if ($req->parameters->{success}) {
return [ 200, [ 'Content-Type' => 'text/plain', ], [ 'OK' ] ];
} elsif ($req->parameters->{undefined}) {
return [ 599, [ 'Content-Type' => 'application/json', ], [
encode_json({ status => 599, message => 'Something wrong' }),
] ];
} else {
return [ 400, [ 'Content-Type' => 'application/json', ], [
( run in 0.461 second using v1.01-cache-2.11-cpan-f56aa216473 )