testcontainers_modules/parity_parity/
mod.rs

1use std::borrow::Cow;
2
3use testcontainers::{core::WaitFor, Image};
4
5const NAME: &str = "parity/parity";
6const TAG: &str = "v2.5.0";
7
8#[allow(missing_docs)]
9// not having docs here is currently allowed to address the missing docs problem one place at a time. Helping us by documenting just one of these places helps other devs tremendously
10#[derive(Debug, Default, Clone)]
11pub struct ParityEthereum {
12    /// (remove if there is another variable)
13    /// Field is included to prevent this struct to be a unit struct.
14    /// This allows extending functionality (and thus further variables) without breaking changes
15    _priv: (),
16}
17
18impl Image for ParityEthereum {
19    fn name(&self) -> &str {
20        NAME
21    }
22
23    fn tag(&self) -> &str {
24        TAG
25    }
26
27    fn ready_conditions(&self) -> Vec<WaitFor> {
28        vec![WaitFor::message_on_stderr("Public node URL:")]
29    }
30
31    fn cmd(&self) -> impl IntoIterator<Item = impl Into<Cow<'_, str>>> {
32        [
33            "--config=dev",
34            "--jsonrpc-apis=all",
35            "--unsafe-expose",
36            "--tracing=on",
37        ]
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use testcontainers::runners::SyncRunner;
44
45    use crate::parity_parity;
46
47    #[test]
48    fn parity_parity_net_version() -> Result<(), Box<dyn std::error::Error + 'static>> {
49        let _ = pretty_env_logger::try_init();
50        let node = parity_parity::ParityEthereum::default().start()?;
51        let host_ip = node.get_host()?;
52        let host_port = node.get_host_port_ipv4(8545)?;
53
54        let response = reqwest::blocking::Client::new()
55            .post(format!("http://{host_ip}:{host_port}"))
56            .body(
57                serde_json::json!({
58                    "jsonrpc": "2.0",
59                    "method": "net_version",
60                    "params": [],
61                    "id": 1
62                })
63                .to_string(),
64            )
65            .header("content-type", "application/json")
66            .send()
67            .unwrap();
68
69        let response = response.text().unwrap();
70        let response: serde_json::Value = serde_json::from_str(&response).unwrap();
71
72        assert_eq!(response["result"], "17");
73        Ok(())
74    }
75}