I have a problem. I am writing tests and was able to simulate a database for sql query using go-sqlmock. But how can I test handlers, that is, the headers I can’t imagine. Here is an example:
func (env *EnvironmentUser) GetUserHandler (w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
id, err := strconv.Atoi(params["id"])
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
user, err := env.Db.GetUser(int(id))
if err != nil {
if err == sql.ErrNoRows {
w.WriteHeader(http.StatusUnauthorized)
}
w.WriteHeader(http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(user)
w.WriteHeader(http.StatusOK)
}
GetUser - listing:
row := db.QueryRow("SELECT * FROM users WHERE id = $1", id)
err = row.Scan(&user.Id, &user.Email, &user.Login, &user.Fullname,
&user.Password, &user.AccVerified)
if err != nil {
return models.User{}, err
}
return user, nil
How do I check the correctness of all haders??
Aucun commentaire:
Enregistrer un commentaire