Dancer-Plugin-RPC

 view release on metacpan or  search on metacpan

lib/Dancer/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/Dancer/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

{
    my $response = dancer_response(
        POST => '/jsonrpc',
        {
            headers => [
                content_type => 'application/rubbish',
            ],
            body => as_jsonrpc('ping'),
        }
    );
    is($response->status, 404, "URI not found for content-type(jsonrpc)");
    is($response->content_type, 'text/html', "Content-Type set to html");

    $response = dancer_response(
        POST => '/xmlrpc',
        {
            headers => [
                content_type => 'application/rubbish',
            ],
            body => as_xmlrpc('ping'),
        }

t/070-default-route-no-rpc.t  view on Meta::CPAN


    my $ok = 1;
    $ok &&= is(
        $response->status,
        $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/072-default-route-jsonrpc.t  view on Meta::CPAN

    my $request = to_json($body);

    my $response = dancer_response(
        POST => $endpoint,
        {
            content_type => 'application/json',
            body         => $request,
        }
    );

    if ($response->{headers}{'content-type'} eq 'application/json') {
        $response->{content} = from_json($response->{content})
    }
    return $response;
}

t/100-xmlrpc.t  view on Meta::CPAN

            ],
            body => <<'        EOXML',
<?xml version="1.0"?>
<methodCall>
    <methodName>system.version</methodName>
    <params/>
</methodCall>
        EOXML
        }
    );
    is($response->status, 404, "Check content-type xmlrpc");
}

{
    my $response = dancer_response(
        POST => '/system',
        {
            headers => [
                'Content-Type' => 'text/xml',
            ],
            body => <<'        EOXML',
<?xml version="1.0"?>
<methodCall>
    <methodName>system.doesnotexist</methodName>
    <params/>
</methodCall>
        EOXML
        }
    );
    is($response->status, 404, "Check content-type xmlrpc");
}

{
    my $old_log = read_logs(); # clean up for this test
    my $response = dancer_response(
        POST => '/api',
        {
            headers => [
                'Content-Type' => 'text/xml',
            ],

t/150-jsonrpc.t  view on Meta::CPAN

                {
                    jsonrpc => '2.0',
                    method  => 'api.uppercase',
                    id      => 42,
                    params  => {argument => 'uppercase'}
                }
            ),
        }
    );

    is($response->{status}, 404, "Not found (not jsonrpc-content-type)")
        or diag(explain($response));

}

{
    my $response = dancer_response(
        POST => '/jsonrpc/api',
        {
            headers => [
                'Content-Type' => 'application/json',

t/175-restrpc.t  view on Meta::CPAN

{
    my $response = dancer_response(
        POST => '/rest/system/ping',
        {
            headers => [
                'Content-Type' => 'form',
            ],
        }
    );

    is($response->status, 404, "Check content-type restrpc");
}

{
    my $old_log = read_logs(); # clean up for this test
    my $response = dancer_response(
        POST => '/rest/api/uppercase',
        {
            headers => [
                'Content-Type' => 'application/json',
            ],



( run in 1.636 second using v1.01-cache-2.11-cpan-d7f47b0818f )