From 831471f6d4650b0a8a2bbfac65a22690bb97471e Mon Sep 17 00:00:00 2001 From: Joe Clapis Date: Sun, 7 Feb 2021 14:27:34 -0500 Subject: [PATCH] Added the ability to handle omitted params fields in incoming requests, and a unit test for it (#94) --- json_rpc/router.nim | 3 +++ tests/testhttp.nim | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/json_rpc/router.nim b/json_rpc/router.nim index 2aa5f08..0dc2eb3 100644 --- a/json_rpc/router.nim +++ b/json_rpc/router.nim @@ -71,6 +71,9 @@ template jsonValid*(jsonString: string, node: var JsonNode): (bool, string) = msg = "" try: node = parseJson(line) + # Handle cases where params is omitted + if not node.hasKey(paramsField): + node.add(paramsField, newJArray()) except CatchableError as exc: valid = false msg = exc.msg diff --git a/tests/testhttp.nim b/tests/testhttp.nim index 04c9dca..39154c5 100644 --- a/tests/testhttp.nim +++ b/tests/testhttp.nim @@ -56,6 +56,13 @@ const "Connection: close\r\n" & "\r\n" & "{\"jsonrpc\":\"2.0\",\"method\":\"myProc\",\"params\":[\"abc\", [1, 2, 3]],\"id\":67}", + "GET / HTTP/1.1\r\n" & + "Host: www.google.com\r\n" & + "Content-Length: 49\r\n" & + "Content-Type: application/json\r\n" & + "Connection: close\r\n" & + "\r\n" & + "{\"jsonrpc\":\"2.0\",\"method\":\"noParamsProc\",\"id\":67}", ] proc continuousTest(address: string, port: Port): Future[int] {.async.} = @@ -150,6 +157,8 @@ var httpsrv = newRpcHttpServer(["localhost:8545"]) # Create RPC on server httpsrv.rpc("myProc") do(input: string, data: array[0..3, int]): result = %("Hello " & input & " data: " & $data) +httpsrv.rpc("noParamsProc") do(): + result = %("Hello world") httpsrv.start() @@ -176,6 +185,8 @@ suite "HTTP Server/HTTP Client RPC test suite": waitFor(disconTest("localhost", Port(8545), 6, 200)) == true test "[Connection]: close test": check waitFor(disconTest("localhost", Port(8545), 7, 200)) == true + test "Omitted params test": + check waitFor(simpleTest("localhost", Port(8545), 8, 200)) == true httpsrv.stop() waitFor httpsrv.closeWait()