Last active
August 14, 2019 16:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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