Modifies NewTestEndpoint() and user_test:

Instead of marshaling the expected response in the test function the
    marshaling happens inside the NewTestEndpoint() where is passed as
    interface{}.
This commit is contained in:
smavros 2019-08-11 19:41:48 +02:00
parent dbd8d7ddeb
commit 41bb54e933
2 changed files with 10 additions and 5 deletions

View file

@ -87,7 +87,7 @@ func LengthOfResponse(router *gin.Engine, token string, url string,
func NewTestEndpoint(router *gin.Engine, token string, url string,
method string, body []byte, expected_code int,
expected_response []byte) error {
expectedResponse interface{}) error {
w := httptest.NewRecorder()
@ -108,9 +108,15 @@ func NewTestEndpoint(router *gin.Engine, token string, url string,
expected_code, w.Code)
}
// Serialize expected response
expectedBytes, err := json.Marshal(expectedResponse)
if err != nil {
return fmt.Errorf("Failed to marshal epxected response")
}
// Check the response
opts := jsondiff.DefaultConsoleOptions()
diff, _ := jsondiff.Compare(w.Body.Bytes(), expected_response, &opts)
diff, _ := jsondiff.Compare(w.Body.Bytes(), expectedBytes, &opts)
if diff.String() != "FullMatch" {
return fmt.Errorf("Response: Expected \"%v\". Got \"%v\".",
"FullMatch", diff.String())

View file

@ -12,7 +12,7 @@ import (
func TestUserEndpoints(t *testing.T) {
myUsers := []common.User{common.User0, common.UserA, common.UserB}
myUsers := []common.User{common.User0}
msgUsers := common.ResponseMsgUsers{Users: myUsers}
db := common.DummyInitDB()
@ -27,7 +27,6 @@ func TestUserEndpoints(t *testing.T) {
RegisterUserEndpoints(api.Group("/users"))
credjson, _ := json.Marshal(common.CredAdmin)
msgUsersjson, _ := json.Marshal(msgUsers)
token, err := common.NewAuthenticateForTest(router,
"/api/authenticate", "POST", credjson, 200)
@ -35,6 +34,6 @@ func TestUserEndpoints(t *testing.T) {
// test GET user/
err = common.NewTestEndpoint(router, token, "/api/users", "GET",
nil, 200, msgUsersjson)
nil, 200, msgUsers)
assert.NoError(t, err)
}