Created
November 4, 2021 02:43
-
-
Save sonya/e1fc0d40ffaf6be2227302949478ce65 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) { | |
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pure gold