Dancer2-Plugin-RPC
view release on metacpan or search on metacpan
lib/Dancer2/RPCPlugin/DefaultRoute.pm view on Meta::CPAN
=head2 setup_default_route
Installs a Dancer route-handler for C<< any qr{.+} >> which tries to return an
appropriate error response to the requestor.
=head3 Responses
All responeses will have B<status: 200 OK>
The B<content-type> (and B<body>) of the request determine the error-response:
=over
=item B<text/xml>
If the B<body> is valid XMLRPC, the response is an XMLRPC-fault:
faultCode => -32601
faultString => "Method '%s' not found"
lib/Dancer2/RPCPlugin/DefaultRoute.pm view on Meta::CPAN
code => -32601
message => "Method '%s' not found"
If the B<body> is I<not> valid JSONRPC, the response is a generic json struct:
'error': {
'code': -32601,
'message': "Method '$request->path' not found"
}
=item B<other/content-type>
Any other content-type is outside the scope of the service. We can respond in
any way we like. For the moment it will be:
status(404)
content_type('text/plain')
body => "Error! '$request->path' was not found for '$request->content_type'"
=back
=cut
t/070-default-route-no-rpc.t view on Meta::CPAN
response_contains_ok(
$response,
'200 OK',
'application/json',
{result => 'pong'},
"ok(jsonrpc: ping)"
);
$request = HTTP::Request->new(
POST => '/xmlrpc',
['content-type' => 'text/xml'],
as_xmlrpc('ping'),
);
$response = $tester->request($request);
response_contains_ok(
$response,
'200 OK',
'text/xml',
{result => 'pong'},
"ok(xmlrpc: ping)"
);
$request = HTTP::Request->new(
POST => '/restrpc/ping',
[ 'content-type' => 'application/json' ],
);
$response = $tester->request($request);
response_contains_ok(
$response,
'200 OK',
'application/json',
{result => 'pong'},
"ok(restrpc: ping)"
);
}
note("Send *rubbish* to the working app **without** catch-all");
{
my $request = HTTP::Request->new(
POST => '/jsonrpc',
[ 'content-type' => 'application/rubbish' ],
as_jsonrpc('ping'),
);
my $response = $tester->request($request);
is($response->status_line, '404 Not Found', "URI not found for content-type(jsonrpc)");
is($response->content_type, 'text/html', "Content-Type set to html");
$request = HTTP::Request->new(
POST => '/xmlrpc',
[ 'content-type' => 'application/rubbish' ],
as_xmlrpc('ping'),
);
$response = $tester->request($request);
response_contains_ok(
$response,
'404 Not Found',
'text/html',
qr{Error 404 - Not Found}
);
t/070-default-route-no-rpc.t view on Meta::CPAN
ok(setup_default_route(),"setting the catchall route");
#any qr{.+} => sub { error(">>>>>>>>>>>>>>>>>>", \@_); pass };
$app = main->to_app();
$tester = Plack::Test->create($app);
note("Send *rubbish* to the working app **with** catch-all");
{
my $request = HTTP::Request->new(
POST => '/jsonrpc',
[ 'content-type' => 'application/rubbish' ],
as_jsonrpc('ping'),
);
my $response = $tester->request($request);
response_contains_ok(
$response,
'404 Not Found',
'text/plain',
"Error! '/jsonrpc' was not found for 'application/rubbish'",
);
t/070-default-route-no-rpc.t view on Meta::CPAN
my $ok = 1;
$ok &&= is(
$response->status_line,
$status,
"$message: status ($status)"
);
$ok &&= is(
$response->content_type,
$content_type,
"$message: content-type ($content_type)"
);
my $data;
if ($response->content_type =~ m{(application|text)/xml}) {
$data = $parser->parse($response->content)->value->value;
$ok &&= is_deeply(
$data,
$content,
"$message: content (xmlrpc)"
);
t/071-default-route-xmlrpc.t view on Meta::CPAN
abeltje_done_testing();
sub _post {
my ($endpoint, $body) = @_;
my $parser = RPC::XML::ParserFactory->new();
$body //= { method => 'system_ping' };
my $xmlrpc_request = RPC::XML::request->new($body->{method})->as_string();
my $request = HTTP::Request->new(
POST => $endpoint,
[ 'content-type' => 'text/xml' ],
$xmlrpc_request,
);
my $response = $tester->request($request);
my $dancer_response = {
content_type => $response->header('content_type'),
status => $response->code,
content => $response->content,
};
if ($dancer_response->{status} eq '200') {
t/100-xmlrpc.t view on Meta::CPAN
<methodCall>
<methodName>ping</methodName>
<params/>
</methodCall>
EOXML
);
my $response = $tester->request($request);
is($response->status_line, "404 Not Found", "Check method POST for xmlrpc");
};
subtest "XMLRPC wrong content-type (404)" => sub {
my $request = HTTP::Request->new(
POST => '/endpoint',
[ 'Content-Type' => 'application/json', ],
<<' EOXML',
<?xml version="1.0"?>
<methodCall>
<methodName>ping</methodName>
<params/>
</methodCall>
EOXML
);
my $response = $tester->request($request);
is($response->status_line, "404 Not Found", "Check content-type xmlrpc")
or diag(explain($response));
};
subtest "XMLRPC unknown rpc-method (404)" => sub {
my $request = HTTP::Request->new(
POST => '/endpoint',
['Content-Type' => 'text/xml'],
<<' EOXML',
<?xml version="1.0"?>
<methodCall>
t/200-jsonrpc.t view on Meta::CPAN
is_deeply(
from_json($response->decoded_content)->{error},
{
code => -32601,
message => "Method 'methodList' not found at '/endpoint' (skipped)"
},
"Unknown jsonrpc-method"
);
};
subtest "JSONRPC wrong content-type => 404" => sub {
my $request = HTTP::Request->new(
POST => '/endpoint',
[ 'Content-Type' => 'text/xml', ],
encode_json(
{
jsonrpc => '2.0',
method => 'method.list',
id => 42,
params => {plugin => 'jsonrpc'}
}
),
);
my $response = $tester->request($request);
is($response->status_line, "404 Not Found", "Check content-type jsonrpc")
or diag(explain($response));
};
abeltje_done_testing();
BEGIN {
package MyJSONRPCApp;
use lib 'ex/';
use Dancer2;
use Dancer2::Plugin::RPC::JSONRPC;
t/300-restrpc.t view on Meta::CPAN
'Accept' => 'application/json',
],
encode_json( {plugin => 'restrpc'} ),
);
my $response = $tester->request($request);
is($response->status_line, "404 Not Found", "Not found...")
or diag("Response: ", explain($response));
};
subtest "RESTRPC wrong content-type => 404" => sub {
my $request = HTTP::Request->new(
POST => '/endpoint/method_list',
[ 'Content-Type' => 'text/xml', ],
encode_json( {plugin => 'jsonrpc'} ),
);
my $response = $tester->request($request);
is($response->status_line, "404 Not Found", "Check content-type jsonrpc")
or diag(explain($response));
};
abeltje_done_testing();
BEGIN {
package MyRESTRPCApp;
use lib 'ex/';
use Dancer2;
use Dancer2::Plugin::RPC::RESTRPC;
( run in 1.135 second using v1.01-cache-2.11-cpan-d7f47b0818f )