Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return raw socket address #186

Merged

Conversation

alexander-jackson
Copy link
Contributor

@alexander-jackson alexander-jackson commented Nov 16, 2023

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:

  • Updates the Server type to store a SocketAddr instead of a String
  • Adds a function to get the underlying address

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
@lipanski lipanski merged commit 3cce903 into lipanski:master Nov 20, 2023
6 checks passed
@lipanski
Copy link
Owner

👍

@alexander-jackson alexander-jackson deleted the feat/return-raw-socket-address branch November 21, 2023 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants