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
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::Method;
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::future::IntoFuture;

use crate::TestResponse;

/// This contains the response from the server.
///
/// Inside are the contents of the response, the status code, and some
/// debugging information.
///
/// You can get the contents out as it's raw string, or deserialise it.
/// One can also also use the `assert_*` functions to test against the
/// response.
#[derive(Debug)]
#[must_use = "futures do nothing unless polled"]
pub struct TestRequest {
    method: Method,
    path: String,
    body: Option<Body>,

    /// This is what we use for logging for when we display the path to the user.
    debug_path: String,

    is_expecting_failure: bool,
}

impl TestRequest {
    pub(crate) fn new(method: Method, path: String, debug_path: String) -> Self {
        Self {
            method,
            path,
            body: None,
            debug_path,
            is_expecting_failure: false,
        }
    }

    /// 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_fail(mut self) -> Self {
        self.is_expecting_failure = true;
        self
    }

    /// Set the body of the request to send up as Json.
    pub fn json<J>(mut self, body: &J) -> Self
    where
        J: 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);
        self
    }

    /// Set the body of the request to send up as raw test.
    pub fn text<S>(self, raw_body: S) -> Self
    where
        S: AsRef<str>,
    {
        let body_bytes = Bytes::copy_from_slice(raw_body.as_ref().as_bytes());
        self.bytes(body_bytes)
    }

    /// Set the body of the request to send up as raw bytes.
    pub fn bytes(mut self, body_bytes: Bytes) -> Self {
        let body: Body = body_bytes.into();

        self.body = Some(body);
        self
    }

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

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

        let request = Request::builder()
            .uri(&self.path)
            .header(header::CONTENT_TYPE, "application/json")
            .method(self.method)
            .body(body)
            .with_context(|| {
                format!(
                    "Expect valid hyper Request to be built on request to {}",
                    self.debug_path
                )
            })?;

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

        let (parts, response_body) = hyper_response.into_parts();
        let response_bytes = to_bytes(response_body).await?;
        let mut response = TestResponse::new(self.debug_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)
    }
}