下面的代码在运行 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 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)
//...
}
最佳答案
Gorilla mux 需要包含在您的测试中。在您的应用程序代码中,您正在使用 mux 设置路线,但在您的测试中您没有。
这是一个question疯狂处理这个问题。
关于google-app-engine - Gorilla mux 在测试期间返回空白 url 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25704851/