2018-06-11 18:26:16 +00:00
|
|
|
#[
|
|
|
|
This module uses debug versions of the rpc components that
|
|
|
|
allow unchecked and unformatted calls.
|
|
|
|
]#
|
|
|
|
|
2018-06-15 10:11:10 +00:00
|
|
|
import unittest, debugclient, ../rpcstreamservers
|
2018-06-11 18:26:16 +00:00
|
|
|
import strformat, chronicles
|
|
|
|
|
2018-06-15 10:11:10 +00:00
|
|
|
var server = newRpcStreamServer("localhost", 8547.Port)
|
2018-06-11 18:26:16 +00:00
|
|
|
var client = newRpcClient()
|
|
|
|
|
|
|
|
server.start()
|
|
|
|
waitFor client.connect("localhost", Port(8547))
|
|
|
|
|
|
|
|
server.rpc("rpc") do(a: int, b: int):
|
|
|
|
result = %(&"a: {a}, b: {b}")
|
|
|
|
|
2018-06-13 18:13:04 +00:00
|
|
|
proc testMissingRpc: Future[Response] {.async.} =
|
|
|
|
var fut = client.call("phantomRpc", %[])
|
|
|
|
result = await fut
|
2018-06-12 18:30:14 +00:00
|
|
|
|
2018-06-13 18:13:04 +00:00
|
|
|
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-12 18:30:14 +00:00
|
|
|
|
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-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
|
2018-06-13 18:13:04 +00:00
|
|
|
test "Missing RPC":
|
2018-06-13 18:19:24 +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-13 18:13:04 +00:00
|
|
|
|
|
|
|
test "Incorrect json version":
|
|
|
|
let res = waitFor testInvalidJsonVer()
|
2018-06-13 18:19:24 +00:00
|
|
|
check res.error == true and res.result["message"] == %"JSON 2.0 required"
|
2018-06-13 18:13:04 +00:00
|
|
|
|
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.
|
|
|
|
let res = waitFor testMalformed()
|
|
|
|
check res.error == true and res.result == %"Timeout"
|
|
|
|
|