Search code examples
restunit-testinggogorillamux

Unable to read variables from a url using gorilla mux in golang


I am trying to write a unit test using gotests and gomock to my restful service written in golang using gorilla but service fails to get variables from the url

Here is my request

req, err := http.NewRequest("GET", "product/5b5758f9931653c36bcaf0a0", nil)

actual endpoint is product/{id}

when I step into my service at the below code

params := mux.Vars(req)

params map is empty when it should have id key mapped to 5b5758f9931653c36bcaf0a0

Strange part is endpoint works fine from post man.

May I know whats wrong with the request?


Solution

  • This solved the issue

    req = mux.SetURLVars(req, map[string]string{"id": "5b5758f9931653c36bcaf0a0"})