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()
This commit is contained in:
smavros 2019-08-11 19:58:41 +02:00
parent 41bb54e933
commit 6aee1a7aca
2 changed files with 23 additions and 10 deletions

View file

@ -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
}

View file

@ -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/