My Delete Handler: (I am using "github.com/gorilla/mux")
func DeletePerson(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
item := params["id"]
fmt.Println("Item = ", item)
...
returns Item = "2" when called by the following curl command:
curl -X DELETE http://localhost:8000/address/2
However, my TEST Code:
func TestDeletePerson(t *testing.T) {
person := &Person{
UniqID: "2",
FirstName: "",
LastName: "",
EmailAddr: "",
PhoneNumb: "",
}
jsonPerson, _ := json.Marshal(person)
request, _ := http.NewRequest("DELETE", "/address/2", bytes.NewBuffer(jsonPerson))
response := httptest.NewRecorder()
DeletePerson(response, request)
Results in DeletePerson returning "" and printing "params" directly returns
map[]
Big Question - WHAT IN HELL AM I MISSING???
Is there another header parameter I have set?
Because you didn't initalize router. Try this
func TestDeletePerson(t *testing.T) {
r := mux.NewRouter()
r.HandleFunc("/adress/{id}", DeletePerson).Methods("DELETE")
request, _ := http.NewRequest("DELETE", "/adress/2", nil)
response := httptest.NewRecorder()
r.ServeHTTP(response, request)
}
Also I think you dont need to send Person object for deletion
The problem is that your solution does not test my delete handler, "DeletePerson". Curl calls "DeletePerson" with the line "curl -X DELETE localhost:8000/address/2"; and it somehow allows the mux.Vars to find apparently map["id":"2'"] where the "2" is the record id to delete. What I can not seem to do is call "DeletePerson:" in such a way it produces the desired map parameter!! What is gorillia mux.Vars reading, some internal header?? – Godfather 3 hours ago