Created
November 4, 2021 02:45
-
-
Save sonya/adbe341c3df10370888e4115a8a6f3c7 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
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