The code below extracts the url value when running the appengine server, but during tests the url var are blank.
Any ideas to why this would be?
func init() {
s := scheduleApi{}
r := NewAERouter()
r.HandleFunc("/leagues/{leagueId}/schedule", s.get).Methods("GET")
http.Handle("/", r.router)
}
func (s *scheduleApi) get(c appengine.Context, w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
fmt.Printf("=======================\n")
fmt.Printf("URL => %v\n", r.URL)
fmt.Printf("params => %v\n", params) // empty map
fmt.Printf("leageid => %v\n", params["leagueId"]) // blank
fmt.Printf("=======================\n")
}
Test
func Test_Get(t *testing.T) {
r, _ := http.NewRequest("GET", "/leagues/99/schedule", nil)
w := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
s := scheduleApi{}
c, _ := aetest.NewContext(nil)
s.get(c, w, r)
})
handler.ServeHTTP(w, r)
//...
}
Gorilla mux needs to be included in your test. In your app code you are setting up the route using mux but in your test you are not.
Here is a question dealing with this issue on go-nuts.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.