Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/json_rpc_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def process_request(request, id_validation_pattern:, &method_finder)
'Method name must be a string and not start with "rpc."'
end

return error_response(id: :unknown_id, id_validation_pattern: id_validation_pattern, error: {
return error_response(id: id, id_validation_pattern: id_validation_pattern, error: {
code: ErrorCode::INVALID_REQUEST,
message: "Invalid Request",
data: error,
Expand Down
21 changes: 17 additions & 4 deletions test/json_rpc_handler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@
end

it "returns an error when jsonrpc is not 2.0" do
handle jsonrpc: "3.0", id: 1, method: "add", params: { a: 1, b: 2 }
handle jsonrpc: "3.0", id: 3, method: "add", params: { a: 1, b: 2 }

assert_rpc_error expected_error: {
code: -32600,
message: "Invalid Request",
data: "JSON-RPC version must be 2.0",
}
assert_equal 3, @response[:id]
end

# method
Expand All @@ -51,13 +52,14 @@
# used for anything else.

it "returns an error when method is not a string" do
handle jsonrpc: "2.0", id: 1, method: 42, params: { a: 1, b: 2 }
handle jsonrpc: "2.0", id: 8, method: 42, params: { a: 1, b: 2 }

assert_rpc_error expected_error: {
code: -32600,
message: "Invalid Request",
data: 'Method name must be a string and not start with "rpc."',
}
assert_equal 8, @response[:id]
end

it "returns an error when method begins with 'rpc.'" do
Expand Down Expand Up @@ -756,8 +758,19 @@
assert_nil @response
end

it "returns an error with the id set to nil when the request is invalid" do
handle_json({ jsonrpc: "0.0", id: 1, method: "add", params: { a: 1, b: 2 } }.to_json)
it "returns an error with the request id when the JSON-RPC version is missing" do
handle_json({ id: 4, method: "add", params: { a: 1, b: 2 } }.to_json)

assert_rpc_error expected_error: {
code: -32600,
message: "Invalid Request",
data: "JSON-RPC version must be 2.0",
}
assert_equal 4, @response[:id]
end

it "returns an error with the id set to nil when the request id is invalid" do
handle_json({ jsonrpc: "2.0", id: {}, method: "add", params: { a: 1, b: 2 } }.to_json)

assert_nil @response[:id]
end
Expand Down