mirror of
https://github.com/logos-storage/nim-json-rpc.git
synced 2026-01-07 08:03:07 +00:00
Added the ability to handle omitted params fields in incoming requests, and a unit test for it (#94)
This commit is contained in:
parent
b0e82cb70a
commit
831471f6d4
@ -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
|
||||||
|
|||||||
@ -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()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user