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
use crate::TestServerConfigBuilder;
use crate::Transport;

/// This is for customising the [`TestServer`](crate::TestServer) on construction.
///
/// It implements [`Default`] to ease building configurations:
///
/// ```rust
/// use ::axum_test::TestServerConfig;
///
/// let config = TestServerConfig {
///     save_cookies: true,
///     ..TestServerConfig::default()
/// };
/// ```
///
/// 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 {
///     save_cookies: true,
///     ..TestServerConfig::default()
/// };
///
/// // Build the Test Server
/// let server = TestServer::new_with_config(my_app, config)?;
/// #
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct TestServerConfig {
    /// Which transport mode to use to process requests.
    /// For setting if the server should use mocked http (which uses [`tower::util::Oneshot`](tower::util::Oneshot)),
    /// or if it should run on a named or random IP address.
    ///
    /// The default is to use mocking, apart from services built using [`axum::extract::connect_info::IntoMakeServiceWithConnectInfo`](axum::extract::connect_info::IntoMakeServiceWithConnectInfo)
    /// (this is because it needs a real TCP stream).
    pub transport: Option<Transport>,

    /// Set for the server to save cookies that are returned,
    /// for use in future requests.
    ///
    /// This is useful for automatically saving session cookies (and similar)
    /// like a browser would do.
    ///
    /// **Defaults** to false (being turned off).
    pub save_cookies: bool,

    /// Sets requests made by the server to always expect a status code returned in the 2xx range,
    /// and to panic if that is missing.
    ///
    /// This is useful when making multiple requests at a start of test
    /// which you presume should always work. It also helps to make tests more explicit.
    ///
    /// **Defaults** to false (being turned off).
    pub expect_success_by_default: bool,

    /// If you make a request with a 'http://' schema,
    /// then it will ignore the Test Server's address.
    ///
    /// For example if the test server is running at `http://localhost:1234`,
    /// and you make a request to `http://google.com`.
    /// Then the request will go to `http://google.com`.
    /// Ignoring the `localhost:1234` part.
    ///
    /// Turning this setting on will change this behaviour.
    ///
    /// After turning this on, the same request will go to
    /// `http://localhost:1234/http://google.com`.
    ///
    /// **Defaults** to false (being turned off).
    pub restrict_requests_with_http_schema: bool,

    /// Set the default content type for all requests created by the `TestServer`.
    ///
    /// This overrides the default 'best efforts' approach of requests.
    pub default_content_type: Option<String>,
}

impl TestServerConfig {
    /// Creates a builder for making it simpler to creating configs.
    ///
    /// ```rust
    /// use ::axum_test::TestServerConfig;
    ///
    /// let config = TestServerConfig::builder()
    ///     .save_cookies()
    ///     .default_content_type(&"application/json")
    ///     .build();
    /// ```
    ///
    pub fn builder() -> TestServerConfigBuilder {
        TestServerConfigBuilder::default()
    }
}

impl Default for TestServerConfig {
    fn default() -> Self {
        Self {
            transport: None,
            save_cookies: false,
            expect_success_by_default: false,
            restrict_requests_with_http_schema: false,
            default_content_type: None,
        }
    }
}