我在一个函数中设置了一个大猩猩多路复用路由器,该函数返回,如下所示*mux.Router
func MakeApp(services service.Service, pe PolicyEnforce) *mux.Router {
app := mux.NewRouter()
app.NotFoundHandler = &NotFound{}
app.Use(token.TokenMiddleware)
# ...
app.Methods(http.MethodPost).Path("/api/v1/subscription/{emisor}/mh").HandlerFunc(MakeUpdateMH(services, pe))
app.Methods(http.MethodGet).Path("/api/v1/subscription/{emisor}/mh").HandlerFunc(MakeGetMH(services, pe))
app.Methods(http.MethodPost).Path("/api/v1/subscription").HandlerFunc(MakeCreateSubscription(services, pe))
app.Methods(http.MethodGet).Path("/api/v1/subscription/{emisor}").HandlerFunc(MakeGetSubscription(services, pe))
# ...
return app
}
因此,在我的测试中,我准备了带有URL的句柄并运行它:
func (suite *AppTestSuite) TestUpdateMH() {
args := &service.UpdateMHInput{
Usuario: new(string),
Clave: new(string),
Pin: new(string),
Certificado: new(string),
Actividades: []string{},
}
reader, err := makeBody(args)
suite.NoError(err)
handle := token.TokenMiddleware(transport.MakeUpdateMH(suite._s, suite.pe))
req := httptest.NewRequest(http.MethodPut, "/api/v1/subscription/-/mh", reader)
w := httptest.NewRecorder()
t := genToken([]v43.Rol{
{
Nombre: "mh",
Permisos: []v43.Permiso{{
Sujeto: permission.MHCredentials,
Accion: permission.Update,
}},
},
})
req.Header.Add("Authorization", t)
// configura repository
suite.r.On("UpdateIssuerMH", emisor, args.Usuario, args.Clave, args.Pin, args.Certificado, args.Actividades).Return(v43.Grupo{}, nil)
handle.ServeHTTP(w, req)
resp := w.Result()
suite.Equal(http.StatusOK, resp.StatusCode, resp.Status)
}
我注意到返回而不是值映射,它应该包含并且我无法理解为什么情况并非如此。mux.Vars(r)nil{"emisor": "-"}
我已经在处理何时为空的情况,但是对于无法使用“-”或空字符串的其他路由器,此怪癖会给我带来麻烦,我做错了什么,我如何仍然可以成功运行测试而不必手动注入我的Vars?以及:这个问题会转化为生产环境吗?"emisor"
慕姐8265434
宝慕林4294392
相关分类