2024-01-03 20:06:53 +07:00
|
|
|
# json-rpc
|
|
|
|
|
# Copyright (c) 2019-2023 Status Research & Development GmbH
|
|
|
|
|
# Licensed under either of
|
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
|
# at your option.
|
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
|
# those terms.
|
|
|
|
|
|
2020-03-17 22:05:42 +02:00
|
|
|
import
|
2024-01-03 20:06:53 +07:00
|
|
|
unittest2,
|
2022-11-08 14:39:29 +01:00
|
|
|
../json_rpc/[rpcclient, rpcserver]
|
2018-03-02 11:46:59 +00:00
|
|
|
|
2018-06-05 10:45:54 +01:00
|
|
|
# Create RPC on server
|
2021-06-23 16:09:44 +07:00
|
|
|
proc setupServer*(srv: RpcServer) =
|
|
|
|
|
srv.rpc("myProc") do(input: string, data: array[0..3, int]):
|
|
|
|
|
return %("Hello " & input & " data: " & $data)
|
|
|
|
|
|
|
|
|
|
srv.rpc("myError") do(input: string, data: array[0..3, int]):
|
|
|
|
|
raise (ref ValueError)(msg: "someMessage")
|
2021-02-15 13:45:51 +01:00
|
|
|
|
2022-02-28 09:39:49 +07:00
|
|
|
srv.rpc("invalidRequest") do():
|
|
|
|
|
raise (ref InvalidRequest)(code: -32001, msg: "Unknown payload")
|
|
|
|
|
|
2021-06-23 16:09:44 +07:00
|
|
|
suite "Socket Server/Client RPC":
|
2024-01-07 14:40:59 +07:00
|
|
|
var srv = newRpcSocketServer(["127.0.0.1:0"])
|
2021-06-23 16:09:44 +07:00
|
|
|
var client = newRpcSocketClient()
|
2018-04-12 18:51:06 +01:00
|
|
|
|
2021-06-23 16:09:44 +07:00
|
|
|
srv.setupServer()
|
|
|
|
|
srv.start()
|
2024-01-07 14:40:59 +07:00
|
|
|
waitFor client.connect(srv.localAddress()[0])
|
2018-04-24 13:43:04 +01:00
|
|
|
|
2021-02-15 13:45:51 +01:00
|
|
|
test "Successful RPC call":
|
|
|
|
|
let r = waitFor client.call("myProc", %[%"abc", %[1, 2, 3, 4]])
|
2024-01-03 20:06:53 +07:00
|
|
|
check r.string == "\"Hello abc data: [1, 2, 3, 4]\""
|
2021-02-15 13:45:51 +01:00
|
|
|
|
|
|
|
|
test "Missing params":
|
|
|
|
|
expect(CatchableError):
|
|
|
|
|
discard waitFor client.call("myProc", %[%"abc"])
|
|
|
|
|
|
|
|
|
|
test "Error RPC call":
|
|
|
|
|
expect(CatchableError): # The error type wont be translated
|
|
|
|
|
discard waitFor client.call("myError", %[%"abc", %[1, 2, 3, 4]])
|
2018-06-07 11:20:33 +03:00
|
|
|
|
2022-02-28 09:39:49 +07:00
|
|
|
test "Invalid request exception":
|
|
|
|
|
try:
|
|
|
|
|
discard waitFor client.call("invalidRequest", %[])
|
|
|
|
|
check false
|
|
|
|
|
except CatchableError as e:
|
2024-01-03 20:06:53 +07:00
|
|
|
check e.msg == """{"code":-32001,"message":"Unknown payload"}"""
|
2022-02-28 09:39:49 +07:00
|
|
|
|
2021-06-23 16:09:44 +07:00
|
|
|
srv.stop()
|
|
|
|
|
waitFor srv.closeWait()
|
2021-06-23 18:44:14 +07:00
|
|
|
|
|
|
|
|
suite "Websocket Server/Client RPC":
|
2024-01-07 14:40:59 +07:00
|
|
|
var srv = newRpcWebSocketServer("127.0.0.1", Port(0))
|
2021-06-23 18:44:14 +07:00
|
|
|
var client = newRpcWebSocketClient()
|
|
|
|
|
|
|
|
|
|
srv.setupServer()
|
|
|
|
|
srv.start()
|
2024-01-07 14:40:59 +07:00
|
|
|
waitFor client.connect("ws://" & $srv.localAddress())
|
2021-06-23 18:44:14 +07:00
|
|
|
|
|
|
|
|
test "Successful RPC call":
|
|
|
|
|
let r = waitFor client.call("myProc", %[%"abc", %[1, 2, 3, 4]])
|
2024-01-03 20:06:53 +07:00
|
|
|
check r.string == "\"Hello abc data: [1, 2, 3, 4]\""
|
2021-06-23 18:44:14 +07:00
|
|
|
|
|
|
|
|
test "Missing params":
|
|
|
|
|
expect(CatchableError):
|
|
|
|
|
discard waitFor client.call("myProc", %[%"abc"])
|
|
|
|
|
|
|
|
|
|
test "Error RPC call":
|
|
|
|
|
expect(CatchableError): # The error type wont be translated
|
|
|
|
|
discard waitFor client.call("myError", %[%"abc", %[1, 2, 3, 4]])
|
|
|
|
|
|
2022-02-28 09:39:49 +07:00
|
|
|
test "Invalid request exception":
|
|
|
|
|
try:
|
|
|
|
|
discard waitFor client.call("invalidRequest", %[])
|
|
|
|
|
check false
|
|
|
|
|
except CatchableError as e:
|
2024-01-03 20:06:53 +07:00
|
|
|
check e.msg == """{"code":-32001,"message":"Unknown payload"}"""
|
2022-02-28 09:39:49 +07:00
|
|
|
|
2021-06-23 18:44:14 +07:00
|
|
|
srv.stop()
|
|
|
|
|
waitFor srv.closeWait()
|
2021-06-23 19:08:07 +07:00
|
|
|
|
|
|
|
|
suite "Websocket Server/Client RPC with Compression":
|
2024-01-07 14:40:59 +07:00
|
|
|
var srv = newRpcWebSocketServer("127.0.0.1", Port(0),
|
2022-11-08 14:39:29 +01:00
|
|
|
compression = true)
|
2021-06-23 19:08:07 +07:00
|
|
|
var client = newRpcWebSocketClient()
|
|
|
|
|
|
|
|
|
|
srv.setupServer()
|
|
|
|
|
srv.start()
|
2024-01-07 14:40:59 +07:00
|
|
|
waitFor client.connect("ws://" & $srv.localAddress(),
|
2022-11-08 14:39:29 +01:00
|
|
|
compression = true)
|
2021-06-23 19:08:07 +07:00
|
|
|
|
|
|
|
|
test "Successful RPC call":
|
|
|
|
|
let r = waitFor client.call("myProc", %[%"abc", %[1, 2, 3, 4]])
|
2024-01-03 20:06:53 +07:00
|
|
|
check r.string == "\"Hello abc data: [1, 2, 3, 4]\""
|
2021-06-23 19:08:07 +07:00
|
|
|
|
|
|
|
|
test "Missing params":
|
|
|
|
|
expect(CatchableError):
|
|
|
|
|
discard waitFor client.call("myProc", %[%"abc"])
|
|
|
|
|
|
|
|
|
|
test "Error RPC call":
|
|
|
|
|
expect(CatchableError): # The error type wont be translated
|
|
|
|
|
discard waitFor client.call("myError", %[%"abc", %[1, 2, 3, 4]])
|
|
|
|
|
|
2022-02-28 09:39:49 +07:00
|
|
|
test "Invalid request exception":
|
|
|
|
|
try:
|
|
|
|
|
discard waitFor client.call("invalidRequest", %[])
|
|
|
|
|
check false
|
|
|
|
|
except CatchableError as e:
|
2024-01-03 20:06:53 +07:00
|
|
|
check e.msg == """{"code":-32001,"message":"Unknown payload"}"""
|
2022-02-28 09:39:49 +07:00
|
|
|
|
2021-06-23 19:08:07 +07:00
|
|
|
srv.stop()
|
|
|
|
|
waitFor srv.closeWait()
|
2024-01-03 20:06:53 +07:00
|
|
|
|
2024-07-23 11:05:54 +01:00
|
|
|
suite "Custom processClient":
|
|
|
|
|
test "Should be able to use custom processClient":
|
|
|
|
|
var wasCalled: bool = false
|
2024-10-22 21:58:46 +02:00
|
|
|
|
|
|
|
|
proc processClientHook(server: StreamServer, transport: StreamTransport) {.async: (raises: []).} =
|
2024-07-23 11:05:54 +01:00
|
|
|
wasCalled = true
|
2024-10-22 21:58:46 +02:00
|
|
|
|
2024-07-23 11:05:54 +01:00
|
|
|
var srv = newRpcSocketServer(processClientHook)
|
|
|
|
|
srv.addStreamServer("localhost", Port(8888))
|
|
|
|
|
var client = newRpcSocketClient()
|
|
|
|
|
srv.setupServer()
|
|
|
|
|
srv.start()
|
|
|
|
|
waitFor client.connect(srv.localAddress()[0])
|
|
|
|
|
asyncCheck client.call("", %[])
|
|
|
|
|
srv.stop()
|
|
|
|
|
waitFor srv.closeWait()
|
|
|
|
|
check wasCalled
|