Skip to content

Instantly share code, notes, and snippets.

@sonya
Created November 4, 2021 02:44
Show Gist options
  • Save sonya/788779802ae320cd31ff761345fe6774 to your computer and use it in GitHub Desktop.
Save sonya/788779802ae320cd31ff761345fe6774 to your computer and use it in GitHub Desktop.
http request mocking code samples
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
var (
Client HTTPClient
)
func init() {
Client = &http.Client{}
}
func GetFixedValue(baseURL string) (string, error) {
url := fmt.Sprintf("%s/fixedvalue", baseURL)
request, _ := http.NewRequest(http.MethodGet, url, nil)
request.Header.Add("Accept", "application/json")
// we have removed the line that says `client := &http.Client{}`
response, err := Client.Do(request)
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment