Added the ability to handle omitted params fields in incoming requests, and a unit test for it (#94)

This commit is contained in:
Joe Clapis 2021-02-07 14:27:34 -05:00 committed by GitHub
parent b0e82cb70a
commit 831471f6d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 0 deletions

View File

@ -71,6 +71,9 @@ template jsonValid*(jsonString: string, node: var JsonNode): (bool, string) =
msg = "" msg = ""
try: try:
node = parseJson(line) node = parseJson(line)
# Handle cases where params is omitted
if not node.hasKey(paramsField):
node.add(paramsField, newJArray())
except CatchableError as exc: except CatchableError as exc:
valid = false valid = false
msg = exc.msg msg = exc.msg

View File

@ -56,6 +56,13 @@ const
"Connection: close\r\n" & "Connection: close\r\n" &
"\r\n" & "\r\n" &
"{\"jsonrpc\":\"2.0\",\"method\":\"myProc\",\"params\":[\"abc\", [1, 2, 3]],\"id\":67}", "{\"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.} = proc continuousTest(address: string, port: Port): Future[int] {.async.} =
@ -150,6 +157,8 @@ var httpsrv = newRpcHttpServer(["localhost:8545"])
# Create RPC on server # Create RPC on server
httpsrv.rpc("myProc") do(input: string, data: array[0..3, int]): httpsrv.rpc("myProc") do(input: string, data: array[0..3, int]):
result = %("Hello " & input & " data: " & $data) result = %("Hello " & input & " data: " & $data)
httpsrv.rpc("noParamsProc") do():
result = %("Hello world")
httpsrv.start() httpsrv.start()
@ -176,6 +185,8 @@ suite "HTTP Server/HTTP Client RPC test suite":
waitFor(disconTest("localhost", Port(8545), 6, 200)) == true waitFor(disconTest("localhost", Port(8545), 6, 200)) == true
test "[Connection]: close test": test "[Connection]: close test":
check waitFor(disconTest("localhost", Port(8545), 7, 200)) == true check waitFor(disconTest("localhost", Port(8545), 7, 200)) == true
test "Omitted params test":
check waitFor(simpleTest("localhost", Port(8545), 8, 200)) == true
httpsrv.stop() httpsrv.stop()
waitFor httpsrv.closeWait() waitFor httpsrv.closeWait()