building http based clients in Go

Hypothetical Client: Runtime

Let’s imagine we have a hypothetical Runtime client with just one public method:

Subtle design decision: Always specify your http client

By starting with a specific HTTP client even with our simplistic implementation, we can potentially provide a custom HTTP client with instrumentation, or possibly one which implements some retry semantics.

Testing this implementation

So now we reach that part: do we do mocking? Which line of abstraction do we strike it against?

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

Conclusion

As we can see, testing http based clients need not resort to any sort of complicated mocking: instead we can just use http.Client as the core interaction point.

Footnotes

[1]: We use ioutil.ReadAll heavily in these examples. Depending on the actual usecase, we should likely use a stream based interface (e.g. json.NewDecoder as an example).

--

--

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

Cyril David

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