以下代码在运行appengine服务器时提取url值,但在测试期间url var为空。
为什么会出现这种情况?
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")
}
测试
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)
//...
}