axum_test/transport.rs
1use educe::Educe;
2use std::net::IpAddr;
3use std::net::TcpListener;
4
5/// Transport is for setting which transport mode for the `TestServer`
6/// to use when making requests.
7#[derive(Educe, Default, Debug)]
8#[educe(Clone, PartialEq)]
9pub enum Transport {
10 /// With this transport mode, `TestRequest` will use a mock HTTP
11 /// transport.
12 ///
13 /// This is the Default Transport type.
14 #[default]
15 MockHttp,
16
17 /// With this transport mode, a real web server will be spun up
18 /// running on a random port. Requests made using the `TestRequest`
19 /// will be made over the network stack.
20 HttpRandomPort,
21
22 /// With this transport mode, a real web server will be spun up.
23 /// Where you can pick which IP and Port to use for this to bind to.
24 ///
25 /// Setting both `ip` and `port` to `None`, is the equivalent of
26 /// using `Transport::HttpRandomPort`.
27 HttpIpPort {
28 /// Set the IP to use for the server.
29 ///
30 /// **Defaults** to `127.0.0.1`.
31 ip: Option<IpAddr>,
32
33 /// Set the port number to use for the server.
34 ///
35 /// **Defaults** to a _random_ port.
36 port: Option<u16>,
37 },
38
39 HttpTcpListner {
40 #[educe(
41 Clone(method(educe_clone_error)),
42 PartialEq(method(educe_return_false))
43 )]
44 tcp_listener: TcpListener,
45 },
46}
47
48const fn educe_clone_error(_: &TcpListener) -> TcpListener {
49 panic!("Transport::clone is not supported when holding a TcpListener")
50}
51
52const fn educe_return_false(_: &TcpListener, _: &TcpListener) -> bool {
53 false
54}