1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use hitt_parser::HittRequest;

pub struct HittResponse {
    pub url: String,
    pub method: String,
    pub status_code: reqwest::StatusCode,
    pub headers: reqwest::header::HeaderMap,
    pub body: String,
    pub http_version: http::version::Version,
    pub duration: core::time::Duration,
}

#[inline]
pub async fn send_request(
    http_client: &reqwest::Client,
    input: &HittRequest,
    timeout: &Option<core::time::Duration>,
) -> Result<HittResponse, reqwest::Error> {
    let url = input.uri.to_string();

    let mut partial_req = http_client.request(input.method.clone(), &url);

    if let Some(http_version) = input.http_version {
        partial_req = partial_req.version(http_version);
    }

    if !input.headers.is_empty() {
        partial_req = partial_req.headers(input.headers.clone());
    }

    if input.body.is_some() {
        if let Some(body) = input.body.clone() {
            partial_req = partial_req.body(body);
        }
    }

    if timeout.is_some() {
        if let Some(dur) = *timeout {
            partial_req = partial_req.timeout(dur);
        }
    }

    let req = partial_req.build()?;

    // TODO: implement more precise benchmark?
    let start = std::time::Instant::now();
    let res = http_client.execute(req).await?;
    let duration = start.elapsed();

    Ok(HittResponse {
        url,
        method: input.method.to_string(),
        status_code: res.status(),
        headers: res.headers().to_owned(),
        http_version: res.version(),
        duration,
        body: res.text().await?,
    })
}

#[cfg(test)]
mod test_send_request {
    use core::{str::FromStr, time::Duration};

    use http::{HeaderMap, StatusCode};

    use crate::send_request;

    #[tokio::test]
    async fn it_should_return_a_response() {
        let http_client = reqwest::Client::new();

        let timeout = None;

        let uri = http::Uri::from_static("https://dummyjson.com/products/1");

        let method = http::Method::GET;

        let input = hitt_parser::HittRequest {
            method: method.clone(),
            uri: uri.clone(),
            headers: HeaderMap::default(),
            body: None,
            http_version: None,
        };

        let result = send_request(&http_client, &input, &timeout)
            .await
            .expect("it to be successful");

        assert_eq!(result.url, uri.to_string());

        assert_eq!(result.status_code, StatusCode::OK);

        assert_eq!(
            http::Method::from_str(&result.method).expect("it to be a valid method"),
            method
        );

        assert!(!result.body.is_empty());
    }

    #[tokio::test]
    async fn it_should_set_http_version() {
        let http_client = reqwest::Client::new();

        let timeout = None;

        let uri = http::Uri::from_static("https://dummyjson.com/products/1");

        let method = http::Method::GET;

        let input = hitt_parser::HittRequest {
            method: method.clone(),
            uri: uri.clone(),
            headers: HeaderMap::default(),
            body: Some("hello world".to_owned()),
            http_version: Some(http::Version::HTTP_11),
        };

        let result = send_request(&http_client, &input, &timeout)
            .await
            .expect("it to be successful");

        assert_eq!(result.url, uri.to_string());

        assert_eq!(result.status_code, StatusCode::OK);

        assert_eq!(
            http::Method::from_str(&result.method).expect("it to be a valid method"),
            method
        );

        assert!(!result.body.is_empty());
    }

    #[tokio::test]
    async fn it_should_set_headers() {
        let http_client = reqwest::Client::new();

        let timeout = None;

        let uri = http::Uri::from_static("https://dummyjson.com/products/1");

        let mut headers = HeaderMap::new();

        let header_key = http::HeaderName::from_static("mads");

        let header_value = http::HeaderValue::from_static("hougesen");

        headers.insert(header_key, header_value);

        let method = http::Method::DELETE;

        let input = hitt_parser::HittRequest {
            method: method.clone(),
            uri: uri.clone(),
            headers,
            body: Some("hello world".to_owned()),
            http_version: None,
        };

        let result = send_request(&http_client, &input, &timeout)
            .await
            .expect("it to be successful");

        assert_eq!(result.url, uri.to_string());

        assert_eq!(result.status_code, StatusCode::OK);

        assert_eq!(
            http::Method::from_str(&result.method).expect("it to be a valid method"),
            method
        );

        assert!(!result.body.is_empty());
    }

    #[tokio::test]
    async fn it_should_set_body() {
        let http_client = reqwest::Client::new();

        let timeout = None;

        let uri = http::Uri::from_static("https://dummyjson.com/products/1");

        let input = hitt_parser::HittRequest {
            method: http::Method::GET,
            uri: uri.clone(),
            headers: HeaderMap::default(),
            body: Some("hello world".to_owned()),
            http_version: None,
        };

        let result = send_request(&http_client, &input, &timeout)
            .await
            .expect("it to be successful");

        assert_eq!(result.url, uri.to_string());

        assert_eq!(result.status_code, StatusCode::OK);

        assert!(!result.body.is_empty());
    }

    #[tokio::test]
    async fn timeout_should_work() {
        let http_client = reqwest::Client::new();

        let timeout = Some(Duration::from_millis(5));

        let uri = http::Uri::from_static("https://dummyjson.com/products/1");

        let input = hitt_parser::HittRequest {
            method: http::Method::GET,
            uri: uri.clone(),
            headers: HeaderMap::default(),
            body: None,
            http_version: None,
        };

        let response = send_request(&http_client, &input, &timeout)
            .await
            .err()
            .expect("it to throw an error");

        assert!(response.is_timeout());
    }
}