|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -require 'test_helper' |
| 3 | +require "test_helper" |
4 | 4 |
|
5 | 5 | describe JsonRpcHandler do |
6 | 6 | before do |
|
9 | 9 | @response_json = nil |
10 | 10 | end |
11 | 11 |
|
12 | | - describe '#handle' do |
| 12 | + describe "#handle" do |
13 | 13 | # Comments verbatim from https://www.jsonrpc.org/specification |
14 | 14 | # |
15 | 15 | # JSON-RPC 2.0 Specification |
|
41 | 41 | assert_rpc_error expected_error: { |
42 | 42 | code: -32600, |
43 | 43 | message: "Invalid Request", |
44 | | - data: "JSON-RPC version must be 2.0" |
| 44 | + data: "JSON-RPC version must be 2.0", |
45 | 45 | } |
46 | 46 | end |
47 | 47 |
|
|
229 | 229 | # method's expected parameters. |
230 | 230 |
|
231 | 231 | it "with array params returns a result" do |
232 | | - register("sum") { |params| params.sum } |
| 232 | + register("sum", &:sum) |
233 | 233 |
|
234 | 234 | handle jsonrpc: "2.0", id: 1, method: "sum", params: [1, 2, 3] |
235 | 235 |
|
|
346 | 346 | assert_rpc_error expected_error: { |
347 | 347 | code: -32700, |
348 | 348 | message: "Parse error", |
349 | | - data: "Invalid JSON" |
| 349 | + data: "Invalid JSON", |
350 | 350 | } |
351 | 351 | end |
352 | 352 |
|
|
360 | 360 | } |
361 | 361 | end |
362 | 362 |
|
363 | | - |
364 | 363 | it "returns an error with the code set to -32601 when the method does not exist" do |
365 | 364 | handle jsonrpc: "2.0", id: 1, method: "add", params: { a: 1, b: 2 } |
366 | 365 |
|
367 | 366 | assert_rpc_error expected_error: { |
368 | 367 | code: -32601, |
369 | 368 | message: "Method not found", |
370 | | - data: "add" |
| 369 | + data: "add", |
371 | 370 | } |
372 | 371 | end |
373 | 372 |
|
|
395 | 394 | assert_rpc_error expected_error: { |
396 | 395 | code: -32603, |
397 | 396 | message: "Internal error", |
398 | | - data: "Something bad happened" |
| 397 | + data: "Something bad happened", |
399 | 398 | } |
400 | 399 | end |
401 | 400 |
|
|
438 | 437 | ] |
439 | 438 |
|
440 | 439 | assert @response.is_a?(Array) |
441 | | - assert @response.all? { |result| result[:jsonrpc] == "2.0"} |
| 440 | + assert @response.all? { |result| result[:jsonrpc] == "2.0" } |
442 | 441 | assert_equal [100, 200], @response.map { |result| result[:id] } |
443 | 442 | assert_equal [3, 12], @response.map { |result| result[:result] } |
444 | 443 | assert @response.all? { |result| result[:error].nil? } |
|
455 | 454 | ] |
456 | 455 |
|
457 | 456 | assert @response.is_a?(Array) |
458 | | - assert @response.all? { |result| result[:jsonrpc] == "2.0"} |
| 457 | + assert @response.all? { |result| result[:jsonrpc] == "2.0" } |
459 | 458 | assert_equal [100, 200], @response.map { |result| result[:id] } |
460 | 459 | assert_equal [3, 5], @response.map { |result| result[:result] } |
461 | 460 | assert @response.all? { |result| result[:error].nil? } |
|
531 | 530 |
|
532 | 531 | @response = JsonRpcHandler.handle( |
533 | 532 | { jsonrpc: "2.0", id: "[email protected]", method: "add", params: { a: 1, b: 2 } }, |
534 | | - id_validation_pattern: custom_pattern |
| 533 | + id_validation_pattern: custom_pattern, |
535 | 534 | ) { |method_name| @registry[method_name] } |
536 | 535 |
|
537 | 536 | assert_rpc_success expected_result: 3 |
|
543 | 542 |
|
544 | 543 | @response = JsonRpcHandler.handle( |
545 | 544 | { jsonrpc: "2.0", id: "id<script>", method: "add", params: { a: 1, b: 2 } }, |
546 | | - id_validation_pattern: custom_pattern |
| 545 | + id_validation_pattern: custom_pattern, |
547 | 546 | ) { |method_name| @registry[method_name] } |
548 | 547 |
|
549 | 548 | assert_rpc_error expected_error: { |
|
559 | 558 |
|
560 | 559 | @response_json = JsonRpcHandler.handle_json( |
561 | 560 | { jsonrpc: "2.0", id: "[email protected]", method: "add", params: { a: 1, b: 2 } }.to_json, |
562 | | - id_validation_pattern: custom_pattern |
| 561 | + id_validation_pattern: custom_pattern, |
563 | 562 | ) { |method_name| @registry[method_name] } |
564 | 563 | @response = JSON.parse(@response_json, symbolize_names: true) |
565 | 564 |
|
|
577 | 576 | { jsonrpc: "2.0", id: "req@1", method: "add", params: { a: 1, b: 2 } }, |
578 | 577 | { jsonrpc: "2.0", id: "req@2", method: "mul", params: { a: 3, b: 4 } }, |
579 | 578 | ], |
580 | | - id_validation_pattern: custom_pattern |
| 579 | + id_validation_pattern: custom_pattern, |
581 | 580 | ) { |method_name| @registry[method_name] } |
582 | 581 |
|
583 | 582 | assert @response.is_a?(Array) |
|
592 | 591 |
|
593 | 592 | @response = JsonRpcHandler.handle( |
594 | 593 | { jsonrpc: "2.0", id: "[email protected]", method: "add", params: { a: 1, b: 2 } }, |
595 | | - id_validation_pattern: custom_pattern |
| 594 | + id_validation_pattern: custom_pattern, |
596 | 595 | ) { |method_name| @registry[method_name] } |
597 | 596 |
|
598 | 597 | assert_rpc_success expected_result: 3 |
|
604 | 603 |
|
605 | 604 | @response = JsonRpcHandler.handle( |
606 | 605 | { jsonrpc: "2.0", id: "<script>alert('xss')</script>", method: "add", params: { a: 1, b: 2 } }, |
607 | | - id_validation_pattern: nil |
| 606 | + id_validation_pattern: nil, |
608 | 607 | ) { |method_name| @registry[method_name] } |
609 | 608 |
|
610 | 609 | assert_rpc_success expected_result: 3 |
|
613 | 612 | end |
614 | 613 | end |
615 | 614 |
|
616 | | - describe '#handle_json' do |
| 615 | + describe "#handle_json" do |
617 | 616 | it "returns a Response object when the request is valid and not a notification" do |
618 | 617 | register("add") { |params| params[:a] + params[:b] } |
619 | 618 |
|
@@ -653,12 +652,12 @@ def handle_json(request_json) |
653 | 652 | end |
654 | 653 |
|
655 | 654 | def assert_rpc_success(expected_result:) |
656 | | - assert_equal expected_result, @response[:result] |
657 | | - assert_nil @response[:error] |
| 655 | + assert_equal(expected_result, @response[:result]) |
| 656 | + assert_nil(@response[:error]) |
658 | 657 | end |
659 | 658 |
|
660 | 659 | def assert_rpc_error(expected_error:) |
661 | | - assert_equal expected_error, @response[:error] |
662 | | - assert_nil @response[:result] |
| 660 | + assert_equal(expected_error, @response[:error]) |
| 661 | + assert_nil(@response[:result]) |
663 | 662 | end |
664 | 663 | end |
0 commit comments