axum_test/util/
new_random_socket_addr.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use anyhow::Result;
use std::net::IpAddr;
use std::net::Ipv4Addr;
use std::net::SocketAddr;

use crate::util::new_random_port;

pub(crate) const DEFAULT_IP_ADDRESS: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);

/// Generates a `SocketAddr` on the IP 127.0.0.1, using a random port.
pub fn new_random_socket_addr() -> Result<SocketAddr> {
    let ip_address = DEFAULT_IP_ADDRESS;
    let port = new_random_port()?;
    let addr = SocketAddr::new(ip_address, port);

    Ok(addr)
}