Leave a Comment
A way to test http client in go
A couple of ways are described here — http://hassansin.github.io/Unit-Testing-http-client-in-Go
The way I liked is the following one:
func Test_Mine(t *testing.T) {
...
client := httpClientWithRoundTripper(http.StatusOK, "OK")
...
}
type roundTripFunc func(req *http.Request) *http.Response
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}
func httpClientWithRoundTripper(statusCode int, response string) *http.Client {
return &http.Client{
Transport: roundTripFunc(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: statusCode,
Body: ioutil.NopCloser(bytes.NewBufferString(response)),
}
}),
}
}
LEAVE A COMMENT
Для отправки комментария вам необходимо авторизоваться.