go - echo c.Get("user") does not work in testing env [echo middleware testing] -
i attempting test golang api built on echo framework/router. have following test.....
func testlogout(t *testing.t) { loadconfig() db := stubdbs(t) convey("when post /logout", t, func() { convey("with valid token, should aa success msg , logged out", func() { e := echo.new() e.use(middleware.jwtwithconfig(middleware.jwtconfig{ signingkey: []byte("secret"), tokenlookup: "query:user", })) req, err := http.newrequest(echo.post, "auth/logout", strings.newreader("")) if err == nil { req.header.set(echo.headerauthorization, fmt.sprintf("bearer %v", token)) rec := httptest.newrecorder() c := e.newcontext(req, rec) logout(db, models.redis)(c) body := getjsonbody(rec.body.string()) error := body.path("error").data() msg := body.path("error").data().(string) pw := body.path("data.user.password").data().(string) token := body.path("data.user.token").data() so(error, shouldequal, nil) so(msg, shouldequal, "logged out") so(rec.code, shouldequal, 200) so(pw, shouldequal, "") so(token, shouldequal, nil) } }) }) }
and in controller....
//logout ... func logout(db *sqlx.db, r *redis.client) echo.handlerfunc { return func(c echo.context) error { var user models.user log.println(c.get("user")) //<-----this logs nil on testing if c.get("user") == nil { res := createautherrorresponse(user, "invalid token") return c.json(http.statusok, res) } token := c.get("user").(*jwt.token) err := models.redis.del(token.raw).err() if err != nil { handleerr(err) res := createautherrorresponse(user, "token not in storage") return c.json(http.statusok, res) } user.password = "" user.token = null.stringfrom("") res := createauthsuccessresponse(user, "logged out.") return c.json(http.statusok, res) } }
any advice on how can test functionality in cho framework? behavior not expect (c.get("user") not act same on testing in live env).
through pain able figure out. here working code. explanation follow.
convey("with valid token, should success msg , logged out", func() { e := echo.new() req, err := http.newrequest(echo.post, "auth/logout", strings.newreader("")) if err == nil { req.header.set(echo.headerauthorization, fmt.sprintf("bearer %v", token)) rec := httptest.newrecorder() c := e.newcontext(req, rec) middleware.jwtwithconfig(middleware.jwtconfig{ signingkey: []byte("secret"), })(logout(db, models.redis))(c) body := getjsonbody(rec.body.string()) error := body.path("error").data() msg := body.path("msg").data().(string) pw := body.path("data.user.password").data().(string) token := body.path("data.user.token").data() so(error, shouldequal, nil) so(msg, shouldequal, "logged out") so(rec.code, shouldequal, 200) so(pw, shouldequal, "") so(token, shouldequal, nil) } })
i had set header not executing jwt echo middleware meant token not validated , made accesisible using c.get() command.
the key here testing echo middlware without running server requires execute middleware function, takes handler function (this function want test) , takes context. so, function should follows test middleware routes. if caps need replace specific case.
a: middleware.middleware_func(necessary_configs) // <--- returns echo.middlewarefunc b: (your_handler(req, res)) // <---echo.middlewarefunc takes echo.hanlderfunc argument , pass handler test req, res (c) // <--finally, call resulting compose function test context a(b())(c)
Comments
Post a Comment