I've googled all over for this but can't find anything.
I have a struct that takes in a http.Client
and it sends several GET requests. In my tests I want to mock the responses so it's not sending real requests.
Currently I've figured out how to only serve 1 request, like this:
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
file, err := os.Open("./testdata/1.html")
if err != nil {
t.Error(err)
}
bytes, err := ioutil.ReadAll(file)
if err != nil {
t.Error(err)
}
w.Write(bytes)
}))
ts.Client() // Now I can inject this client into my struct.
So once that response is mocked out and the http client is performs a new request, my tests are sending out real requests after that.
How do I allow for several handlers so I can mock several responses upon calling http.Client.Get(...)
?