Skip to content

Instantly share code, notes, and snippets.

@sonya
Created November 4, 2021 02:45
Show Gist options
  • Save sonya/adbe341c3df10370888e4115a8a6f3c7 to your computer and use it in GitHub Desktop.
Save sonya/adbe341c3df10370888e4115a8a6f3c7 to your computer and use it in GitHub Desktop.
http request mocking code samples
func TestGetFixedValue(t *testing.T) {
Client = &MockClient{
DoFunc: func(req *http.Request) (*http.Response, error) {
if req.URL.Path != "/fixedvalue" {
t.Errorf("Expected to request '/fixedvalue', got: %s", req.URL.Path)
}
if req.Header.Get("Accept") != "application/json" {
t.Errorf("Expected Accept: application/json header, got: %s", req.Header.Get("Accept"))
}
responseBody := ioutil.NopCloser(bytes.NewReader([]byte(`{"value":"fixed"}`)))
return &http.Response{
StatusCode: 200,
Body: responseBody,
}, nil
},
}
value, _ := GetFixedValue("https://example.com")
if value != "fixed" {
t.Errorf("Expected 'fixed', got %s", value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment