axum_test/
test_server_builder.rs1use crate::TestServer;
2use crate::TestServerConfig;
3use crate::Transport;
4use crate::internals::ErrorMessage;
5use crate::transport_layer::IntoTransportLayer;
6use anyhow::Result;
7use std::net::IpAddr;
8
9#[derive(Default, Debug, Clone)]
33pub struct TestServerBuilder {
34 config: TestServerConfig,
35}
36
37impl TestServerBuilder {
38 pub fn new() -> Self {
40 Default::default()
41 }
42
43 pub fn from_config(config: TestServerConfig) -> Self {
44 Self { config }
45 }
46
47 pub fn http_transport(self) -> Self {
48 self.transport(Transport::HttpRandomPort)
49 }
50
51 pub fn http_transport_with_ip_port(self, ip: Option<IpAddr>, port: Option<u16>) -> Self {
52 self.transport(Transport::HttpIpPort { ip, port })
53 }
54
55 pub fn mock_transport(self) -> Self {
56 self.transport(Transport::MockHttp)
57 }
58
59 pub fn transport(mut self, transport: Transport) -> Self {
60 self.config.transport = Some(transport);
61 self
62 }
63
64 pub fn save_cookies(mut self) -> Self {
65 self.config.save_cookies = true;
66 self
67 }
68
69 pub fn do_not_save_cookies(mut self) -> Self {
70 self.config.save_cookies = false;
71 self
72 }
73
74 pub fn default_content_type(mut self, content_type: &str) -> Self {
75 self.config.default_content_type = Some(content_type.to_string());
76 self
77 }
78
79 pub fn expect_success_by_default(mut self) -> Self {
80 self.config.expect_success_by_default = true;
81 self
82 }
83
84 pub fn restrict_requests_with_http_scheme(mut self) -> Self {
85 self.config.restrict_requests_with_http_scheme = true;
86 self
87 }
88
89 pub fn build<A>(self, app: A) -> TestServer
109 where
110 A: IntoTransportLayer,
111 {
112 self.try_build(app)
113 .error_message("Failed to build TestServer")
114 }
115
116 pub fn try_build<A>(self, app: A) -> Result<TestServer>
119 where
120 A: IntoTransportLayer,
121 {
122 self.into_config().try_build(app)
123 }
124
125 pub fn into_config(self) -> TestServerConfig {
147 self.config
148 }
149}
150
151impl From<TestServerConfig> for TestServerBuilder {
152 fn from(config: TestServerConfig) -> Self {
153 TestServerBuilder::from_config(config)
154 }
155}
156
157#[cfg(test)]
158mod test_build {
159 use super::*;
160 use std::net::Ipv4Addr;
161
162 #[test]
163 fn it_should_build_default_config_by_default() {
164 let config = TestServer::builder().into_config();
165 let expected = TestServerConfig::default();
166
167 assert_eq!(config, expected);
168 }
169
170 #[test]
171 fn it_should_save_cookies_when_set() {
172 let config = TestServer::builder().save_cookies().into_config();
173
174 assert_eq!(config.save_cookies, true);
175 }
176
177 #[test]
178 fn it_should_not_save_cookies_when_set() {
179 let config = TestServer::builder().do_not_save_cookies().into_config();
180
181 assert_eq!(config.save_cookies, false);
182 }
183
184 #[test]
185 fn it_should_mock_transport_when_set() {
186 let config = TestServer::builder().mock_transport().into_config();
187
188 assert_eq!(config.transport, Some(Transport::MockHttp));
189 }
190
191 #[test]
192 fn it_should_use_random_http_transport_when_set() {
193 let config = TestServer::builder().http_transport().into_config();
194
195 assert_eq!(config.transport, Some(Transport::HttpRandomPort));
196 }
197
198 #[test]
199 fn it_should_use_http_transport_with_ip_port_when_set() {
200 let config = TestServer::builder()
201 .http_transport_with_ip_port(Some(IpAddr::V4(Ipv4Addr::new(123, 4, 5, 6))), Some(987))
202 .into_config();
203
204 assert_eq!(
205 config.transport,
206 Some(Transport::HttpIpPort {
207 ip: Some(IpAddr::V4(Ipv4Addr::new(123, 4, 5, 6))),
208 port: Some(987),
209 })
210 );
211 }
212
213 #[test]
214 fn it_should_set_default_content_type_when_set() {
215 let config = TestServer::builder()
216 .default_content_type("text/csv")
217 .into_config();
218
219 assert_eq!(config.default_content_type, Some("text/csv".to_string()));
220 }
221
222 #[test]
223 fn it_should_set_expect_success_by_default_when_set() {
224 let config = TestServer::builder()
225 .expect_success_by_default()
226 .into_config();
227
228 assert_eq!(config.expect_success_by_default, true);
229 }
230
231 #[test]
232 fn it_should_set_restrict_requests_with_http_scheme_when_set() {
233 let config = TestServer::builder()
234 .restrict_requests_with_http_scheme()
235 .into_config();
236
237 assert_eq!(config.restrict_requests_with_http_scheme, true);
238 }
239}