You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	Handle empty http response correctly (#3805)
This commit is contained in:
		| @@ -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) | ||||
|   | ||||
| @@ -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() | ||||
|  | ||||
|   | ||||
| @@ -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) | ||||
| 	}) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user