Skip to content

Instantly share code, notes, and snippets.

@glenjamin
Created January 21, 2022 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glenjamin/66d75ab0188ddb60f9d0d8cbbdd2421c to your computer and use it in GitHub Desktop.
Save glenjamin/66d75ab0188ddb60f9d0d8cbbdd2421c to your computer and use it in GitHub Desktop.
Testing an HTTP client that makes encrypted requests to a server you don't control

Here's neat testing trick I've just come across.

I've got an HTTP client that makes requests to https://accounts.google.com.

So I create a fake HTTP server:

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
/// ...
})

And then I can force my HTTP client to make non-HTTPS connections to my local server

&http.Client{Transport: &http.Transport{
  DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
    address := srv.Listener.Addr()
    return net.Dial(address.Network(), address.String())
  },
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment