@@ -21,9 +21,17 @@ pub trait Rpc {
21
21
#[ rpc( name = "protocolVersion" ) ]
22
22
fn protocol_version ( & self ) -> Result < String > ;
23
23
24
+ /// Negates number and returns a result
25
+ #[ rpc( name = "neg" ) ]
26
+ fn neg ( & self , i64 ) -> Result < i64 > ;
27
+
24
28
/// Adds two numbers and returns a result
25
29
#[ rpc( name = "add" ) ]
26
30
fn add ( & self , u64 , u64 ) -> Result < u64 > ;
31
+
32
+ /// Adds up to four numbers and returns a result
33
+ #[ rpc( name = "add_multi" ) ]
34
+ fn add_multi ( & self , u64 , Option < u64 > , Option < u64 > , Option < u64 > ) -> Result < u64 > ;
27
35
}
28
36
29
37
#[ derive( Default ) ]
@@ -34,9 +42,17 @@ impl Rpc for RpcImpl {
34
42
Ok ( "version1" . into ( ) )
35
43
}
36
44
45
+ fn neg ( & self , a : i64 ) -> Result < i64 > {
46
+ Ok ( -a)
47
+ }
48
+
37
49
fn add ( & self , a : u64 , b : u64 ) -> Result < u64 > {
38
50
Ok ( a + b)
39
51
}
52
+
53
+ fn add_multi ( & self , a : u64 , b : Option < u64 > , c : Option < u64 > , d : Option < u64 > ) -> Result < u64 > {
54
+ Ok ( a + b. unwrap_or_default ( ) + c. unwrap_or_default ( ) + d. unwrap_or_default ( ) )
55
+ }
40
56
}
41
57
42
58
#[ test]
@@ -70,3 +86,90 @@ fn should_accept_empty_array_as_no_params() {
70
86
let result3: Response = serde_json:: from_str ( & res3. unwrap ( ) ) . unwrap ( ) ;
71
87
assert_eq ! ( expected, result3) ;
72
88
}
89
+
90
+ #[ test]
91
+ fn should_accept_single_param ( ) {
92
+ let mut io = IoHandler :: new ( ) ;
93
+ let rpc = RpcImpl :: default ( ) ;
94
+ io. extend_with ( rpc. to_delegate ( ) ) ;
95
+
96
+ // when
97
+ let req1 = r#"{"jsonrpc":"2.0","id":1,"method":"neg","params":[1]}"# ;
98
+
99
+ let res1 = io. handle_request_sync ( req1) ;
100
+
101
+ // then
102
+ let result1: Response = serde_json:: from_str ( & res1. unwrap ( ) ) . unwrap ( ) ;
103
+ assert_eq ! ( result1, serde_json:: from_str( r#"{
104
+ "jsonrpc": "2.0",
105
+ "result": -1,
106
+ "id": 1
107
+ }"# ) . unwrap( ) ) ;
108
+ }
109
+
110
+ #[ test]
111
+ fn should_accept_multiple_params ( ) {
112
+ let mut io = IoHandler :: new ( ) ;
113
+ let rpc = RpcImpl :: default ( ) ;
114
+ io. extend_with ( rpc. to_delegate ( ) ) ;
115
+
116
+ // when
117
+ let req1 = r#"{"jsonrpc":"2.0","id":1,"method":"add","params":[1, 2]}"# ;
118
+
119
+ let res1 = io. handle_request_sync ( req1) ;
120
+
121
+ // then
122
+ let result1: Response = serde_json:: from_str ( & res1. unwrap ( ) ) . unwrap ( ) ;
123
+ assert_eq ! ( result1, serde_json:: from_str( r#"{
124
+ "jsonrpc": "2.0",
125
+ "result": 3,
126
+ "id": 1
127
+ }"# ) . unwrap( ) ) ;
128
+ }
129
+
130
+ #[ test]
131
+ fn should_accept_multiple_trailing_params ( ) {
132
+ let mut io = IoHandler :: new ( ) ;
133
+ let rpc = RpcImpl :: default ( ) ;
134
+ io. extend_with ( rpc. to_delegate ( ) ) ;
135
+
136
+ // when
137
+ let req1 = r#"{"jsonrpc":"2.0","id":1,"method":"add_multi","params":[1]}"# ;
138
+ let req2 = r#"{"jsonrpc":"2.0","id":1,"method":"add_multi","params":[1, 2]}"# ;
139
+ let req3 = r#"{"jsonrpc":"2.0","id":1,"method":"add_multi","params":[1, 2, 3]}"# ;
140
+ let req4 = r#"{"jsonrpc":"2.0","id":1,"method":"add_multi","params":[1, 2, 3, 4]}"# ;
141
+
142
+ let res1 = io. handle_request_sync ( req1) ;
143
+ let res2 = io. handle_request_sync ( req2) ;
144
+ let res3 = io. handle_request_sync ( req3) ;
145
+ let res4 = io. handle_request_sync ( req4) ;
146
+
147
+ // then
148
+ let result1: Response = serde_json:: from_str ( & res1. unwrap ( ) ) . unwrap ( ) ;
149
+ assert_eq ! ( result1, serde_json:: from_str( r#"{
150
+ "jsonrpc": "2.0",
151
+ "result": 1,
152
+ "id": 1
153
+ }"# ) . unwrap( ) ) ;
154
+
155
+ let result2: Response = serde_json:: from_str ( & res2. unwrap ( ) ) . unwrap ( ) ;
156
+ assert_eq ! ( result2, serde_json:: from_str( r#"{
157
+ "jsonrpc": "2.0",
158
+ "result": 3,
159
+ "id": 1
160
+ }"# ) . unwrap( ) ) ;
161
+
162
+ let result3: Response = serde_json:: from_str ( & res3. unwrap ( ) ) . unwrap ( ) ;
163
+ assert_eq ! ( result3, serde_json:: from_str( r#"{
164
+ "jsonrpc": "2.0",
165
+ "result": 6,
166
+ "id": 1
167
+ }"# ) . unwrap( ) ) ;
168
+
169
+ let result4: Response = serde_json:: from_str ( & res4. unwrap ( ) ) . unwrap ( ) ;
170
+ assert_eq ! ( result4, serde_json:: from_str( r#"{
171
+ "jsonrpc": "2.0",
172
+ "result": 10,
173
+ "id": 1
174
+ }"# ) . unwrap( ) ) ;
175
+ }
0 commit comments