From 6aee1a7acad1bc8c5d05ba930313e57ec44486d8 Mon Sep 17 00:00:00 2001 From: smavros Date: Sun, 11 Aug 2019 19:58:41 +0200 Subject: [PATCH] Improves NewAuthenticateForTest(): - As NewTestEndpoint() the credentials are passed as interface{} and marsaling is happening inside the function. - Add check for ignored error from http.NewRequest() --- common/utilities.go | 28 ++++++++++++++++++++++------ routes/user/user_test.go | 5 +---- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/common/utilities.go b/common/utilities.go index d5a9e69..bcb1323 100644 --- a/common/utilities.go +++ b/common/utilities.go @@ -92,12 +92,18 @@ func NewTestEndpoint(router *gin.Engine, token string, url string, w := httptest.NewRecorder() if body != nil { - req, _ := http.NewRequest(method, url, bytes.NewBuffer(body)) + req, err := http.NewRequest(method, url, bytes.NewBuffer(body)) + if err != nil { + return fmt.Errorf("Faile to create new request: %v", err) + } req.Header.Set("Content-Type", "application/json") req.Header.Add("Authorization", "Bearer "+token) router.ServeHTTP(w, req) } else { - req, _ := http.NewRequest(method, url, nil) + req, err := http.NewRequest(method, url, nil) + if err != nil { + return fmt.Errorf("Faile to create new request: %v", err) + } req.Header.Add("Authorization", "Bearer "+token) router.ServeHTTP(w, req) } @@ -111,7 +117,7 @@ func NewTestEndpoint(router *gin.Engine, token string, url string, // Serialize expected response expectedBytes, err := json.Marshal(expectedResponse) if err != nil { - return fmt.Errorf("Failed to marshal epxected response") + return fmt.Errorf("Failed to marshal epxected response: %v", err) } // Check the response @@ -149,11 +155,21 @@ func TestEndpoint(t *testing.T, router *gin.Engine, token string, url string, me } func NewAuthenticateForTest(router *gin.Engine, url string, - method string, body []byte, expected_code int) (string, error) { + method string, credentials interface{}, expected_code int) (string, + error) { w := httptest.NewRecorder() - req, _ := http.NewRequest(method, url, bytes.NewBuffer(body)) + // Marshal credentials + body, err := json.Marshal(credentials) + if err != nil { + return "", fmt.Errorf("Failed to marshal credentials: %v", err) + } + + req, err := http.NewRequest(method, url, bytes.NewBuffer(body)) + if err != nil { + return "", fmt.Errorf("Faile to create new request: %v", err) + } req.Header.Set("Content-Type", "application/json") router.ServeHTTP(w, req) @@ -166,7 +182,7 @@ func NewAuthenticateForTest(router *gin.Engine, url string, var body_data map[string]interface{} // Get the response - err := json.Unmarshal([]byte(w.Body.String()), &body_data) + err = json.Unmarshal([]byte(w.Body.String()), &body_data) if err != nil { return "", err } diff --git a/routes/user/user_test.go b/routes/user/user_test.go index 56ddf4f..2aabd83 100644 --- a/routes/user/user_test.go +++ b/routes/user/user_test.go @@ -1,7 +1,6 @@ package user import ( - "encoding/json" "testing" "github.com/gin-gonic/gin" @@ -26,10 +25,8 @@ func TestUserEndpoints(t *testing.T) { api.Use(Authentication(true)) RegisterUserEndpoints(api.Group("/users")) - credjson, _ := json.Marshal(common.CredAdmin) - token, err := common.NewAuthenticateForTest(router, - "/api/authenticate", "POST", credjson, 200) + "/api/authenticate", "POST", common.CredAdmin, 200) assert.NoError(t, err) // test GET user/