nim-json-rpc/tests/testerrors.nim

81 lines
2.4 KiB
Nim
Raw Normal View History

2018-06-11 18:26:16 +00:00
#[
This module uses debug versions of the rpc components that
allow unchecked and unformatted calls.
]#
import unittest, debugclient, ../json_rpc/rpcserver
2018-06-11 18:26:16 +00:00
import strformat, chronicles
var server = newRpcSocketServer("localhost", Port(8545))
2018-07-12 13:08:43 +00:00
var client = newRpcSocketClient()
2018-06-11 18:26:16 +00:00
server.start()
waitFor client.connect("localhost", Port(8545))
2018-06-11 18:26:16 +00:00
server.rpc("rpc") do(a: int, b: int):
result = %(&"a: {a}, b: {b}")
2018-06-19 17:22:13 +00:00
server.rpc("makeError"):
if true:
raise newException(ValueError, "Test")
proc testMissingRpc: Future[Response] {.async.} =
var fut = client.call("phantomRpc", %[])
result = await fut
proc testInvalidJsonVer: Future[Response] {.async.} =
let json =
$ %{"jsonrpc": %"3.99", "method": %"rpc", "params": %[],
"id": % $client.nextId} & "\c\l"
var fut = client.rawCall("rpc", json)
result = await fut
2018-06-13 18:31:00 +00:00
proc testMalformed: Future[Response] {.async.} =
let malformedJson = "{field: 2, \"field: 3}"
var fut = client.rawCall("rpc", malformedJson)
await fut or sleepAsync(1000)
if fut.finished: result = fut.read()
else: result = (true, %"Timeout")
2018-06-19 17:22:13 +00:00
proc testRaise: Future[Response] {.async.} =
2018-07-11 17:27:50 +00:00
var fut = client.call("makeError", %[])
2018-06-19 17:22:13 +00:00
result = await fut
2018-06-11 18:26:16 +00:00
suite "RPC Errors":
2018-06-13 18:31:00 +00:00
# Note: We don't expect a exceptions for most of the tests,
# because the server should respond with the error in json
test "Missing RPC":
2018-06-21 17:40:49 +00:00
#expect ValueError:
try:
2018-06-19 17:22:13 +00:00
let res = waitFor testMissingRpc()
check res.error == true and
res.result["message"] == %"Method not found" and
res.result["data"] == %"phantomRpc is not a registered method."
2018-06-21 17:40:49 +00:00
except:
echo "Error ", getCurrentExceptionMsg()
2018-07-11 17:27:50 +00:00
#[test "Incorrect json version":
2018-06-21 17:40:49 +00:00
#expect ValueError:
try:
2018-06-19 17:22:13 +00:00
let res = waitFor testInvalidJsonVer()
check res.error == true and res.result["message"] == %"JSON 2.0 required"
2018-06-21 17:40:49 +00:00
except:
echo "Error ", getCurrentExceptionMsg()
2018-07-11 17:27:50 +00:00
]#
2018-06-19 17:22:13 +00:00
test "Raising exceptions":
2018-06-21 17:40:49 +00:00
#expect ValueError:
try:
2018-06-19 17:22:13 +00:00
let res = waitFor testRaise()
2018-06-21 17:40:49 +00:00
except:
echo "Error ", getCurrentExceptionMsg()
2018-06-13 18:31:00 +00:00
test "Malformed json":
# TODO: We time out here because the server won't be able to
# find an id to return to us, so we cannot complete the future.
2018-06-21 17:40:49 +00:00
try:
let res = waitFor testMalformed()
check res.error == true and res.result == %"Timeout"
except:
echo "Error ", getCurrentExceptionMsg()
2018-06-13 18:31:00 +00:00