nim-json-rpc/tests/testserverclient.nim

38 lines
1.4 KiB
Nim
Raw Normal View History

import ../ rpcclient, ../ rpcserver, unittest, asyncdispatch, json, tables
2018-03-02 11:46:59 +00:00
#[
TODO: Importing client before server causes the error:
2018-05-09 16:17:33 +01:00
Error: undeclared identifier: 'result' for the `myProc` RPC.
This is because the RPC procs created by clientdispatch clash with ethprocs.
Currently, easiest solution is to import rpcserver (and therefore generate
ethprocs) before rpcclient.
]#
2018-04-24 13:43:04 +01:00
# TODO: dummy implementations of RPC calls handled in async fashion.
# TODO: check required json parameters like version are being raised
2018-04-24 13:43:04 +01:00
var srv = sharedRpcServer()
srv.address = "localhost"
srv.port = Port(8545)
srv.rpc("myProc") do(input: string, data: array[0..3, int]):
2018-04-24 19:22:16 +01:00
result = %("Hello " & input & " data: " & $data)
2018-04-12 18:51:06 +01:00
2018-04-12 19:29:48 +01:00
asyncCheck srv.serve
2018-04-24 13:43:04 +01:00
2018-04-24 13:50:56 +01:00
suite "RPC":
proc main {.async.} =
var client = newRpcClient()
await client.connect("localhost", Port(8545))
2018-04-24 13:50:56 +01:00
test "Version":
var response = waitFor client.web3_clientVersion()
check response == "Nimbus-RPC-Test"
2018-04-24 13:50:56 +01:00
test "SHA3":
var response = waitFor client.web3_sha3("abc")
check response == "3A985DA74FE225B2045C172D6BD390BD855F086E3E9D525B46BFE24511431532"
2018-04-24 13:50:56 +01:00
test "Custom RPC":
# Custom async RPC call
var response = waitFor client.call("myProc", %[%"abc", %[1, 2, 3, 4]])
2018-04-24 19:22:16 +01:00
check response.result.getStr == "Hello abc data: [1, 2, 3, 4]"
2018-04-26 19:37:19 +01:00
2018-05-17 19:13:44 +01:00
waitFor main() # TODO: When an error occurs during a test, stop the server
2018-04-24 13:50:56 +01:00