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
use anyhow::Result;
use std::net::IpAddr;

use crate::transport_layer::IntoTransportLayer;
use crate::TestServer;
use crate::TestServerConfig;
use crate::Transport;

/// This is for easing the building of [`TestServerConfig`](crate::TestServerConfig).
///
/// For full documentation see there.
///
/// ```rust
/// use axum_test::TestServerConfig;
///
/// let config = TestServerConfig::builder()
///     .save_cookies()
///     .default_content_type(&"application/json")
///     .build();
/// ```
///
/// These can be passed to `TestServer::new_with_config`:
///
/// ```rust
/// # async fn test() -> Result<(), Box<dyn ::std::error::Error>> {
/// #
/// use axum::Router;
/// use axum_test::TestServer;
/// use axum_test::TestServerConfig;
///
/// let my_app = Router::new();
/// let config = TestServerConfig::builder()
///     .save_cookies()
///     .default_content_type(&"application/json")
///     .build();
///
/// // Build the Test Server
/// let server = TestServer::new_with_config(my_app, config)?;
/// #
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct TestServerConfigBuilder {
    config: TestServerConfig,
}

impl TestServerConfigBuilder {
    pub fn http_transport(self) -> Self {
        self.transport(Transport::HttpRandomPort)
    }

    pub fn http_transport_with_ip_port(self, ip: Option<IpAddr>, port: Option<u16>) -> Self {
        self.transport(Transport::HttpIpPort { ip, port })
    }

    pub fn mock_transport(self) -> Self {
        self.transport(Transport::MockHttp)
    }

    pub fn transport(mut self, transport: Transport) -> Self {
        self.config.transport = Some(transport);
        self
    }

    pub fn save_cookies(mut self) -> Self {
        self.config.save_cookies = true;
        self
    }

    pub fn do_not_save_cookies(mut self) -> Self {
        self.config.save_cookies = false;
        self
    }

    pub fn default_content_type(mut self, content_type: &str) -> Self {
        self.config.default_content_type = Some(content_type.to_string());
        self
    }

    pub fn default_scheme(mut self, scheme: &str) -> Self {
        self.config.default_scheme = Some(scheme.to_string());
        self
    }

    pub fn expect_success_by_default(mut self) -> Self {
        self.config.expect_success_by_default = true;
        self
    }

    pub fn restrict_requests_with_http_schema(mut self) -> Self {
        self.config.restrict_requests_with_http_schema = true;
        self
    }

    pub fn build(self) -> TestServerConfig {
        self.config
    }

    /// This is shorthand for calling [`crate::TestServer::new_with_config`].
    ///
    /// ```rust
    /// use axum::Router;
    /// use axum_test::TestServerConfig;
    ///
    /// let app = Router::new();
    /// let server = TestServerConfig::builder()
    ///     .save_cookies()
    ///     .default_content_type(&"application/json")
    ///     .build_server(app);
    /// ```
    pub fn build_server<A>(self, app: A) -> Result<TestServer>
    where
        A: IntoTransportLayer,
    {
        self.build().build_server(app)
    }
}

impl Default for TestServerConfigBuilder {
    fn default() -> Self {
        Self {
            config: TestServerConfig::default(),
        }
    }
}

#[cfg(test)]
mod test_build {
    use super::*;
    use std::net::Ipv4Addr;

    #[test]
    fn it_should_build_default_config_by_default() {
        let config = TestServerConfig::builder().build();
        let expected = TestServerConfig::default();

        assert_eq!(config, expected);
    }

    #[test]
    fn it_should_save_cookies_when_set() {
        let config = TestServerConfig::builder().save_cookies().build();

        assert_eq!(config.save_cookies, true);
    }

    #[test]
    fn it_should_not_save_cookies_when_set() {
        let config = TestServerConfig::builder().do_not_save_cookies().build();

        assert_eq!(config.save_cookies, false);
    }

    #[test]
    fn it_should_mock_transport_when_set() {
        let config = TestServerConfig::builder().mock_transport().build();

        assert_eq!(config.transport, Some(Transport::MockHttp));
    }

    #[test]
    fn it_should_use_random_http_transport_when_set() {
        let config = TestServerConfig::builder().http_transport().build();

        assert_eq!(config.transport, Some(Transport::HttpRandomPort));
    }

    #[test]
    fn it_should_use_http_transport_with_ip_port_when_set() {
        let config = TestServerConfig::builder()
            .http_transport_with_ip_port(Some(IpAddr::V4(Ipv4Addr::new(123, 4, 5, 6))), Some(987))
            .build();

        assert_eq!(
            config.transport,
            Some(Transport::HttpIpPort {
                ip: Some(IpAddr::V4(Ipv4Addr::new(123, 4, 5, 6))),
                port: Some(987),
            })
        );
    }

    #[test]
    fn it_should_set_default_content_type_when_set() {
        let config = TestServerConfig::builder()
            .default_content_type("text/csv")
            .build();

        assert_eq!(config.default_content_type, Some("text/csv".to_string()));
    }

    #[test]
    fn it_should_set_default_scheme_when_set() {
        let config = TestServerConfig::builder().default_scheme("ftps").build();

        assert_eq!(config.default_scheme, Some("ftps".to_string()));
    }

    #[test]
    fn it_should_set_expect_success_by_default_when_set() {
        let config = TestServerConfig::builder()
            .expect_success_by_default()
            .build();

        assert_eq!(config.expect_success_by_default, true);
    }

    #[test]
    fn it_should_set_restrict_requests_with_http_schema_when_set() {
        let config = TestServerConfig::builder()
            .restrict_requests_with_http_schema()
            .build();

        assert_eq!(config.restrict_requests_with_http_schema, true);
    }
}