rust_integration_services/http/
mod.rs

1#[cfg(feature = "http")]
2pub mod http_executor;
3#[cfg(feature = "http")]
4pub mod http_receiver;
5#[cfg(feature = "http")]
6pub mod http_sender;
7#[cfg(feature = "http")]
8pub mod http_request;
9#[cfg(feature = "http")]
10pub mod http_response;
11#[cfg(feature = "http")]
12pub mod http_status;
13#[cfg(feature = "http")]
14pub mod http_method;
15
16#[cfg(feature = "http")]
17#[cfg(test)]
18mod test {
19    use std::{env::home_dir, time::Duration};
20
21    use crate::http::{http_method::HttpMethod, http_receiver::{HttpReceiver, HttpReceiverEventSignal}, http_request::HttpRequest, http_response::HttpResponse, http_sender::HttpSender, http_status::HttpStatus};
22
23    #[tokio::test(start_paused = true)]
24    async fn http_receiver() {
25        tokio::spawn(async move {
26            HttpReceiver::new("127.0.0.1:8080")
27            .route("GET", "/", async move |_uuid, _request| {
28                HttpResponse::ok()
29            })
30            .on_event(async move |event| {
31                match event {
32                    HttpReceiverEventSignal::OnConnectionOpened(uuid, ip) => eprintln!("Connection[{}]: {}", uuid, ip),
33                    HttpReceiverEventSignal::OnRequest(uuid, request) => eprintln!("Request[{}]: {:?}", uuid, request),
34                    HttpReceiverEventSignal::OnResponse(uuid, response) => eprintln!("Response[{}]: {:?}", uuid, response),
35                    HttpReceiverEventSignal::OnConnectionFailed(uuid, err) => eprintln!("Failed[{}]: {}", uuid, err),
36                }
37            })
38            .receive()
39            .await;
40        });
41
42        tokio::time::advance(Duration::from_millis(100)).await;
43
44        let result = HttpSender::new().send("http://127.0.0.1:8080", HttpRequest::get()).await;
45        assert!(result.is_ok());
46        let response = result.unwrap();
47        assert_eq!(response.status.code(), 200);
48    }
49
50    /// Create your own certs for testing.
51    /// 
52    /// sudo dnf install mkcert
53    /// 
54    /// mkcert -install
55    /// 
56    /// mkcert localhost 127.0.0.1 ::1
57    #[tokio::test(start_paused = true)]
58    async fn http_receiver_tls() {
59        assert!(home_dir().is_some());
60        tokio::spawn(async move {
61            let server_cert_path = home_dir().unwrap().join(".config/rust-integration-services/certs/localhost+2.pem");
62            let server_key_path = home_dir().unwrap().join(".config/rust-integration-services/certs/localhost+2-key.pem");
63            HttpReceiver::new("127.0.0.1:8080")
64            .tls(server_cert_path, server_key_path)
65            .route("GET", "/", async move |_uuid, _request| {
66                HttpResponse::ok()
67            })
68            .on_event(async move |event| {
69                match event {
70                    HttpReceiverEventSignal::OnConnectionOpened(uuid, ip) => eprintln!("Connection[{}]: {}", uuid, ip),
71                    HttpReceiverEventSignal::OnRequest(uuid, request) => eprintln!("Request[{}]: {:?}", uuid, request),
72                    HttpReceiverEventSignal::OnResponse(uuid, response) => eprintln!("Response[{}]: {:?}", uuid, response),
73                    HttpReceiverEventSignal::OnConnectionFailed(uuid, err) => eprintln!("Failed[{}]: {}", uuid, err),
74                }
75            })
76            .receive()
77            .await;
78        });
79
80        tokio::time::advance(Duration::from_millis(200)).await;
81        let root_ca_path = home_dir().unwrap().join(".local/share/mkcert/rootCA.pem");
82        let client = HttpSender::new().add_root_ca(root_ca_path);
83        let result = client.send("https://127.0.0.1:8080", HttpRequest::get()).await;
84        assert!(result.is_ok());
85        let response = result.unwrap();
86        assert_eq!(response.status.code(), 200);
87    }
88
89    #[tokio::test]
90    async fn http_sender() {
91        let result = HttpSender::new().send("http://httpbin.org/get", HttpRequest::get()).await;
92        assert!(result.is_ok());
93    }
94
95    #[tokio::test]
96    async fn http_sender_tls() {
97        let result = HttpSender::new().send("https://httpbin.org/get", HttpRequest::get()).await;
98        assert!(result.is_ok());
99    }
100
101    #[tokio::test]
102    async fn http_status() {
103        assert_eq!(HttpStatus::Ok.code(), 200);
104        assert_eq!(HttpStatus::BadRequest.code(), 400);
105        assert_eq!(HttpStatus::NotFound.code(), 404);
106        assert_eq!(HttpStatus::InternalServerError.code(), 500);
107    }
108
109    #[tokio::test]
110    async fn http_method() {
111        assert_eq!(HttpMethod::Get.as_str(), "GET");
112        assert_eq!(HttpMethod::Post.as_str(), "POST");
113        assert_eq!(HttpMethod::Delete.as_str(), "DELETE");
114    }
115
116    #[tokio::test]
117    async fn http_request() {
118        let request = HttpRequest::get().body(b"test").header("test", "test");
119        assert_eq!(request.method.as_str(), "GET");
120        assert_eq!(request.body, b"test");
121        assert_eq!(request.headers.get("test").unwrap(), "test");
122    }
123
124    #[tokio::test]
125    async fn http_response() {
126        let response = HttpResponse::ok().body(b"test").header("test", "test");
127        assert_eq!(response.status.code(), 200);
128        assert_eq!(response.status.text(), "OK");
129        assert_eq!(response.body, b"test");
130        assert_eq!(response.headers.get("test").unwrap(), "test");
131    }
132}