1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
use strict;
use warnings;
use Test::More;
use Plack::Test;
use HTTP::Request;
use JSON;
BEGIN {
use_ok "JSON::RPC::Dispatch";
use_ok "JSON::RPC::Constants", ':all';
use_ok "JSON::RPC::Test";
use_ok "t::JSON::RPC::Test::Handler::Sum";
}
subtest 'defaults' => sub {
my $dispatch = JSON::RPC::Dispatch->new();
if (ok $dispatch->coder) {
isa_ok $dispatch->coder, 'JSON';
}
if (ok $dispatch->router) {
isa_ok $dispatch->router, "Router::Simple";
}
if (ok $dispatch->parser) {
isa_ok $dispatch->parser, "JSON::RPC::Parser";
}
};
subtest 'normal dispatch' => sub {
my $coder = JSON->new;
my $router = Router::Simple->new;
$router->connect( blowup => {
handler => "Sum",
action => "blowup",
} );
$router->connect( 'sum' => {
handler => 'Sum',
action => 'sum',
} );
$router->connect( tidy_error => {
handler => "Sum",
action => "tidy_error",
} );
$router->connect( 'sum_obj' => {
handler => t::JSON::RPC::Test::Handler::Sum->new,
action => 'sum',
} );
my $dispatch = JSON::RPC::Dispatch->new(
coder => $coder,
parser => JSON::RPC::Parser->new( coder => $coder ),
prefix => 't::JSON::RPC::Test::Handler',
router => $router,
);
ok $dispatch, "dispatch ok";
my $request_get = sub {
my $cb = shift;
my ($req, $res, $json);
my $uri = URI->new( "http://localhost" );
# no such method...
$uri->query_form(
method => 'not_found'
);
$req = HTTP::Request->new( GET => $uri );
$res = $cb->( $req );
if (! ok $res->is_success, "response is success") {
diag $res->as_string;
}
$json = $coder->decode( $res->decoded_content );
if ( ! ok $json->{error}, "I should have gotten an error" ) {
diag explain $json;
}
if (! is $json->{error}->{code}, JSON::RPC::Constants::RPC_METHOD_NOT_FOUND(), "code is RPC_METHOD_NOT_FOUND" ) {
diag explain $json;
}
my @params = ( 1, 2, 3, 4, 5 );
foreach my $method ( qw(sum sum_obj) ){
$uri->query_form(
method => $method,
params => $coder->encode(\@params)
);
$req = HTTP::Request->new( GET => $uri );
$res = $cb->( $req );
if (! ok $res->is_success, "response is success") {
diag $res->as_string;
}
$json = $coder->decode( $res->decoded_content );
if (! ok ! $json->{error}, "no errors") {
diag explain $json;
}
my $sum = 0;
foreach my $p (@params) {
$sum += $p;
}
is $json->{result}, $sum, "sum matches";
}
my $id = time();
$uri->query_form(
jsonrpc => '2.0',
id => $id,
method => 'blowup',
params => "fuga",
);
$req = HTTP::Request->new( GET => $uri );
$res = $cb->( $req );
if (! ok $res->is_success, "response is success") {
diag $res->as_string;
}
$json = $coder->decode( $res->decoded_content );
is $json->{jsonrpc}, '2.0';
is $json->{id}, $id;
ok $json->{error};
};
my $request_post = sub {
my $cb = shift;
my ($req, $res, $post_content, $json);
my $headers = HTTP::Headers->new( Content_Type => 'application/json',);
my $uri = URI->new( "http://localhost" );
$post_content = $coder->encode( { method => 'not_found' } );
# no such method...
$req = HTTP::Request->new( POST => $uri, $headers, $post_content);
$res = $cb->($req);
if (! ok $res->is_success, "response is success") {
diag $res->as_string;
}
$json = $coder->decode( $res->decoded_content );
if ( ! ok $json->{error}, "I should have gotten an error" ) {
diag explain $json;
}
if (! is $json->{error}->{code}, JSON::RPC::Constants::RPC_METHOD_NOT_FOUND(), "code is RPC_METHOD_NOT_FOUND" ) {
diag explain $json;
}
my @params = ( 1, 2, 3, 4, 5 );
foreach my $method ( qw(sum sum_obj) ){
$post_content = $coder->encode(
{
method => $method,
params => \@params,
},
);
$req = HTTP::Request->new( POST => $uri, $headers, $post_content );
$res = $cb->( $req );
if (! ok $res->is_success, "response is success") {
diag $res->as_string;
}
$json = $coder->decode( $res->decoded_content );
if (! ok ! $json->{error}, "no errors") {
diag explain $json;
}
my $sum = 0;
foreach my $p (@params) {
$sum += $p;
}
is $json->{result}, $sum, "sum matches";
}
my $id = time();
$post_content = $coder->encode(
{
jsonrpc => '2.0',
id => $id,
method => 'blowup',
params => "fuga",
},
);
$req = HTTP::Request->new( POST => $uri, $headers, $post_content );
$res = $cb->( $req );
if (! ok $res->is_success, "response is success") {
diag $res->as_string;
}
$json = $coder->decode( $res->decoded_content );
is $json->{jsonrpc}, '2.0';
is $json->{id}, $id;
ok $json->{error};
};
my $request_post_batch = sub {
my $cb = shift;
my ($req, $res, $post_content, $json);
my $headers = HTTP::Headers->new( Content_Type => 'application/json',);
my $uri = URI->new( "http://localhost" );
$post_content = $coder->encode(
[
{
jsonrpc => '2.0',
id => 1,
method => 'sum',
params => [(1..3)],
}
],
);
$req = HTTP::Request->new( POST => $uri, $headers, $post_content );
$res = $cb->( $req );
if (! ok $res->is_success, "response is success") {
diag $res->as_string;
}
$json = $coder->decode( $res->decoded_content );
is ref $json, 'ARRAY', 'response is array-ref';
};
# XXX I want to test both Plack::Request and raw env, but test_rpc
# makes it kinda hard... oh well, it's not /that/ much of a problem
test_rpc $dispatch, sub {
my $cb = shift;
subtest 'JSONRPC via GET' => sub { $request_get->($cb) };
subtest 'JSONRPC via POST' => sub { $request_post->($cb) };
subtest 'JSONRPC via POST (Batch)' => sub { $request_post_batch->($cb) };
subtest 'JSONRPC Error' => sub {
my ($post_content, $req, $res, $json);
my $headers = HTTP::Headers->new( Content_Type => 'application/json',);
my $uri = URI->new( "http://localhost" );
$post_content = $coder->encode( [ method => "hoge"] );
$req = HTTP::Request->new( POST => $uri, $headers, $post_content );
$res = $cb->($req);
$json = $coder->decode( $res->decoded_content );
if (! is $json->{error}->{code}, RPC_INVALID_PARAMS ){
diag explain $json;
}
$post_content = "{ [[ broken json }";
$req = HTTP::Request->new( POST => $uri, $headers, $post_content );
$res = $cb->($req);
$json = $coder->decode( $res->decoded_content );
if (! is $json->{error}->{code}, RPC_PARSE_ERROR ) {
diag explain $json;
}
$post_content = "[]";
$req = HTTP::Request->new( POST => $uri, $headers, $post_content );
$res = $cb->($req);
$json = $coder->decode( $res->decoded_content );
if (! is $json->{error}->{code}, RPC_INVALID_REQUEST ){
diag explain $json;
}
# invalid method 'PUT'
$req = HTTP::Request->new( PUT => $uri );
$res = $cb->($req);
$json = $coder->decode( $res->decoded_content );
if (! is $json->{error}->{code}, RPC_INVALID_REQUEST ){
diag explain $json;
}
my $id = time();
$post_content = $coder->encode(
{
jsonrpc => '2.0',
id => $id,
method => 'tidy_error',
params => "foo",
}
);
$req = HTTP::Request->new( POST => $uri, $headers, $post_content);
$res = $cb->($req);
$json = $coder->decode( $res->decoded_content );
if (! is $json->{error}->{code}, RPC_INTERNAL_ERROR) {
diag explain $json;
}
is $json->{error}->{message}, 'short description of the error';
is $json->{error}->{data}, 'additional information about the error';
};
subtest 'JSONRPC Notification handling' => sub {
my ($post_content, $req, $res, $json);
my $headers = HTTP::Headers->new( Content_Type => 'application/json',);
my $uri = URI->new( "http://localhost" );
$post_content = $coder->encode( { method => "hoge", jsonrpc => '2.0' } );
$req = HTTP::Request->new( POST => $uri, $headers, $post_content );
$res = $cb->($req);
if (! ok $res->is_success, "Notification with nonexistent method: response is success") {
diag $res->as_string;
}
if (! is $res->code, 204, "Notification with nonexistent method: code is 204 for no content") {
diag $res->as_string;
}
if (! ok !length($res->content), "Notification with nonexistent method: content has no length") {
diag $res->as_string;
}
$post_content = $coder->encode( [
{
jsonrpc => '2.0',
method => 'blowup',
params => "fuga",
},
{
jsonrpc => '2.0',
id => undef,
method => 'blowup',
params => "fuga",
},
] );
$req = HTTP::Request->new( POST => $uri, $headers, $post_content );
$res = $cb->($req);
if (! ok $res->is_success, "Notification and a NULL id call: response is success") {
diag $res->as_string;
}
$json = $coder->decode( $res->decoded_content );
is scalar @$json, 1, "Notification and a NULL id call: response has one element";
ok (exists $json->[0]->{id} && !defined $json->[0]->{id}, "Notification and a NULL id call: response element has NULL id");
ok ($json->[0]->{error}, "Notification and a NULL id call: response element has an error");
$post_content = $coder->encode( [
{
jsonrpc => '2.0',
method => 'blowup',
params => "fuga",
},
{
jsonrpc => '2.0',
method => 'blowup',
params => "fuga2",
},
] );
$req = HTTP::Request->new( POST => $uri, $headers, $post_content );
$res = $cb->($req);
if (! ok $res->is_success, "Notification batch: response is success") {
diag $res->as_string;
}
if (! is $res->code, 204, "Notification batch: code is 204 for no content") {
diag $res->as_string;
}
if (! ok !length($res->content), "Notification batch: content has no length") {
diag $res->as_string;
}
};
};
};
done_testing;
|