我在返回的函数中有一个 Gorilla Mux 路由器设置*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)
}
把手里面的东西是这样的:
func MakeUpdateMH(s service.Service, p PolicyEnforce) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
emisor := mux.Vars(r)["emisor"]
// revisa permisos
permitido := p.TienePermiso(r.Context(), permission.MHCredentials, permission.Update) && p.PuedeActuarSobreEmisor(r.Context(), emisor)
if !permitido {
reportError(w, fault.ErrPermissionDenied, http.StatusForbidden, fault.MessageForbidden, fault.MessageForbidden)
return
}
// cambia el emisor afectado por aquel obtenido de la URL
if emisor != "-" || emisor == "" {
emisor = token.GetSub(r.Context())
}
var input service.UpdateMHInput
dec := json.NewDecoder(r.Body)
err := dec.Decode(&input)
if err != nil {
http.Error(w, fault.NewBackendError("no se pudo decodificar solicitud: %v", err).Error(), http.StatusBadRequest)
return
}
output, err := s.UpdateMHCredentials(emisor, input.Usuario, input.Clave, input.Pin, input.Certificado, input.Actividades)
if err != nil {
http.Error(w, fault.NewBackendError("Error al actualizar credenciales de MH: %v", err).Error(), http.StatusInternalServerError)
return
}
enc := json.NewEncoder(w)
enc.Encode(output)
}
}
我注意到它mux.Vars(r)
返回nil
而不是值映射,它应该包含{"emisor": "-"}
,我不明白为什么不是这样。
我已经在处理 when"emisor"
为空的情况,但是对于不能使用“-”或空字符串的其他路由器,这个怪癖给我带来了麻烦,我做错了什么,我怎样才能在没有成功的情况下成功运行我的测试手动注入我的变量?还有:这个问题会转化为生产吗?