Skip to content

Commit

Permalink
feat: return raw socket address
Browse files Browse the repository at this point in the history
I have a lambda function that would be nice to end-to-end test. It
functions as a proxy and receives a URL, resolves it to an IP address
and then returns the response to the client. The existing tests use
`mockito` for the HTTP responses and point directly to the server, but
it would be good to test the DNS resolution in the mix as well.

In essence, it would be nice to do something like:

```
let host = "example.com";

let mut http_server = mockito::Server::new();
let mut dns_server = dns_mock_server::Server::new();

let socket_addr = http_server.socket_address();
dns_server.add_records("example.com", vec![socket_addr.ip()]);

let port = socket_addr.port();
let uri = format!("http://{host}:{port}");

handle_request(uri);
```

This will then resolve `example.com` into the address of the `mockito`
server before making the request to the mock and handling the response.

However, `mockito` doesn't currently expose the address of the server in
a nice format. While the socket address likely could just be parsed with
`SocketAddr::from_str` on the result of `host_with_port`, it makes more
sense to expose the `SocketAddr` directly (and begin passing it around
instead of `String`).

This change:
* Updates the `Server` type to store a `SocketAddr` instead of a `String`
* Adds a function to get the underlying address
  • Loading branch information
alexander-jackson committed Nov 16, 2023
1 parent 496f26d commit 2ed230b
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl State {
///
#[derive(Debug)]
pub struct Server {
address: String,
address: SocketAddr,
state: Arc<RwLock<State>>,
}

Expand Down Expand Up @@ -199,7 +199,7 @@ impl Server {
pub(crate) fn try_new_with_port(port: u16) -> Result<Server, Error> {
let state = Arc::new(RwLock::new(State::new()));
let address = SocketAddr::from(([127, 0, 0, 1], port));
let (address_sender, address_receiver) = mpsc::channel::<String>();
let (address_sender, address_receiver) = mpsc::channel::<SocketAddr>();
let runtime = runtime::Builder::new_current_thread()
.enable_all()
.build()
Expand All @@ -226,7 +226,7 @@ impl Server {
pub(crate) async fn try_new_with_port_async(port: u16) -> Result<Server, Error> {
let state = Arc::new(RwLock::new(State::new()));
let address = SocketAddr::from(([127, 0, 0, 1], port));
let (address_sender, address_receiver) = mpsc::channel::<String>();
let (address_sender, address_receiver) = mpsc::channel::<SocketAddr>();
let runtime = runtime::Builder::new_current_thread()
.enable_all()
.build()
Expand All @@ -249,7 +249,7 @@ impl Server {

async fn bind_server(
address: SocketAddr,
address_sender: mpsc::Sender<String>,
address_sender: mpsc::Sender<SocketAddr>,
state: Arc<RwLock<State>>,
) -> Result<(), Error> {
let listener = TcpListener::bind(address)
Expand All @@ -260,7 +260,7 @@ impl Server {
.local_addr()
.map_err(|err| Error::new_with_context(ErrorKind::ServerFailure, err))?;

address_sender.send(address.to_string()).unwrap();
address_sender.send(address).unwrap();

while let Ok((stream, _)) = listener.accept().await {
let mutex = state.clone();
Expand Down Expand Up @@ -311,7 +311,14 @@ impl Server {
/// Can be used with `std::net::TcpStream`.
///
pub fn host_with_port(&self) -> String {
self.address.clone()
self.address.to_string()
}

///
/// The raw address of the mock server.
///
pub fn socket_address(&self) -> SocketAddr {
self.address
}

///
Expand Down

0 comments on commit 2ed230b

Please sign in to comment.