2024-01-07 14:40:59 +07:00
# json-rpc
# Copyright (c) 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.
2024-01-03 20:06:53 +07:00
import
unittest2 ,
.. / json_rpc / router ,
2024-02-02 14:14:37 +07:00
json_serialization / std / options ,
json_serialization / stew / results
2024-01-03 20:06:53 +07:00
var server = RpcRouter ( )
2024-02-02 14:14:37 +07:00
type
OptAlias [ T ] = results . Opt [ T ]
server . rpc ( " std_option " ) do ( A : int , B : Option [ int ] , C : string , D : Option [ int ] , E : Option [ string ] ) - > string :
2024-01-03 20:06:53 +07:00
var res = " A: " & $ A
res . add " , B: " & $ B . get ( 99 )
res . add " , C: " & C
res . add " , D: " & $ D . get ( 77 )
res . add " , E: " & E . get ( " none " )
return res
2024-02-02 14:14:37 +07:00
server . rpc ( " results_opt " ) do ( A : int , B : Opt [ int ] , C : string , D : Opt [ int ] , E : Opt [ string ] ) - > string :
var res = " A: " & $ A
res . add " , B: " & $ B . get ( 99 )
res . add " , C: " & C
res . add " , D: " & $ D . get ( 77 )
res . add " , E: " & E . get ( " none " )
return res
server . rpc ( " mixed_opt " ) do ( A : int , B : Opt [ int ] , C : string , D : Option [ int ] , E : Opt [ string ] ) - > string :
var res = " A: " & $ A
res . add " , B: " & $ B . get ( 99 )
res . add " , C: " & C
res . add " , D: " & $ D . get ( 77 )
res . add " , E: " & E . get ( " none " )
return res
server . rpc ( " alias_opt " ) do ( A : int , B : OptAlias [ int ] , C : string , D : Option [ int ] , E : OptAlias [ string ] ) - > string :
var res = " A: " & $ A
res . add " , B: " & $ B . get ( 99 )
res . add " , C: " & C
res . add " , D: " & $ D . get ( 77 )
res . add " , E: " & E . get ( " none " )
return res
2024-01-03 20:06:53 +07:00
server . rpc ( " noParams " ) do ( ) - > int :
return 123
server . rpc ( " emptyParams " ) :
return % 777
server . rpc ( " comboParams " ) do ( a , b , c : int ) - > int :
return a + b + c
server . rpc ( " returnJsonString " ) do ( a , b , c : int ) - > JsonString :
return JsonString ( $ ( a + b + c ) )
func req ( meth : string , params : string ) : string =
""" { " jsonrpc " : " 2.0 " , " id " :0, " method " : """ &
" \" " & meth & " \" , \" params \" : " & params & " } "
2024-02-02 14:14:37 +07:00
template test_optional ( meth : static [ string ] ) =
test meth & " B E, positional " :
let n = req ( meth , " [44, null, \" apple \" , 33] " )
2024-01-03 20:06:53 +07:00
let res = waitFor server . route ( n )
check res = = """ { " jsonrpc " : " 2.0 " , " id " :0, " result " : " A: 44, B: 99, C: apple, D: 33, E: none " } """
2024-02-02 14:14:37 +07:00
test meth & " B D E, positional " :
let n = req ( meth , " [44, null, \" apple \" ] " )
2024-01-03 20:06:53 +07:00
let res = waitFor server . route ( n )
check res = = """ { " jsonrpc " : " 2.0 " , " id " :0, " result " : " A: 44, B: 99, C: apple, D: 77, E: none " } """
2024-02-02 14:14:37 +07:00
test meth & " D E, positional " :
let n = req ( meth , " [44, 567, \" apple \" ] " )
2024-01-03 20:06:53 +07:00
let res = waitFor server . route ( n )
check res = = """ { " jsonrpc " : " 2.0 " , " id " :0, " result " : " A: 44, B: 567, C: apple, D: 77, E: none " } """
2024-02-02 14:14:37 +07:00
test meth & " D wrong E, positional " :
let n = req ( meth , " [44, 567, \" apple \" , \" banana \" ] " )
2024-01-03 20:06:53 +07:00
let res = waitFor server . route ( n )
2024-02-02 14:14:37 +07:00
when meth = = " std_option " :
check res = = """ { " jsonrpc " : " 2.0 " , " id " :0, " error " :{ " code " :-32000, " message " : " `std_option` raised an exception " , " data " : " Parameter [D] of type ' Option[system.int] ' could not be decoded: number expected " }} """
elif meth = = " results_opt " :
check res = = """ { " jsonrpc " : " 2.0 " , " id " :0, " error " :{ " code " :-32000, " message " : " `results_opt` raised an exception " , " data " : " Parameter [D] of type ' Opt[system.int] ' could not be decoded: number expected " }} """
elif meth = = " mixed_opt " :
check res = = """ { " jsonrpc " : " 2.0 " , " id " :0, " error " :{ " code " :-32000, " message " : " `mixed_opt` raised an exception " , " data " : " Parameter [D] of type ' Option[system.int] ' could not be decoded: number expected " }} """
else :
check res = = """ { " jsonrpc " : " 2.0 " , " id " :0, " error " :{ " code " :-32000, " message " : " `alias_opt` raised an exception " , " data " : " Parameter [D] of type ' Option[system.int] ' could not be decoded: number expected " }} """
test meth & " D extra, positional " :
let n = req ( meth , " [44, 567, \" apple \" , 999, \" banana \" , true] " )
2024-01-03 20:06:53 +07:00
let res = waitFor server . route ( n )
check res = = """ { " jsonrpc " : " 2.0 " , " id " :0, " result " : " A: 44, B: 567, C: apple, D: 999, E: banana " } """
2024-02-02 14:14:37 +07:00
test meth & " B D E, named " :
let n = req ( meth , """ { " A " : 33, " C " : " banana " } """ )
2024-01-03 20:06:53 +07:00
let res = waitFor server . route ( n )
check res = = """ { " jsonrpc " : " 2.0 " , " id " :0, " result " : " A: 33, B: 99, C: banana, D: 77, E: none " } """
2024-02-02 14:14:37 +07:00
test meth & " B E, D front, named " :
let n = req ( meth , """ { " D " : 8887, " A " : 33, " C " : " banana " } """ )
2024-01-03 20:06:53 +07:00
let res = waitFor server . route ( n )
check res = = """ { " jsonrpc " : " 2.0 " , " id " :0, " result " : " A: 33, B: 99, C: banana, D: 8887, E: none " } """
2024-02-02 14:14:37 +07:00
test meth & " B E, D front, extra X, named " :
let n = req ( meth , """ { " D " : 8887, " X " : false , " A " : 33, " C " : " banana " } """ )
2024-01-03 20:06:53 +07:00
let res = waitFor server . route ( n )
check res = = """ { " jsonrpc " : " 2.0 " , " id " :0, " result " : " A: 33, B: 99, C: banana, D: 8887, E: none " } """
2024-02-02 14:14:37 +07:00
suite " rpc router " :
test " no params " :
let n = req ( " noParams " , " [] " )
let res = waitFor server . route ( n )
check res = = """ { " jsonrpc " : " 2.0 " , " id " :0, " result " :123} """
test " no params with params " :
let n = req ( " noParams " , " [123] " )
let res = waitFor server . route ( n )
2024-10-04 03:50:33 +00:00
check res = = """ { " jsonrpc " : " 2.0 " , " id " :0, " error " :{ " code " :-32000, " message " : " `noParams` raised an exception " , " data " : " Expected 0 JSON parameter(s) but got 1 " }} """
2024-02-02 14:14:37 +07:00
test_optional ( " std_option " )
test_optional ( " results_opt " )
test_optional ( " mixed_opt " )
test_optional ( " alias_opt " )
2024-01-03 20:06:53 +07:00
test " empty params " :
let n = req ( " emptyParams " , " [] " )
let res = waitFor server . route ( n )
check res = = """ { " jsonrpc " : " 2.0 " , " id " :0, " result " :777} """
test " combo params " :
let n = req ( " comboParams " , " [6,7,8] " )
let res = waitFor server . route ( n )
check res = = """ { " jsonrpc " : " 2.0 " , " id " :0, " result " :21} """
test " return json string " :
let n = req ( " returnJsonString " , " [6,7,8] " )
let res = waitFor server . route ( n )
check res = = """ { " jsonrpc " : " 2.0 " , " id " :0, " result " :21} """