mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-03-03 15:02:35 +02:00
Handle empty http response correctly (#3805)
This commit is contained in:
parent
52a532e8dc
commit
8ce7577a34
@ -323,11 +323,12 @@ type AbapBinding struct {
|
|||||||
|
|
||||||
// ClientMock contains information about the client mock
|
// ClientMock contains information about the client mock
|
||||||
type ClientMock struct {
|
type ClientMock struct {
|
||||||
Token string
|
Token string
|
||||||
Body string
|
Body string
|
||||||
BodyList []string
|
BodyList []string
|
||||||
StatusCode int
|
StatusCode int
|
||||||
Error error
|
Error error
|
||||||
|
NilResponse bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetOptions sets clientOptions for a client mock
|
// 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
|
// 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) {
|
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
|
var body []byte
|
||||||
if c.Body != "" {
|
if c.Body != "" {
|
||||||
body = []byte(c.Body)
|
body = []byte(c.Body)
|
||||||
|
@ -219,7 +219,10 @@ func GetStatus(failureMessage string, connectionDetails ConnectionDetailsHTTP, c
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.SetErrorCategory(log.ErrorInfrastructure)
|
log.SetErrorCategory(log.ErrorInfrastructure)
|
||||||
err = HandleHTTPError(resp, err, failureMessage, connectionDetails)
|
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()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/stretchr/testify/assert"
|
"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")
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user