use jsonrpcmsg::{
Request, Response, Batch, Message, Params, Id, Error,
to_request_string, to_response_string, to_batch_string,
from_request_string, from_response_string, from_batch_string
};
use jsonrpcmsg::deserialize::DeserializationError;
use serde_json::{json, Map};
#[test]
fn test_request_serialization_deserialization() {
let array_params = vec![json!(42), json!(23)];
let request = Request::new_v2(
"subtract".to_string(),
Some(Params::Array(array_params)),
Some(Id::Number(1))
);
let json_string = to_request_string(&request).unwrap();
let parsed_request = from_request_string(&json_string).unwrap();
assert_eq!(request, parsed_request);
let mut map = Map::new();
map.insert("minuend".to_string(), json!(42));
map.insert("subtrahend".to_string(), json!(23));
let request2 = Request::new_v2(
"subtract".to_string(),
Some(Params::Object(map)),
Some(Id::String("abc".to_string()))
);
let json_string2 = to_request_string(&request2).unwrap();
let parsed_request2 = from_request_string(&json_string2).unwrap();
assert_eq!(request2, parsed_request2);
}
#[test]
fn test_response_serialization_deserialization() {
let result = json!(19);
let response = Response::success_v2(result, Some(Id::Number(1)));
let json_string = to_response_string(&response).unwrap();
let parsed_response = from_response_string(&json_string).unwrap();
assert_eq!(response, parsed_response);
let error = Error::method_not_found();
let response2 = Response::error_v2(error, Some(Id::Number(2)));
let json_string2 = to_response_string(&response2).unwrap();
let parsed_response2 = from_response_string(&json_string2).unwrap();
assert_eq!(response2, parsed_response2);
}
#[test]
fn test_batch_serialization_deserialization() {
let mut batch = Batch::new();
let request = Request::new_v2(
"subtract".to_string(),
Some(Params::Array(vec![json!(42), json!(23)])),
Some(Id::Number(1))
);
batch.add_request(request);
let result = json!(19);
let response = Response::success_v2(result, Some(Id::Number(1)));
batch.add_response(response);
let json_string = to_batch_string(&batch).unwrap();
let parsed_batch = from_batch_string(&json_string).unwrap();
assert_eq!(batch.len(), parsed_batch.len());
match (&batch[0], &parsed_batch[0]) {
(Message::Request(original), Message::Request(parsed)) => {
assert_eq!(original, parsed);
}
_ => panic!("Expected Request messages"),
}
match (&batch[1], &parsed_batch[1]) {
(Message::Response(original), Message::Response(parsed)) => {
assert_eq!(original, parsed);
}
_ => panic!("Expected Response messages"),
}
}
#[test]
fn test_notification_serialization() {
let notification = Request::notification_v2(
"update".to_string(),
Some(Params::Array(vec![json!(1), json!(2), json!(3)]))
);
let json_string = to_request_string(¬ification).unwrap();
let parsed_notification = from_request_string(&json_string).unwrap();
assert_eq!(notification, parsed_notification);
assert!(parsed_notification.is_notification());
}
#[test]
fn test_serialization_error() {
}
#[test]
fn test_deserialization_error() {
let invalid_json = "{ invalid json }";
let result = from_request_string(invalid_json);
assert!(result.is_err());
match result {
Err(DeserializationError::JsonError(_)) => {},
_ => panic!("Expected JsonError"),
}
}