Newer
Older
use crate::client::HttpClient;
use jsonrpsee_types::{
error::Error,
http::HttpConfig,
jsonrpc::{self, ErrorCode, JsonValue, Params},
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
};
use jsonrpsee_test_utils::helpers::*;
use jsonrpsee_test_utils::types::Id;
#[tokio::test]
async fn method_call_works() {
let result = run_request_with_response(ok_response("hello".into(), Id::Num(0))).await.unwrap();
assert_eq!(JsonValue::String("hello".into()), result);
}
#[tokio::test]
async fn notification_works() {
let server_addr = http_server_with_hardcoded_response(String::new()).await;
let uri = format!("http://{}", server_addr);
let client = HttpClient::new(&uri, HttpConfig::default()).unwrap();
client
.notification("i_dont_care_about_the_response_because_the_server_should_not_respond", Params::None)
.await
.unwrap();
}
#[tokio::test]
async fn response_with_wrong_id() {
let err = run_request_with_response(ok_response("hello".into(), Id::Num(99))).await.unwrap_err();
assert!(matches!(err, Error::InvalidRequestId));
}
#[tokio::test]
async fn response_method_not_found() {
let err = run_request_with_response(method_not_found(Id::Num(0))).await.unwrap_err();
assert_jsonrpc_error_response(err, ErrorCode::MethodNotFound, METHOD_NOT_FOUND.into());
}
#[tokio::test]
async fn response_parse_error() {
let err = run_request_with_response(parse_error(Id::Num(0))).await.unwrap_err();
assert_jsonrpc_error_response(err, ErrorCode::ParseError, PARSE_ERROR.into());
}
#[tokio::test]
async fn invalid_request_works() {
let err = run_request_with_response(invalid_request(Id::Num(0_u64))).await.unwrap_err();
assert_jsonrpc_error_response(err, ErrorCode::InvalidRequest, INVALID_REQUEST.into());
}
#[tokio::test]
async fn invalid_params_works() {
let err = run_request_with_response(invalid_params(Id::Num(0_u64))).await.unwrap_err();
assert_jsonrpc_error_response(err, ErrorCode::InvalidParams, INVALID_PARAMS.into());
}
#[tokio::test]
async fn internal_error_works() {
let err = run_request_with_response(internal_error(Id::Num(0_u64))).await.unwrap_err();
assert_jsonrpc_error_response(err, ErrorCode::InternalError, INTERNAL_ERROR.into());
}
#[tokio::test]
async fn subscription_response_to_request() {
let req = r#"{"jsonrpc":"2.0","method":"subscribe_hello","params":{"subscription":"3px4FrtxSYQ1zBKW154NoVnrDhrq764yQNCXEgZyM6Mu","result":"hello my friend"}}"#.to_string();
let err = run_request_with_response(req).await.unwrap_err();
assert!(matches!(err, Error::Custom(_)));
}
async fn run_request_with_response(response: String) -> Result<JsonValue, Error> {
let server_addr = http_server_with_hardcoded_response(response).await;
let uri = format!("http://{}", server_addr);
let client = HttpClient::new(&uri, HttpConfig::default())?;
client.request("say_hello", Params::None).await
}
fn assert_jsonrpc_error_response(response: Error, code: ErrorCode, message: String) {
let expected = jsonrpc::Error { code, message, data: None };
match response {
Error::Request(err) => {
assert_eq!(err, expected);
}
e @ _ => panic!("Expected error: \"{}\", got: {:?}", expected, e),
};
}