2017-04-07 22 views
0

我正在嘗試測試構建在echo框架/路由器上的golang API。我有以下的測試.....echo c.Get(「user」)在測試env時不起作用[echo中間件測試]

func TestLogout(t *testing.T) { 
loadConfig() 
db := stubDBs(t) 
Convey("When you post to /logout", t, func() { 
    Convey("with a valid token, you should get aa success msg and be 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) 
     } 

    }) 
}) 
} 

,並在控制器....

//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 only 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) 
    } 
} 

我如何能測試在町框架這個功能有什麼建議?這是我不希望的行爲(c.Get(「用戶」)在測試中的行爲與現場環境中的行爲不同)。

+0

它是使用echo JWT中間件https://echo.labstack.com/cookbook/jwt –

+0

另外,如果是任何人任何幫助的缺省訪問方法。 log.Println(c.Request()。Header)測試中是否有頭文件信息,是否只能通過c.Get() –

回答

0

經過很多的痛苦,我才弄清楚了這一點。這是工作代碼。下面的解釋。

 Convey("with a valid token, you should get a success msg and be 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) 
     } 

    }) 

我已經適當地設置我的頭,但沒有執行JWT回聲中間件這意味着我的令牌不被驗證和accesisible使用c.Get()命令所做的。

這裏的關鍵在於測試echo middlware而不運行服務器需要您執行中間件功能,然後該中間件功能需要處理函數(這是您要測試的函數),然後使用上下文。所以,函數應該看起來如下來測試你的中間件路由。如果是全部大寫,則需要將其替換爲特定的大小寫。

A: middleware.MIDDLEWARE_FUNC(NECESSARY_CONFIGS) // <--- this returns echo.MiddlewareFunc 

B: (YOUR_HANDLER(req, res)) // <---echo.MiddlewareFunc takes an echo.HanlderFunc as its only argument and you pass your handler you test req, res 

(c) // <--Finally, you call the resulting compose function with your test context 

A(B())(c) 
相關問題