mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
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:
parent
41bb54e933
commit
6aee1a7aca
2 changed files with 23 additions and 10 deletions
|
@ -92,12 +92,18 @@ func NewTestEndpoint(router *gin.Engine, token string, url string,
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
if body != nil {
|
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.Set("Content-Type", "application/json")
|
||||||
req.Header.Add("Authorization", "Bearer "+token)
|
req.Header.Add("Authorization", "Bearer "+token)
|
||||||
router.ServeHTTP(w, req)
|
router.ServeHTTP(w, req)
|
||||||
} else {
|
} 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)
|
req.Header.Add("Authorization", "Bearer "+token)
|
||||||
router.ServeHTTP(w, req)
|
router.ServeHTTP(w, req)
|
||||||
}
|
}
|
||||||
|
@ -111,7 +117,7 @@ func NewTestEndpoint(router *gin.Engine, token string, url string,
|
||||||
// Serialize expected response
|
// Serialize expected response
|
||||||
expectedBytes, err := json.Marshal(expectedResponse)
|
expectedBytes, err := json.Marshal(expectedResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to marshal epxected response")
|
return fmt.Errorf("Failed to marshal epxected response: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the response
|
// 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,
|
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()
|
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")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
router.ServeHTTP(w, req)
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
@ -166,7 +182,7 @@ func NewAuthenticateForTest(router *gin.Engine, url string,
|
||||||
var body_data map[string]interface{}
|
var body_data map[string]interface{}
|
||||||
|
|
||||||
// Get the response
|
// Get the response
|
||||||
err := json.Unmarshal([]byte(w.Body.String()), &body_data)
|
err = json.Unmarshal([]byte(w.Body.String()), &body_data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package user
|
package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -26,10 +25,8 @@ func TestUserEndpoints(t *testing.T) {
|
||||||
api.Use(Authentication(true))
|
api.Use(Authentication(true))
|
||||||
RegisterUserEndpoints(api.Group("/users"))
|
RegisterUserEndpoints(api.Group("/users"))
|
||||||
|
|
||||||
credjson, _ := json.Marshal(common.CredAdmin)
|
|
||||||
|
|
||||||
token, err := common.NewAuthenticateForTest(router,
|
token, err := common.NewAuthenticateForTest(router,
|
||||||
"/api/authenticate", "POST", credjson, 200)
|
"/api/authenticate", "POST", common.CredAdmin, 200)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// test GET user/
|
// test GET user/
|
||||||
|
|
Loading…
Add table
Reference in a new issue