Skip to content

Commit 253a286

Browse files
authored
Add unit-tests for the request/call (de)serialization (#326)
1 parent 4e95f43 commit 253a286

File tree

2 files changed

+95
-20
lines changed

2 files changed

+95
-20
lines changed

types/src/v2/params.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl<'a> Id<'a> {
196196

197197
#[cfg(test)]
198198
mod test {
199-
use super::{Cow, Id, JsonValue, RpcParams};
199+
use super::{Cow, Id, JsonRpcParams, JsonValue, RpcParams, SubscriptionId, TwoPointZero};
200200

201201
#[test]
202202
fn id_deserialization() {
@@ -228,6 +228,23 @@ mod test {
228228
assert_eq!(serialized, r#"[null,0,2,3,"3","test"]"#);
229229
}
230230

231+
#[test]
232+
fn params_serialize() {
233+
let test_vector = &[
234+
("null", JsonRpcParams::NoParams),
235+
("[42,23]", JsonRpcParams::Array(serde_json::from_str("[42,23]").unwrap())),
236+
(
237+
r#"{"a":42,"b":null,"c":"aa"}"#,
238+
JsonRpcParams::Map(serde_json::from_str(r#"{"a":42,"b":null,"c":"aa"}"#).unwrap()),
239+
),
240+
];
241+
242+
for (initial_ser, params) in test_vector {
243+
let serialized = serde_json::to_string(params).unwrap();
244+
assert_eq!(&serialized, initial_ser);
245+
}
246+
}
247+
231248
#[test]
232249
fn params_parse() {
233250
let none = RpcParams::new(None);
@@ -248,4 +265,25 @@ mod test {
248265
let obj: Result<JsonValue, _> = object_params.parse();
249266
assert!(obj.is_ok());
250267
}
268+
269+
#[test]
270+
fn two_point_zero_serde_works() {
271+
let initial_ser = r#""2.0""#;
272+
// The fact that it was deserialized is enough.
273+
let two_point_zero: TwoPointZero = serde_json::from_str(initial_ser).unwrap();
274+
let serialized = serde_json::to_string(&two_point_zero).unwrap();
275+
assert_eq!(serialized, initial_ser);
276+
}
277+
278+
#[test]
279+
fn subscription_id_serde_works() {
280+
let test_vector = &[("42", SubscriptionId::Num(42)), (r#""one""#, SubscriptionId::Str("one".into()))];
281+
282+
for (initial_ser, expected) in test_vector {
283+
let id: SubscriptionId = serde_json::from_str(initial_ser).unwrap();
284+
assert_eq!(&id, expected);
285+
let serialized = serde_json::to_string(&id).unwrap();
286+
assert_eq!(&serialized, initial_ser);
287+
}
288+
}
251289
}

types/src/v2/request.rs

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,35 @@ impl<'a> JsonRpcNotificationSer<'a> {
8282
#[cfg(test)]
8383
mod test {
8484
use super::{
85-
Id, JsonRpcCallSer, JsonRpcInvalidRequest, JsonRpcNotification, JsonRpcNotificationSer, JsonRpcRequest,
86-
TwoPointZero,
85+
Id, JsonRpcCallSer, JsonRpcInvalidRequest, JsonRpcNotification, JsonRpcNotificationSer, JsonRpcParams,
86+
JsonRpcRequest, TwoPointZero,
8787
};
88+
use serde_json::{value::RawValue, Value};
8889

90+
fn assert_request<'a>(request: JsonRpcRequest<'a>, id: Id<'a>, method: &str, params: Option<&str>) {
91+
assert_eq!(request.jsonrpc, TwoPointZero);
92+
assert_eq!(request.id, id);
93+
assert_eq!(request.method, method);
94+
assert_eq!(request.params.map(RawValue::get), params);
95+
}
96+
97+
/// Checks that we can deserialize the object with or without non-mandatory fields.
8998
#[test]
90-
fn deserialize_valid_call_works() {
91-
let ser = r#"{"jsonrpc":"2.0","method":"say_hello","params":[1,"bar"],"id":1}"#;
92-
let dsr: JsonRpcRequest = serde_json::from_str(ser).unwrap();
93-
assert_eq!(dsr.method, "say_hello");
94-
assert_eq!(dsr.jsonrpc, TwoPointZero);
99+
fn deserialize_request() {
100+
let method = "subtract";
101+
let params = "[42, 23]";
102+
103+
let test_vector = vec![
104+
// With all fields set.
105+
(r#"{"jsonrpc":"2.0", "method":"subtract", "params":[42, 23], "id":1}"#, Id::Number(1), Some(params)),
106+
// Without params field
107+
(r#"{"jsonrpc":"2.0", "method":"subtract", "id":null}"#, Id::Null, None),
108+
];
109+
110+
for (ser, id, params) in test_vector.into_iter() {
111+
let request = serde_json::from_str(ser).unwrap();
112+
assert_request(request, id, method, params);
113+
}
95114
}
96115

97116
#[test]
@@ -102,14 +121,6 @@ mod test {
102121
assert_eq!(dsr.jsonrpc, TwoPointZero);
103122
}
104123

105-
#[test]
106-
fn deserialize_valid_call_without_params_works() {
107-
let ser = r#"{"jsonrpc":"2.0","method":"say_hello", "id":1}"#;
108-
let dsr: JsonRpcRequest = serde_json::from_str(ser).unwrap();
109-
assert_eq!(dsr.method, "say_hello");
110-
assert_eq!(dsr.jsonrpc, TwoPointZero);
111-
}
112-
113124
// TODO(niklasad1): merge the types `JsonRpcParams` and `RpcParams` and remove `RawValue`.
114125
#[test]
115126
#[ignore]
@@ -131,12 +142,38 @@ mod test {
131142
assert_eq!(deserialized, JsonRpcInvalidRequest { id: Id::Number(120) });
132143
}
133144

145+
/// Checks that we can serialize the object with or without non-mandatory fields.
134146
#[test]
135147
fn serialize_call() {
136-
let exp = r#"{"jsonrpc":"2.0","method":"say_hello","id":"bar","params":[]}"#;
137-
let req = JsonRpcCallSer::new(Id::Str("bar".into()), "say_hello", vec![].into());
138-
let ser = serde_json::to_string(&req).unwrap();
139-
assert_eq!(exp, ser);
148+
let method = "subtract";
149+
let id = Id::Number(1); // It's enough to check one variant, since the type itself also has tests.
150+
let params: JsonRpcParams = vec![Value::Number(42.into()), Value::Number(23.into())].into(); // Same as above.
151+
let test_vector = &[
152+
// With all fields set.
153+
(
154+
r#"{"jsonrpc":"2.0","method":"subtract","id":1,"params":[42,23]}"#,
155+
Some(id.clone()),
156+
Some(params.clone()),
157+
),
158+
// Without ID field.
159+
(r#"{"jsonrpc":"2.0","method":"subtract","id":null,"params":[42,23]}"#, None, Some(params)),
160+
// Without params field
161+
(r#"{"jsonrpc":"2.0","method":"subtract","id":1,"params":null}"#, Some(id), None),
162+
// Without params and ID.
163+
(r#"{"jsonrpc":"2.0","method":"subtract","id":null,"params":null}"#, None, None),
164+
];
165+
166+
for (ser, id, params) in test_vector.iter().cloned() {
167+
let request = serde_json::to_string(&JsonRpcCallSer {
168+
jsonrpc: TwoPointZero,
169+
method,
170+
id: id.unwrap_or(Id::Null),
171+
params: params.unwrap_or(JsonRpcParams::NoParams),
172+
})
173+
.unwrap();
174+
175+
assert_eq!(&request, ser);
176+
}
140177
}
141178

142179
#[test]

0 commit comments

Comments
 (0)