The httpmock
is a simple mocking library to mock the golang http client response.
Its intent is to reduce the boiler plate code required to write the unit test where a unit of code to test is wrapping the http.Client or passing http.Client as a dependency.
-
Execute the
go get github.com/hrishin/httpmock
-
Let's mock the response for
GET /foo
request which return theHTTP 200 OK
response with some response body contentimport ( "testing" "github.com/hrishin/httpmock" ) func Test_some_test(t *testing.T) { // I prefer using _ as convention(readability) mockResponse := httpmock.Response{ URI: "/foo", StatusCode: 200, Body: "bar response", } api := &someAPI{ client: httpmock.Client(&mockResponse), API: "https://some.com/api/", } got := api.do() // assert got response from API }
In this example, someAPI
is using http.Client as a dependency in order to execute the HTTP request for a given API URL.