mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
Improves NewTestEndpoint():
When the HTTP code check is failing (non-200 message) the response body is printed so the failure can be identified from the test information.
This commit is contained in:
parent
0fb9bc9157
commit
b9d78a3823
1 changed files with 6 additions and 6 deletions
|
@ -86,13 +86,13 @@ func LengthOfResponse(router *gin.Engine, token string, url string,
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTestEndpoint(router *gin.Engine, token string, url string,
|
func NewTestEndpoint(router *gin.Engine, token string, url string,
|
||||||
method string, responseBody interface{}, expected_code int,
|
method string, requestBody interface{}, expected_code int,
|
||||||
expectedResponse interface{}) error {
|
expectedResponse interface{}) error {
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
// Marshal the HTTP request body
|
// Marshal the HTTP request body
|
||||||
body, err := json.Marshal(responseBody)
|
body, err := json.Marshal(requestBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to marshal reqeust body: %v", err)
|
return fmt.Errorf("Failed to marshal reqeust body: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -100,7 +100,7 @@ func NewTestEndpoint(router *gin.Engine, token string, url string,
|
||||||
if body != nil {
|
if body != nil {
|
||||||
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
|
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Faile to create new request: %v", err)
|
return fmt.Errorf("Failed 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)
|
||||||
|
@ -108,7 +108,7 @@ func NewTestEndpoint(router *gin.Engine, token string, url string,
|
||||||
} else {
|
} else {
|
||||||
req, err := http.NewRequest(method, url, nil)
|
req, err := http.NewRequest(method, url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Faile to create new request: %v", err)
|
return fmt.Errorf("Failed 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)
|
||||||
|
@ -116,8 +116,8 @@ func NewTestEndpoint(router *gin.Engine, token string, url string,
|
||||||
|
|
||||||
// Check the return HTTP Code
|
// Check the return HTTP Code
|
||||||
if w.Code != expected_code {
|
if w.Code != expected_code {
|
||||||
return fmt.Errorf("HTTP Code: Expected \"%v\". Got \"%v\".",
|
return fmt.Errorf("HTTP Code: Expected \"%v\". Got \"%v\".\n"+
|
||||||
expected_code, w.Code)
|
"Response message:\n%v", expected_code, w.Code, w.Body.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serialize expected response
|
// Serialize expected response
|
||||||
|
|
Loading…
Add table
Reference in a new issue