Created
November 4, 2021 02:42
-
-
Save sonya/99e23752f6cd967c6fe5596c646cee15 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) { | |
httpmock.Activate() | |
defer httpmock.DeactivateAndReset() | |
httpmock.RegisterResponder("GET", "https://example.com/fixedvalue", | |
func(req *http.Request) (*http.Response, error) { | |
if req.Header.Get("Accept") != "application/json" { | |
t.Errorf("Expected Accept: application/json header, got: %s", req.Header.Get("Accept")) | |
} | |
resp, err := httpmock.NewJsonResponse(200, map[string]interface{}{ | |
"value": "fixed", | |
}) | |
return resp, err | |
}, | |
) | |
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