Skip to content

Instantly share code, notes, and snippets.

@sonya
Created November 4, 2021 02:43
Show Gist options
  • Save sonya/e1fc0d40ffaf6be2227302949478ce65 to your computer and use it in GitHub Desktop.
Save sonya/e1fc0d40ffaf6be2227302949478ce65 to your computer and use it in GitHub Desktop.
http request mocking code samples
func TestGetFixedValue(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/fixedvalue" {
t.Errorf("Expected to request '/fixedvalue', got: %s", r.URL.Path)
}
if r.Header.Get("Accept") != "application/json" {
t.Errorf("Expected Accept: application/json header, got: %s", r.Header.Get("Accept"))
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"value":"fixed"}`))
}))
defer server.Close()
value, _ := GetFixedValue(server.URL)
if value != "fixed" {
t.Errorf("Expected 'fixed', got %s", value)
}
}
@perebaj
Copy link

perebaj commented Jan 18, 2024

pure gold

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment