lipanski / mockito

HTTP mocking for Rust!
MIT License
695 stars 59 forks source link

feat: return raw socket address #186

Closed alexander-jackson closed 1 year ago

alexander-jackson commented 1 year ago

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(host, 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:

lipanski commented 1 year ago

:+1: