Skip to content

Instantly share code, notes, and snippets.

@philangist
Created May 15, 2018 01:54
Show Gist options
  • Save philangist/2537c4a088b65a4eaaad67280b2eac29 to your computer and use it in GitHub Desktop.
Save philangist/2537c4a088b65a4eaaad67280b2eac29 to your computer and use it in GitHub Desktop.
simple test scenario using net/http/httptest
type getUserTestCase struct {
Tag string
ID string
Expected *UserResponse
}
func DummyHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"placeholder": value}`)
}
func TestGetUsers(t *testing.T){
fmt.Println("Running TestGetUsers...")
c := getUserTestCase{
Tag: "Case 1 - Basic deserialization",
ID: "1000",
Expected: &UserResponse{
Data: User{
ID: 1,
FullName: "John Smith",
Email: "john.smith@gmail.com",
Country: "Antigua",
Language: "Dutch",
LastIP: "10.10.10.10",
},
},
}
httpClient := &http.Client{Timeout: time.Second * 10}
tServer := httptest.NewServer(http.HandlerFunc(DummyHandler))
defer tServer.Close()
actual, _ := GetUserData(httpClient, tServer.URL, c.ID)
if !reflect.DeepEqual(actual, c.Expected) {
t.Errorf(
"Unmarshalled value '%v' did not match expected value '%v'\n",
actual, c.Expected)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment