rust_integration_services/http/
mod.rs

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