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, 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("/", async move |_uuid, _request| {
28                HttpResponse::ok()
29            })
30            .receive()
31            .await;
32        });
33
34        tokio::time::advance(Duration::from_millis(200)).await;
35        let result = HttpSender::new().send("http://127.0.0.1:8080", HttpRequest::get()).await;
36        assert!(result.is_ok());
37        let response = result.unwrap();
38        assert_eq!(response.status.code(), 200);
39    }
40
41    /// Create your own certs for testing.
42    /// 
43    /// sudo dnf install mkcert
44    /// 
45    /// mkcert -install
46    /// 
47    /// mkcert localhost 127.0.0.1 ::1
48    #[tokio::test(start_paused = true)]
49    async fn http_receiver_tls() {
50        assert!(home_dir().is_some());
51        tokio::spawn(async move {
52            let server_cert_path = home_dir().unwrap().join(".config/rust-integration-services/certs/localhost+2.pem");
53            let server_key_path = home_dir().unwrap().join(".config/rust-integration-services/certs/localhost+2-key.pem");
54            HttpReceiver::new("127.0.0.1:8080")
55            .tls(server_cert_path, server_key_path)
56            .route("/", async move |_uuid, _request| {
57                HttpResponse::ok()
58            })
59            .receive()
60            .await;
61        });
62
63        tokio::time::advance(Duration::from_millis(200)).await;
64        let root_ca_path = home_dir().unwrap().join(".local/share/mkcert/rootCA.pem");
65        let client = HttpSender::new().add_root_ca(root_ca_path);
66        let result = client.send("https://127.0.0.1:8080", HttpRequest::get()).await;
67        assert!(result.is_ok());
68        let response = result.unwrap();
69        assert_eq!(response.status.code(), 200);
70    }
71
72    #[tokio::test]
73    async fn http_sender() {
74        let result = HttpSender::new().send("http://httpbin.org/get", HttpRequest::get()).await;
75        assert!(result.is_ok());
76    }
77
78    #[tokio::test]
79    async fn http_sender_tls() {
80        let result = HttpSender::new().send("https://httpbin.org/get", HttpRequest::get()).await;
81        assert!(result.is_ok());
82    }
83
84    #[tokio::test]
85    async fn http_status() {
86        assert_eq!(HttpStatus::Ok.code(), 200);
87        assert_eq!(HttpStatus::BadRequest.code(), 400);
88        assert_eq!(HttpStatus::NotFound.code(), 404);
89        assert_eq!(HttpStatus::InternalServerError.code(), 500);
90    }
91
92    #[tokio::test]
93    async fn http_method() {
94        assert_eq!(HttpMethod::Get.as_str(), "GET");
95        assert_eq!(HttpMethod::Post.as_str(), "POST");
96        assert_eq!(HttpMethod::Delete.as_str(), "DELETE");
97    }
98
99    #[tokio::test]
100    async fn http_request() {
101        let request = HttpRequest::get().body(b"test").header("test", "test");
102        assert_eq!(request.method.as_str(), "GET");
103        assert_eq!(request.body, b"test");
104        assert_eq!(request.headers.get("test").unwrap(), "test");
105    }
106
107    #[tokio::test]
108    async fn http_response() {
109        let response = HttpResponse::ok().body(b"test").header("test", "test");
110        assert_eq!(response.status.code(), 200);
111        assert_eq!(response.status.text(), "OK");
112        assert_eq!(response.body, b"test");
113        assert_eq!(response.headers.get("test").unwrap(), "test");
114    }
115}