i'm trying find solution write test , mock http response. in function accept interface:
type httpclient interface { do(req *http.request) (*http.response, error) }
i makes http request base auth
func getoverview(client httpclient, overview *overview) (*overview, error) { request, err := http.newrequest("get", fmt.sprintf("%s:%s/api/overview", overview.config.url, overview.config.port), nil) if (err != nil) { log.println(err) } request.setbasicauth(overview.config.user, overview.config.password) resp, err := client.do(request)
how can mock httpclient? i'm looking mock library, instance: https://github.com/h2non/gock there mock , post
maybe should in different way. i'll grateful advice
any struct method matching signature have in interface implement interface. example, create struct clientmock
type clientmock struct { }
with method
func (c *clientmock) do(req *http.request) (*http.response, error) { return &http.response{}, nil }
you inject clientmock
struct getoverview
func. here's example in go playground.
Comments
Post a Comment