nanorpc_http/
lib.rs

1pub mod client;
2pub mod server;
3
4#[cfg(test)]
5mod tests {
6    use async_trait::async_trait;
7    use nanorpc::nanorpc_derive;
8    use smol::future::FutureExt;
9
10    use crate::{
11        client::{HttpRpcTransport, Proxy},
12        server::HttpRpcServer,
13    };
14
15    #[nanorpc_derive]
16    #[async_trait]
17    pub trait AddProtocol {
18        async fn add(&self, x: f64, y: f64) -> f64 {
19            x + y
20        }
21    }
22
23    struct AddImpl;
24
25    impl AddProtocol for AddImpl {}
26
27    #[test]
28    fn simple_pingpong() {
29        smol::future::block_on(async {
30            let service = AddService(AddImpl);
31            let server = HttpRpcServer::bind("127.0.0.1:12345".parse().unwrap())
32                .await
33                .unwrap();
34            async { server.run(service).await.unwrap() }
35                .race(async {
36                    let client =
37                        AddClient::from(HttpRpcTransport::new("127.0.0.1:12345".parse().unwrap()));
38                    assert_eq!(client.add(1.0, 2.0).await.unwrap(), 3.0);
39                })
40                .await
41        });
42    }
43}