I use echo framework with Custom Context:
ApiContext struct {
echo.Context
UserID int64
UserRole string
}
my middleware:
e.Use(func(h echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
cc := &common.ApiContext{c, 0, ""}
return h(cc)
}
})
my handler:
func (app *App) listEntity(c echo.Context) error {
ctx := c.(*ApiContext) // error!
....
}
my test:
func TestlistEntity(t *testing.T){
e := echo.New()
req := httptest.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetPath("/api/v1/entity/list")
if assert.NoError(t, EntityList(c)) {
assert.Equal(t, http.StatusOK rec.Code)
}
}
i got this error:
panic: interface conversion: echo.Context is *echo.context, not *common.ApiContext
in handler function type assertion
How correctly to write the test? ps. this method working fine.