Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Last active August 14, 2019 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeoncross/a3005073eeb2809cc357378ef24ad73e to your computer and use it in GitHub Desktop.
Save xeoncross/a3005073eeb2809cc357378ef24ad73e to your computer and use it in GitHub Desktop.
Simple wrapper to use for testing when you need a custom http.Client for faking network requests
// https://golang.org/pkg/net/http/#Client
type MockClient struct {
Body interface{}
DoFunc func(req *http.Request) (*http.Response, error)
}
func (m *MockClient) Do(req *http.Request) (*http.Response, error) {
if m.DoFunc != nil {
return m.DoFunc(req)
}
if m.Body != nil {
body, err := json.Marshal(m.Body)
if err != nil {
return nil, err
}
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBuffer(body)),
StatusCode: http.StatusOK,
}, nil
}
// Default response
return &http.Response{}, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment