1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00

Handle empty http response correctly (#3805)

This commit is contained in:
Daniel Mieg 2022-05-25 13:57:13 +02:00 committed by GitHub
parent 52a532e8dc
commit 8ce7577a34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 6 deletions

View File

@ -323,11 +323,12 @@ type AbapBinding struct {
// ClientMock contains information about the client mock
type ClientMock struct {
Token string
Body string
BodyList []string
StatusCode int
Error error
Token string
Body string
BodyList []string
StatusCode int
Error error
NilResponse bool
}
// SetOptions sets clientOptions for a client mock
@ -336,6 +337,10 @@ func (c *ClientMock) SetOptions(opts piperhttp.ClientOptions) {}
// SendRequest sets a HTTP response for a client mock
func (c *ClientMock) SendRequest(method, url string, bdy io.Reader, hdr http.Header, cookies []*http.Cookie) (*http.Response, error) {
if c.NilResponse {
return nil, c.Error
}
var body []byte
if c.Body != "" {
body = []byte(c.Body)

View File

@ -219,7 +219,10 @@ func GetStatus(failureMessage string, connectionDetails ConnectionDetailsHTTP, c
if err != nil {
log.SetErrorCategory(log.ErrorInfrastructure)
err = HandleHTTPError(resp, err, failureMessage, connectionDetails)
return body, resp.Status, err
if resp != nil {
status = resp.Status
}
return body, status, err
}
defer resp.Body.Close()

View File

@ -6,6 +6,7 @@ import (
"os"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
@ -303,3 +304,22 @@ func TestCreateRequestBodies(t *testing.T) {
assert.Equal(t, `{"sc_name":"/DMO/REPO", "tag_name":"myTag"}`, body, "Expected different body")
})
}
func TestGetStatus(t *testing.T) {
t.Run("Graceful Exit", func(t *testing.T) {
client := &ClientMock{
NilResponse: true,
Error: errors.New("Backend Error"),
StatusCode: 500,
}
connectionDetails := ConnectionDetailsHTTP{
URL: "example.com",
}
_, status, err := GetStatus("failure message", connectionDetails, client)
assert.Error(t, err, "Expected Error")
assert.Equal(t, "", status)
})
}