You've already forked sap-jenkins-library
mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-07-13 01:30:24 +02:00
Correction in abaputils (#1958)
* Fix * Adapt errors * Consider unexpected JSON * defer closing the response body * Add comments to explain function * Improve assert statements semantically * Change comment format due to CodeClimate * Extract sub function
This commit is contained in:
@ -311,7 +311,7 @@ repositories:
|
||||
func TestHandleHTTPError(t *testing.T) {
|
||||
t.Run("Test", func(t *testing.T) {
|
||||
|
||||
errorValue := "HTTP 400"
|
||||
errorValue := "Received Error"
|
||||
abapErrorCode := "abapErrorCode"
|
||||
abapErrorMessage := "abapErrorMessage"
|
||||
bodyString := `{"error" : { "code" : "` + abapErrorCode + `", "message" : { "lang" : "en", "value" : "` + abapErrorMessage + `" } } }`
|
||||
@ -327,7 +327,47 @@ func TestHandleHTTPError(t *testing.T) {
|
||||
|
||||
err := HandleHTTPError(&resp, receivedErr, message, ConnectionDetailsHTTP{})
|
||||
assert.Error(t, err, "Error was expected")
|
||||
assert.EqualError(t, err, fmt.Sprintf("%s: %s - %s", errorValue, abapErrorCode, abapErrorMessage))
|
||||
assert.EqualError(t, err, fmt.Sprintf("%s: %s - %s", receivedErr.Error(), abapErrorCode, abapErrorMessage))
|
||||
log.Entry().Info(err.Error())
|
||||
})
|
||||
|
||||
t.Run("Non JSON Error", func(t *testing.T) {
|
||||
|
||||
errorValue := "Received Error"
|
||||
bodyString := `Error message`
|
||||
body := []byte(bodyString)
|
||||
|
||||
resp := http.Response{
|
||||
Status: "400 Bad Request",
|
||||
StatusCode: 400,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(body)),
|
||||
}
|
||||
receivedErr := errors.New(errorValue)
|
||||
message := "Custom Error Message"
|
||||
|
||||
err := HandleHTTPError(&resp, receivedErr, message, ConnectionDetailsHTTP{})
|
||||
assert.Error(t, err, "Error was expected")
|
||||
assert.EqualError(t, err, fmt.Sprintf("%s", receivedErr.Error()))
|
||||
log.Entry().Info(err.Error())
|
||||
})
|
||||
|
||||
t.Run("Different JSON Error", func(t *testing.T) {
|
||||
|
||||
errorValue := "Received Error"
|
||||
bodyString := `{"abap" : { "key" : "value" } }`
|
||||
body := []byte(bodyString)
|
||||
|
||||
resp := http.Response{
|
||||
Status: "400 Bad Request",
|
||||
StatusCode: 400,
|
||||
Body: ioutil.NopCloser(bytes.NewReader(body)),
|
||||
}
|
||||
receivedErr := errors.New(errorValue)
|
||||
message := "Custom Error Message"
|
||||
|
||||
err := HandleHTTPError(&resp, receivedErr, message, ConnectionDetailsHTTP{})
|
||||
assert.Error(t, err, "Error was expected")
|
||||
assert.EqualError(t, err, fmt.Sprintf("%s", receivedErr.Error()))
|
||||
log.Entry().Info(err.Error())
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user