Skip to content

Commit 47f9809

Browse files
committed
test parsing and network request
1 parent 9c4c519 commit 47f9809

File tree

1 file changed

+91
-0
lines changed
  • crates/settings/src/remote/chains

1 file changed

+91
-0
lines changed

crates/settings/src/remote/chains/mod.rs

+91
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,94 @@ impl RemoteNetworks {
3232
}
3333
}
3434
}
35+
36+
#[cfg(test)]
37+
mod tests {
38+
use super::*;
39+
40+
#[tokio::test]
41+
async fn test_try_from_remote_network_config_source_ok_case() {
42+
let remote_networks_config_source = RemoteNetworksConfigSource {
43+
format: "chainid".to_string(),
44+
url: "https://chainid.network/chains.json".to_string(),
45+
};
46+
47+
let result =
48+
RemoteNetworks::try_from_remote_network_config_source(remote_networks_config_source)
49+
.await
50+
.unwrap();
51+
52+
let RemoteNetworks::ChainId(chain_ids) = result;
53+
54+
assert_ne!(chain_ids.len(), 0);
55+
}
56+
57+
#[tokio::test]
58+
async fn test_try_from_remote_network_config_source_invalid_response_err_case() {
59+
let remote_networks_config_source = RemoteNetworksConfigSource {
60+
format: "chainid".to_string(),
61+
url: "https://example.com/".to_string(),
62+
};
63+
64+
let result =
65+
RemoteNetworks::try_from_remote_network_config_source(remote_networks_config_source)
66+
.await;
67+
68+
if let Err(RemoteNetworkError::ReqwestError(err)) = &result {
69+
if err.is_decode() {
70+
return;
71+
}
72+
}
73+
74+
panic!("Expected RemoteNetworkError::ReqwestError decoding error");
75+
}
76+
77+
#[tokio::test]
78+
async fn test_try_from_remote_network_config_source_invalid_url_err_case() {
79+
let remote_networks_config_source = RemoteNetworksConfigSource {
80+
format: "chainid".to_string(),
81+
url: "https://invalid.url/unreal".to_string(),
82+
};
83+
84+
let result =
85+
RemoteNetworks::try_from_remote_network_config_source(remote_networks_config_source)
86+
.await;
87+
88+
if let Err(RemoteNetworkError::ReqwestError(err)) = &result {
89+
if err.is_connect() {
90+
return;
91+
}
92+
}
93+
94+
panic!("Expected RemoteNetworkError::ReqwestError connection error");
95+
}
96+
97+
#[tokio::test]
98+
async fn test_try_from_remote_network_config_source_format_parsing_err_case() {
99+
let remote_networks_config_sources = vec![
100+
RemoteNetworksConfigSource {
101+
format: "".to_string(),
102+
url: "https://example.com".to_string(),
103+
},
104+
RemoteNetworksConfigSource {
105+
format: "unknown".to_string(),
106+
url: "https://polygon-rpc.com/".to_string(),
107+
},
108+
];
109+
110+
for remote_networks_config_source in remote_networks_config_sources {
111+
let unexpected_format = remote_networks_config_source.format.clone();
112+
113+
let result = RemoteNetworks::try_from_remote_network_config_source(
114+
remote_networks_config_source,
115+
)
116+
.await;
117+
118+
if let Err(RemoteNetworkError::UnknownFormat(format)) = result {
119+
assert_eq!(format, unexpected_format);
120+
} else {
121+
panic!("Expected RemoteNetworkError::UnknownFormat error");
122+
}
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)