`Hi , I am new to Go and writing Testcases in Go . I have a REST server where all the data are retrieved from POST method. I am trying to write a test for methods but I am getting errors while testing.
func (r *Rest) GetToken(c *gin.Context) {
log.Println("--- PROD : GET TOKEN called ---")
data, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c = services.SendError(err, "10001", r.errorMsgList, c)
return
}
var jsonData entities.GetTokenRequest
e := json.Unmarshal(data, &jsonData)
if e != nil {
log.Println(e)
c = services.SendError(e, "10002", r.errorMsgList, c)
return
}
log.Println(jsonData)
res, err := r.cs.GetToken(jsonData)
`
if err == nil {
c.JSON(http.StatusOK, res)
return
} else {
c=services.SendError(err, "10003",r.errorMsgList, c)
return
}
}
func TestTokenHandler(t *testing.T) {
r := SetUpRouter()
reqBody, _ := mock.GetTokenRequestJson(testConfig.TokenPayload)
r.POST(testConfig.RouteList.CreateTokenApi, restService.TokenHandle)
req, _ := http.NewRequest("POST", testConfig.RouteList.CreateTokenApi, strings.NewReader(reqBody))
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
responseBody, _ := ioutil.ReadAll(w.Body)
`var tokenResponse entities.TokenResponse
`err := json.Unmarshal(responseBody, &tokenResponse)`
if err != nil {
return
}
`assert.NotEmpty`(t, tokenResponse.Data.CreateServiceTokenResponse_1.Token, "Failed to generate token")`
}