0

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?

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

The router is initialized in tested program. I went ahead and added
the router initialization into the golang test function - it didnot fix the problem. I tried to look a gorrilla mux code - but it is not a language I understand, so no help there. I seems like there is something missing that is not obvious that I must add to the request that I do not knnow about. If Cul can do it, golang sould be able to do it!!!!
at your example below you forgot to use r.ServeHTTP(response, request), because handler doesn't know anything about your outside routes
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 the it produces the desired map parameter!! What is gorillia mux.Vars reading, some intermal header??
mux.Vars read r.URL and than try to find rule of route with it. My code was not working because I was mistaken with url, now it's work fine
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.