Google app engine Gorilla mux在测试期间返回空白url参数
下面的代码在运行appengine服务器时提取url值,但在测试期间url变量为空 你知道为什么会这样吗Google app engine Gorilla mux在测试期间返回空白url参数,google-app-engine,go,Google App Engine,Go,下面的代码在运行appengine服务器时提取url值,但在测试期间url变量为空 你知道为什么会这样吗 func init() { s := scheduleApi{} r := NewAERouter() r.HandleFunc("/leagues/{leagueId}/schedule", s.get).Methods("GET") http.Handle("/", r.router) } func (s *scheduleApi) get(c ap
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")
}
试验
测试中需要包括Gorilla mux。在应用程序代码中,您正在使用mux设置路由,但在测试中,您没有这样做 下面是一个关于如何处理这个问题的讨论
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)
//...
}