Created
November 4, 2021 02:44
-
-
Save sonya/788779802ae320cd31ff761345fe6774 to your computer and use it in GitHub Desktop.
http request mocking code samples
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
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