From the course: Unit Testing in Go

Unlock the full course today

Join today to access over 25,000 courses taught by industry experts.

Unit testing HTTP clients

Unit testing HTTP clients - Go Tutorial

From the course: Unit Testing in Go

Unit testing HTTP clients

- [Instructor] As a developer, you would most likely have to deal with REST APIs. You'll either be creating them or consuming them in some way. This is why this chapter will focus on unit testing REST API clients and handlers in Go. When building REST API clients in Golang, you want to ensure that your REST API client correctly retrieves data from the API and handles different scenarios, such as successful responses, expected response bodies, and error conditions. Now, how can you possibly achieve this level of granular testing without calling the actual API? Don't worry. Go has a built-in package, httptest, that allows you to mock the real API to validate the different scenarios and the sessions you want safely. The httptest package in Go provides a set of utilities for testing HTTP servers and making HTTP requests in unit tests. The package allows you to create mock HTTP servers and simulate requests and responses…

Contents