Skip to content

Instantly share code, notes, and snippets.

@sonya
Created November 4, 2021 02:42
Show Gist options
  • Save sonya/99e23752f6cd967c6fe5596c646cee15 to your computer and use it in GitHub Desktop.
Save sonya/99e23752f6cd967c6fe5596c646cee15 to your computer and use it in GitHub Desktop.
http request mocking code samples
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