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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use ::anyhow::anyhow;
use ::anyhow::Context;
use ::anyhow::Result;
use ::auto_future::AutoFuture;
use ::hyper::body::to_bytes;
use ::hyper::body::Body;
use ::hyper::body::Bytes;
use ::hyper::header;
use ::hyper::http::header::SET_COOKIE;
use ::hyper::http::Request;
use ::hyper::Client;
use ::serde::Serialize;
use ::serde_json::to_vec as json_to_vec;
use ::std::convert::AsRef;
use ::std::fmt::Debug;
use ::std::fmt::Display;
use ::std::fmt::Write;
use ::std::future::IntoFuture;
use ::std::sync::Arc;
use ::std::sync::Mutex;
use axum::http::HeaderValue;
use hyper::header::HeaderName;

use crate::InnerTestServer;
use crate::TestResponse;

mod test_request_config;
pub(crate) use self::test_request_config::*;

mod test_request_details;
pub(crate) use self::test_request_details::*;

const JSON_CONTENT_TYPE: &'static str = &"application/json";
const TEXT_CONTENT_TYPE: &'static str = &"text/plain";

///
/// A `TestRequest` represents a HTTP request to the test server.
///
/// ## Creating
///
/// Requests are created by the `TestServer`. You do not create them yourself.
///
/// The `TestServer` has functions corresponding to specific requests.
/// For example calling `TestServer::get` to create a new HTTP GET request,
/// or `TestServer::post to create a HTTP POST request.
///
/// ## Customising
///
/// The `TestRequest` allows the caller to fill in the rest of the request
/// to be sent to the server. Including the headers, the body, cookies, the content type,
/// and other relevant details.
///
/// The TestRequest struct provides a number of methods to set up the request,
/// such as json, text, bytes, expect_failure, content_type, etc.
/// The do_save_cookies and do_not_save_cookies methods are used to control cookie handling.
///
/// ## Sending
///
/// Once fully configured you send the rquest by awaiting the request object.
///
/// ```rust,ignore
/// let request = server.get(&"/user");
/// let response = request.await;
/// ```
///
/// You will receive back a `TestResponse`.
///
#[derive(Debug)]
#[must_use = "futures do nothing unless polled"]
pub struct TestRequest {
    details: TestRequestDetails,

    inner_test_server: Arc<Mutex<InnerTestServer>>,

    full_request_path: String,
    body: Option<Body>,
    headers: Vec<(HeaderName, HeaderValue)>,
    content_type: Option<String>,

    is_expecting_failure: bool,
    is_saving_cookies: bool,
}

impl TestRequest {
    pub(crate) fn new(
        inner_test_server: Arc<Mutex<InnerTestServer>>,
        config: TestRequestConfig,
        details: TestRequestDetails,
    ) -> Result<Self> {
        let server_locked = inner_test_server.as_ref().lock().map_err(|err| {
            anyhow!(
                "Failed to lock InternalTestServer for {} {}, received {:?}",
                details.method,
                details.path,
                err
            )
        })?;
        let full_request_path = build_request_path(server_locked.server_address(), &details.path);

        let cookie_header_raw =
            server_locked
                .cookies()
                .iter()
                .fold(String::new(), |mut buffer, cookie| {
                    if buffer.len() > 0 {
                        write!(buffer, "; ").expect(
                            "Writing to internal string for cookie header should always work",
                        );
                    }

                    write!(buffer, "{}", cookie)
                        .expect("Writing to internal string for cookie header should always work");

                    buffer
                });

        ::std::mem::drop(server_locked);

        let mut initial_headers: Vec<(HeaderName, HeaderValue)> = vec![];
        if cookie_header_raw.len() > 0 {
            let header_value = HeaderValue::from_str(&cookie_header_raw)?;
            initial_headers.push((header::COOKIE, header_value));
        }

        Ok(Self {
            details,
            inner_test_server,
            full_request_path,
            body: None,
            headers: initial_headers,
            content_type: config.content_type,
            is_expecting_failure: false,
            is_saving_cookies: config.save_cookies,
        })
    }

    /// Any cookies returned will be saved to the `TestServer` that created this,
    /// which will continue to use those cookies on future requests.
    pub fn do_save_cookies(mut self) -> Self {
        self.is_saving_cookies = true;
        self
    }

    /// Cookies returned by this will _not_ be saved to the `TestServer`.
    /// For use by future requests.
    ///
    /// This is the default behaviour.
    /// You can change that default in `TestServerConfig`.
    pub fn do_not_save_cookies(mut self) -> Self {
        self.is_saving_cookies = false;
        self
    }

    /// Marks that this request should expect to fail.
    /// Failiure is deemend as any response that isn't a 200.
    ///
    /// By default, requests are expct to always succeed.
    pub fn expect_failure(mut self) -> Self {
        self.is_expecting_failure = true;
        self
    }

    /// Marks that this request should expect to succeed.
    /// Success is deemend as returning a 200.
    ///
    /// Note this is the default behaviour when creating a new `TestRequest`.
    pub fn expect_success(mut self) -> Self {
        self.is_expecting_failure = false;
        self
    }

    /// Set the body of the request to send up as Json.
    pub fn json<J>(mut self, body: &J) -> Self
    where
        J: ?Sized + Serialize,
    {
        let body_bytes = json_to_vec(body).expect("It should serialize the content into JSON");
        let body: Body = body_bytes.into();
        self.body = Some(body);

        if self.content_type == None {
            self.content_type = Some(JSON_CONTENT_TYPE.to_string());
        }

        self
    }

    /// Set raw text as the body of the request.
    ///
    /// If there isn't a content type set, this will default to `text/plain`.
    pub fn text<T>(mut self, raw_text: T) -> Self
    where
        T: Display,
    {
        let body_text = format!("{}", raw_text);
        let body_bytes = Bytes::from(body_text.into_bytes());

        if self.content_type == None {
            self.content_type = Some(TEXT_CONTENT_TYPE.to_string());
        }

        self.bytes(body_bytes)
    }

    /// Set raw bytes as the body of the request.
    ///
    /// The content type is left unchanged.
    pub fn bytes(mut self, body_bytes: Bytes) -> Self {
        let body: Body = body_bytes.into();

        self.body = Some(body);
        self
    }

    /// Set the content type to use for this request in the header.
    pub fn content_type(mut self, content_type: &str) -> Self {
        self.content_type = Some(content_type.to_string());
        self
    }

    async fn send_or_panic(self) -> TestResponse {
        self.send().await.expect("Sending request failed")
    }

    async fn send(mut self) -> Result<TestResponse> {
        let path = self.details.path;
        let save_cookies = self.is_saving_cookies;
        let body = self.body.unwrap_or(Body::empty());

        let mut request_builder = Request::builder()
            .uri(&self.full_request_path)
            .method(self.details.method);

        // Add all the headers we have.
        let mut headers = self.headers;
        if let Some(content_type) = self.content_type {
            let header = build_content_type_header(content_type)?;
            headers.push(header);
        }

        for (header_name, header_value) in headers {
            request_builder = request_builder.header(header_name, header_value);
        }

        let request = request_builder.body(body).with_context(|| {
            format!(
                "Expect valid hyper Request to be built on request to {}",
                path
            )
        })?;

        let hyper_response = Client::new()
            .request(request)
            .await
            .with_context(|| format!("Expect Hyper Response to succeed on request to {}", path))?;

        let (parts, response_body) = hyper_response.into_parts();
        let response_bytes = to_bytes(response_body).await?;

        if save_cookies {
            let cookie_headers = parts.headers.get_all(SET_COOKIE).into_iter();
            InnerTestServer::add_cookies_by_header(&mut self.inner_test_server, cookie_headers)?;
        }

        let mut response = TestResponse::new(path, parts, response_bytes);

        // Assert if ok or not.
        if self.is_expecting_failure {
            response = response.assert_status_not_ok();
        } else {
            response = response.assert_status_ok();
        }

        Ok(response)
    }
}

impl IntoFuture for TestRequest {
    type Output = TestResponse;
    type IntoFuture = AutoFuture<TestResponse>;

    fn into_future(self) -> Self::IntoFuture {
        let raw_future = self.send_or_panic();
        AutoFuture::new(raw_future)
    }
}

fn build_request_path(root_path: &str, sub_path: &str) -> String {
    if sub_path == "" {
        return format!("http://{}", root_path.to_string());
    }

    if sub_path.starts_with("/") {
        return format!("http://{}{}", root_path, sub_path);
    }

    format!("http://{}/{}", root_path, sub_path)
}

fn build_content_type_header(content_type: String) -> Result<(HeaderName, HeaderValue)> {
    let header_value = HeaderValue::from_str(&content_type)
        .with_context(|| format!("Failed to store header content type '{}'", content_type))?;

    Ok((header::CONTENT_TYPE, header_value))
}