building http based clients in Go

When I came from the very dynamic world of ruby, my first approach to designing HTTP clients in go was to think about mocking similar to what I was used to in Ruby. I quickly learned that it’s not the ideal approach in Go, and over time have learned a few tips and tricks for dealing with HTTP clients.

Hypothetical Client: Runtime

If the implementation is mostly executing an HTTP request against a URL, one can imagine the following simple implementation:

This all makes sense so far, and is straightforward.

Subtle design decision: Always specify your http client

But most importantly, when going to production, tuning timeout knobs would be the most important thing to not forget.

Testing this implementation

Because of two important design decisions, we’re able to test this easily without any heavy dependencies, namely:

  1. We specify the URL for the client explicitly.
  2. We allow the caller to specify an httpClient.

The following is an example test implementation, which simply utilizes the wonderful httptest package:

Conclusion

Footnotes

[2]: httptest also has an equivalent NewTLSServer helper function — which creates a listener with TLS. The Client() method then would return an HTTP client which would have the self signed certificate information.

--

--

software engineer at auth0, writing code for humans, with humans.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cyril David

software engineer at auth0, writing code for humans, with humans.