Skip to content

Commit 1c2e689

Browse files
committed
Add handle_json block
1 parent 37b3308 commit 1c2e689

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

test/json_rpc_handler_test.rb

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
before do
77
@registry = {}
88
@response = nil
9+
@response_json = nil
910
end
1011

11-
describe '#handle_json' do
12+
describe '#handle' do
1213
# Comments verbatim from https://www.jsonrpc.org/specification
1314
#
1415
# JSON-RPC 2.0 Specification
@@ -221,12 +222,6 @@
221222
#
222223
# Either the result member or error member MUST be included, but both members MUST NOT be included.
223224

224-
it "returns an error with the id set to nil when the request is invalid" do
225-
handle_json("Invalid JSON")
226-
227-
assert_nil @response[:id]
228-
end
229-
230225
# 5.1 Error object
231226
#
232227
# When a rpc call encounters an error, the Response Object MUST contain the error member with a value that is a Object
@@ -277,12 +272,6 @@
277272
assert_rpc_error(expected_error: { code: -32603, message: "Internal error", data: "Something bad happened" })
278273
end
279274

280-
it "returns an error with the code set to -32700 there is a JSON parse error" do
281-
handle_json("Invalid JSON")
282-
283-
assert_rpc_error(expected_error: { code: -32700, message: "Parse error", data: "Invalid JSON" })
284-
end
285-
286275
# 6 Batch
287276
#
288277
# To send several Request objects at the same time, the Client MAY send an Array filled with Request objects.
@@ -367,6 +356,38 @@
367356
# system extension is defined in a related specification. All system extensions are OPTIONAL.
368357
end
369358

359+
describe '#handle_json' do
360+
it "returns a Response object when the request is valid and not a notification" do
361+
register("add") do |params|
362+
params[:a] + params[:b]
363+
end
364+
365+
handle_json({ jsonrpc: "2.0", id: 1, method: "add", params: { a: 1, b: 2 } }.to_json)
366+
367+
assert_rpc_success(expected_result: 3)
368+
end
369+
370+
it "returns nil for notifications" do
371+
register("ping") {}
372+
373+
handle_json({ jsonrpc: "2.0", method: "ping" }.to_json)
374+
375+
assert_nil @response
376+
end
377+
378+
it "returns an error with the id set to nil when the request is invalid" do
379+
handle_json("Invalid JSON")
380+
381+
assert_nil @response[:id]
382+
end
383+
384+
it "returns an error with the code set to -32700 there is a JSON parse error" do
385+
handle_json("Invalid JSON")
386+
387+
assert_rpc_error(expected_error: { code: -32700, message: "Parse error", data: "Invalid JSON" })
388+
end
389+
end
390+
370391
private
371392

372393
def register(method_name, &block)
@@ -378,8 +399,8 @@ def handle(request, &find_method)
378399
end
379400

380401
def handle_json(request_json)
381-
response_json = JsonRpcHandler.handle_json(request_json) { |method_name| @registry[method_name] }
382-
@response = JSON.parse(response_json, symbolize_names: true) if response_json
402+
@response_json = JsonRpcHandler.handle_json(request_json) { |method_name| @registry[method_name] }
403+
@response = JSON.parse(@response_json, symbolize_names: true) if @response_json
383404
end
384405

385406
def assert_rpc_success(expected_result:)

0 commit comments

Comments
 (0)