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