Introduction

In episode 7, Miki discussed design considerations to keep in mind while creating interfaces in Go with the first idea he proposed being that an interface should represent what we need from a type, and not what is stored on the type. To add some clarity to this thought, Miki explained how the io.Reader and io.Writer interfaces each require one method to be implemented although the underlying concrete type may store more information. The second idea he proposed was that interfaces should be small, with less than 6 methods, to ensure your code remains composable.

In this video, Miki will build an API client for a web service. The type will have a method called Health that:

  • Performs an HTTP request to a URL.
  • Returns an error depending on the response code received from the server.

Miki will then proceed by writing unit tests to verify if the client is working as intended. In lieu of a real server, Miki will create a new type that implements the http.RoundTripper interface and use it as the request’s transport during testing to manually specify the response code returned. Watch and learn how to mock server responses for unit tests.

Things you will learn in this video

  • Perform an HTTP request with the Go STD library.
  • Mock an HTTP response for unit testing.

Video

Trusted by top technology companies

We've built our reputation as educators and bring that mentality to every project. When you partner with us, your team will learn best practices and grow along the way.

30,000+

Engineers Trained

1,000+

Companies Worldwide

12+

Years in Business