2019-12-02 11:14:00 +00:00
|
|
|
import unittest, json, chronicles, options
|
2020-03-17 20:05:42 +00:00
|
|
|
import ../json_rpc/rpcserver, ./helpers
|
2018-04-26 18:37:02 +00:00
|
|
|
|
2018-05-08 15:26:13 +00:00
|
|
|
type
|
|
|
|
# some nested types to check object parsing
|
|
|
|
Test2 = object
|
|
|
|
x: array[0..2, int]
|
|
|
|
y: string
|
|
|
|
|
|
|
|
Test = object
|
|
|
|
a: array[0..1, int]
|
|
|
|
b: Test2
|
|
|
|
|
2018-08-28 20:19:12 +00:00
|
|
|
MyObject = object
|
2018-05-08 15:26:13 +00:00
|
|
|
a: int
|
|
|
|
b: Test
|
|
|
|
c: float
|
2018-08-28 20:19:12 +00:00
|
|
|
|
|
|
|
MyOptional = object
|
|
|
|
maybeInt: Option[int]
|
|
|
|
|
2018-11-23 16:11:12 +00:00
|
|
|
MyOptionalNotBuiltin = object
|
|
|
|
val: Option[Test2]
|
|
|
|
|
2018-05-08 15:26:13 +00:00
|
|
|
let
|
|
|
|
testObj = %*{
|
|
|
|
"a": %1,
|
|
|
|
"b": %*{
|
|
|
|
"a": %[5, 0],
|
|
|
|
"b": %*{
|
|
|
|
"x": %[1, 2, 3],
|
|
|
|
"y": %"test"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"c": %1.23}
|
|
|
|
|
2018-07-11 09:19:13 +00:00
|
|
|
var s = newRpcSocketServer(["localhost:8545"])
|
2018-05-08 15:26:13 +00:00
|
|
|
|
|
|
|
# RPC definitions
|
2018-06-07 07:02:14 +00:00
|
|
|
s.rpc("rpc.simplepath"):
|
2018-04-26 18:37:02 +00:00
|
|
|
result = %1
|
2018-05-08 15:26:13 +00:00
|
|
|
|
2018-06-07 07:02:14 +00:00
|
|
|
s.rpc("rpc.differentparams") do(a: int, b: string):
|
2018-05-09 16:31:28 +00:00
|
|
|
result = %[%a, %b]
|
2018-05-08 15:26:13 +00:00
|
|
|
|
2018-05-21 23:13:00 +00:00
|
|
|
s.rpc("rpc.arrayparam") do(arr: array[0..5, byte], b: string):
|
2018-05-09 16:31:28 +00:00
|
|
|
var res = %arr
|
2018-04-26 18:37:02 +00:00
|
|
|
res.add %b
|
|
|
|
result = %res
|
2018-05-08 15:26:13 +00:00
|
|
|
|
2018-05-21 23:13:00 +00:00
|
|
|
s.rpc("rpc.seqparam") do(a: string, s: seq[int]):
|
2018-04-26 18:37:02 +00:00
|
|
|
var res = newJArray()
|
2018-05-08 15:26:13 +00:00
|
|
|
res.add %a
|
2018-04-26 18:37:02 +00:00
|
|
|
for item in s:
|
|
|
|
res.add %int(item)
|
|
|
|
result = res
|
2018-05-08 15:26:13 +00:00
|
|
|
|
2018-05-21 23:13:00 +00:00
|
|
|
s.rpc("rpc.objparam") do(a: string, obj: MyObject):
|
2018-04-26 18:37:02 +00:00
|
|
|
result = %obj
|
2018-05-08 15:26:13 +00:00
|
|
|
|
2018-05-21 23:13:00 +00:00
|
|
|
s.rpc("rpc.returntypesimple") do(i: int) -> int:
|
2018-05-08 16:30:05 +00:00
|
|
|
result = i
|
|
|
|
|
2018-05-21 23:13:00 +00:00
|
|
|
s.rpc("rpc.returntypecomplex") do(i: int) -> Test2:
|
2018-05-08 16:30:05 +00:00
|
|
|
result.x = [1, i, 3]
|
|
|
|
result.y = "test"
|
|
|
|
|
2018-05-21 23:13:00 +00:00
|
|
|
s.rpc("rpc.testreturns") do() -> int:
|
2018-05-16 22:16:20 +00:00
|
|
|
return 1234
|
|
|
|
|
2018-07-17 03:06:29 +00:00
|
|
|
s.rpc("rpc.multivarsofonetype") do(a, b: string) -> string:
|
|
|
|
result = a & " " & b
|
|
|
|
|
2018-08-28 20:19:12 +00:00
|
|
|
s.rpc("rpc.optional") do(obj: MyOptional) -> MyOptional:
|
|
|
|
result = obj
|
|
|
|
|
2018-11-12 10:47:03 +00:00
|
|
|
s.rpc("rpc.optionalArg") do(val: int, obj: Option[MyOptional]) -> MyOptional:
|
|
|
|
if obj.isSome():
|
|
|
|
result = obj.get()
|
|
|
|
else:
|
|
|
|
result = MyOptional(maybeInt: some(val))
|
|
|
|
|
2018-11-16 13:07:39 +00:00
|
|
|
type
|
|
|
|
OptionalFields = object
|
|
|
|
a: int
|
|
|
|
b: Option[int]
|
|
|
|
c: string
|
|
|
|
d: Option[int]
|
|
|
|
e: Option[string]
|
|
|
|
|
|
|
|
s.rpc("rpc.mixedOptionalArg") do(a: int, b: Option[int], c: string,
|
|
|
|
d: Option[int], e: Option[string]) -> OptionalFields:
|
|
|
|
|
|
|
|
result.a = a
|
|
|
|
result.b = b
|
|
|
|
result.c = c
|
|
|
|
result.d = d
|
|
|
|
result.e = e
|
|
|
|
|
2018-11-23 16:11:12 +00:00
|
|
|
s.rpc("rpc.optionalArgNotBuiltin") do(obj: Option[MyOptionalNotBuiltin]) -> string:
|
2018-11-23 16:56:00 +00:00
|
|
|
result = "Empty1"
|
|
|
|
if obj.isSome:
|
|
|
|
let val = obj.get.val
|
|
|
|
result = "Empty2"
|
|
|
|
if val.isSome:
|
|
|
|
result = obj.get.val.get.y
|
2018-11-23 16:11:12 +00:00
|
|
|
|
2018-11-26 18:03:56 +00:00
|
|
|
type
|
|
|
|
MaybeOptions = object
|
|
|
|
o1: Option[bool]
|
|
|
|
o2: Option[bool]
|
|
|
|
o3: Option[bool]
|
|
|
|
|
|
|
|
s.rpc("rpc.optInObj") do(data: string, options: Option[MaybeOptions]) -> int:
|
|
|
|
if options.isSome:
|
|
|
|
let o = options.get
|
|
|
|
if o.o1.isSome: result += 1
|
|
|
|
if o.o2.isSome: result += 2
|
|
|
|
if o.o3.isSome: result += 4
|
2019-05-09 14:34:01 +00:00
|
|
|
|
2018-05-08 15:26:13 +00:00
|
|
|
# Tests
|
2018-04-26 18:37:02 +00:00
|
|
|
suite "Server types":
|
|
|
|
test "On macro registration":
|
2018-07-06 16:47:43 +00:00
|
|
|
check s.hasMethod("rpc.simplepath")
|
|
|
|
check s.hasMethod("rpc.differentparams")
|
|
|
|
check s.hasMethod("rpc.arrayparam")
|
|
|
|
check s.hasMethod("rpc.seqparam")
|
|
|
|
check s.hasMethod("rpc.objparam")
|
|
|
|
check s.hasMethod("rpc.returntypesimple")
|
|
|
|
check s.hasMethod("rpc.returntypecomplex")
|
|
|
|
check s.hasMethod("rpc.testreturns")
|
2018-07-17 03:06:29 +00:00
|
|
|
check s.hasMethod("rpc.multivarsofonetype")
|
2018-11-12 10:47:03 +00:00
|
|
|
check s.hasMethod("rpc.optionalArg")
|
2018-11-26 18:03:56 +00:00
|
|
|
check s.hasMethod("rpc.mixedOptionalArg")
|
|
|
|
check s.hasMethod("rpc.optionalArgNotBuiltin")
|
2018-11-26 18:33:47 +00:00
|
|
|
check s.hasMethod("rpc.optInObj")
|
2018-05-08 15:26:13 +00:00
|
|
|
|
2018-05-09 16:31:28 +00:00
|
|
|
test "Simple paths":
|
|
|
|
let r = waitFor rpcSimplePath(%[])
|
2020-03-17 20:05:42 +00:00
|
|
|
check r == "1"
|
2018-05-09 16:31:28 +00:00
|
|
|
|
|
|
|
test "Different param types":
|
|
|
|
let
|
|
|
|
inp = %[%1, %"abc"]
|
|
|
|
r = waitFor rpcDifferentParams(inp)
|
|
|
|
check r == inp
|
|
|
|
|
2018-05-08 15:26:13 +00:00
|
|
|
test "Array parameters":
|
2018-04-26 18:37:02 +00:00
|
|
|
let r1 = waitfor rpcArrayParam(%[%[1, 2, 3], %"hello"])
|
|
|
|
var ckR1 = %[1, 2, 3, 0, 0, 0]
|
|
|
|
ckR1.elems.add %"hello"
|
|
|
|
check r1 == ckR1
|
|
|
|
|
2018-05-08 15:26:13 +00:00
|
|
|
test "Seq parameters":
|
2018-04-26 18:37:02 +00:00
|
|
|
let r2 = waitfor rpcSeqParam(%[%"abc", %[1, 2, 3, 4, 5]])
|
|
|
|
var ckR2 = %["abc"]
|
|
|
|
for i in 0..4: ckR2.add %(i + 1)
|
|
|
|
check r2 == ckR2
|
2018-05-08 15:26:13 +00:00
|
|
|
|
2018-04-26 18:37:02 +00:00
|
|
|
test "Object parameters":
|
2018-05-08 15:26:13 +00:00
|
|
|
let r = waitfor rpcObjParam(%[%"abc", testObj])
|
|
|
|
check r == testObj
|
|
|
|
|
2018-05-08 16:30:05 +00:00
|
|
|
test "Simple return types":
|
|
|
|
let
|
|
|
|
inp = %99
|
|
|
|
r1 = waitfor rpcReturnTypeSimple(%[%inp])
|
|
|
|
check r1 == inp
|
|
|
|
|
|
|
|
test "Complex return types":
|
|
|
|
let
|
|
|
|
inp = 99
|
|
|
|
r1 = waitfor rpcReturnTypeComplex(%[%inp])
|
|
|
|
check r1 == %*{"x": %[1, inp, 3], "y": "test"}
|
|
|
|
|
2018-08-28 20:19:12 +00:00
|
|
|
test "Option types":
|
|
|
|
let
|
|
|
|
inp1 = MyOptional(maybeInt: some(75))
|
|
|
|
inp2 = MyOptional()
|
|
|
|
r1 = waitfor rpcOptional(%[%inp1])
|
|
|
|
r2 = waitfor rpcOptional(%[%inp2])
|
|
|
|
check r1 == %inp1
|
|
|
|
check r2 == %inp2
|
|
|
|
|
2018-05-16 22:16:20 +00:00
|
|
|
test "Return statement":
|
|
|
|
let r = waitFor rpcTestReturns(%[])
|
|
|
|
check r == %1234
|
|
|
|
|
2018-04-26 18:37:02 +00:00
|
|
|
test "Runtime errors":
|
|
|
|
expect ValueError:
|
2018-05-08 19:36:13 +00:00
|
|
|
# root param not array
|
|
|
|
discard waitfor rpcArrayParam(%"test")
|
|
|
|
expect ValueError:
|
|
|
|
# too big for array
|
2018-04-26 18:37:02 +00:00
|
|
|
discard waitfor rpcArrayParam(%[%[0, 1, 2, 3, 4, 5, 6], %"hello"])
|
2018-05-08 19:36:13 +00:00
|
|
|
expect ValueError:
|
|
|
|
# wrong sub parameter type
|
|
|
|
discard waitfor rpcArrayParam(%[%"test", %"hello"])
|
2018-05-09 16:31:28 +00:00
|
|
|
expect ValueError:
|
|
|
|
# wrong param type
|
|
|
|
let res = waitFor rpcDifferentParams(%[%"abc", %1])
|
|
|
|
# TODO: When errors are proper return values, check error for param name
|
2018-06-07 08:20:33 +00:00
|
|
|
|
2018-07-17 03:06:29 +00:00
|
|
|
test "Multiple variables of one type":
|
|
|
|
let r = waitfor rpcMultiVarsOfOneType(%[%"hello", %"world"])
|
|
|
|
check r == %"hello world"
|
|
|
|
|
2018-11-12 10:47:03 +00:00
|
|
|
test "Optional arg":
|
|
|
|
let
|
|
|
|
int1 = MyOptional(maybeInt: some(75))
|
|
|
|
int2 = MyOptional(maybeInt: some(117))
|
|
|
|
r1 = waitFor rpcOptionalArg(%[%117, %int1])
|
|
|
|
r2 = waitFor rpcOptionalArg(%[%117])
|
|
|
|
check r1 == %int1
|
|
|
|
check r2 == %int2
|
|
|
|
|
2018-11-23 16:11:12 +00:00
|
|
|
test "Mixed optional arg":
|
2018-11-16 13:07:39 +00:00
|
|
|
var ax = waitFor rpcMixedOptionalArg(%[%10, %11, %"hello", %12, %"world"])
|
|
|
|
check ax == %OptionalFields(a: 10, b: some(11), c: "hello", d: some(12), e: some("world"))
|
|
|
|
var bx = waitFor rpcMixedOptionalArg(%[%10, newJNull(), %"hello"])
|
|
|
|
check bx == %OptionalFields(a: 10, c: "hello")
|
|
|
|
|
2018-11-23 16:11:12 +00:00
|
|
|
test "Non-built-in optional types":
|
|
|
|
let
|
2018-11-23 16:56:00 +00:00
|
|
|
t2 = Test2(x: [1, 2, 3], y: "Hello")
|
|
|
|
testOpts1 = MyOptionalNotBuiltin(val: some(t2))
|
|
|
|
testOpts2 = MyOptionalNotBuiltin()
|
|
|
|
var r = waitFor rpcOptionalArgNotBuiltin(%[%testOpts1])
|
|
|
|
check r == %t2.y
|
|
|
|
var r2 = waitFor rpcOptionalArgNotBuiltin(%[])
|
|
|
|
check r2 == %"Empty1"
|
|
|
|
var r3 = waitFor rpcOptionalArgNotBuiltin(%[%testOpts2])
|
|
|
|
check r3 == %"Empty2"
|
2018-11-23 16:11:12 +00:00
|
|
|
|
2018-11-26 18:03:56 +00:00
|
|
|
test "Manually set up JSON for optionals":
|
|
|
|
# Check manual set up json with optionals
|
2018-11-26 18:12:26 +00:00
|
|
|
let opts1 = parseJson("""{"o1": true}""")
|
|
|
|
var r1 = waitFor rpcOptInObj(%[%"0x31ded", opts1])
|
|
|
|
check r1 == %1
|
|
|
|
let opts2 = parseJson("""{"o2": true}""")
|
|
|
|
var r2 = waitFor rpcOptInObj(%[%"0x31ded", opts2])
|
|
|
|
check r2 == %2
|
|
|
|
let opts3 = parseJson("""{"o3": true}""")
|
|
|
|
var r3 = waitFor rpcOptInObj(%[%"0x31ded", opts3])
|
|
|
|
check r3 == %4
|
|
|
|
# Combinations
|
|
|
|
let opts4 = parseJson("""{"o1": true, "o3": true}""")
|
|
|
|
var r4 = waitFor rpcOptInObj(%[%"0x31ded", opts4])
|
|
|
|
check r4 == %5
|
|
|
|
let opts5 = parseJson("""{"o2": true, "o3": true}""")
|
|
|
|
var r5 = waitFor rpcOptInObj(%[%"0x31ded", opts5])
|
|
|
|
check r5 == %6
|
|
|
|
let opts6 = parseJson("""{"o1": true, "o2": true}""")
|
|
|
|
var r6 = waitFor rpcOptInObj(%[%"0x31ded", opts6])
|
|
|
|
check r6 == %3
|
2018-11-26 18:03:56 +00:00
|
|
|
|
2018-06-07 08:20:33 +00:00
|
|
|
s.stop()
|
2018-09-13 18:20:16 +00:00
|
|
|
waitFor s.closeWait()
|
2018-11-16 13:07:39 +00:00
|
|
|
|