nim-json-rpc/tests/testserverclient.nim

39 lines
1.3 KiB
Nim
Raw Normal View History

import ../ rpcclient, ../ rpcserver
import unittest, asyncdispatch, json, tables
from os import getCurrentDir, DirSep
from strutils import rsplit
2018-03-02 11:46:59 +00:00
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)
import stint
# generate all client ethereum rpc calls
createRpcSigs(currentSourcePath.rsplit(DirSep, 1)[0] & DirSep & "ethcallsigs.nim")
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("0x68656c6c6f20776f726c64")
check response == "0x47173285A8D7341E5E972FC677286384F802F8EF42A5EC5F03BBFA254CB01FAD"
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