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 
			
		
		
		
	chore(http): allow to send prepared requests (#2580)
* Update http.go * fix c&p issue * convert URL to string * Apply suggestions from code review fix code climate findings * add test case
This commit is contained in:
		
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							f2f5dbecb8
						
					
				
				
					commit
					5184a76453
				
			| @@ -129,8 +129,6 @@ func (c *Client) Upload(data UploadRequestData) (*http.Response, error) { | ||||
| 		return nil, errors.New(fmt.Sprintf("Http method %v is not allowed. Possible values are %v or %v", data.Method, http.MethodPost, http.MethodPut)) | ||||
| 	} | ||||
|  | ||||
| 	httpClient := c.initialize() | ||||
|  | ||||
| 	bodyBuffer := &bytes.Buffer{} | ||||
| 	bodyWriter := multipart.NewWriter(bodyBuffer) | ||||
|  | ||||
| @@ -166,12 +164,7 @@ func (c *Client) Upload(data UploadRequestData) (*http.Response, error) { | ||||
| 	request.Header.Add("Content-Type", "multipart/form-data; boundary=\""+boundary+"\"") | ||||
| 	request.Header.Add("Connection", "Keep-Alive") | ||||
|  | ||||
| 	response, err := httpClient.Do(request) | ||||
| 	if err != nil { | ||||
| 		return response, errors.Wrapf(err, "HTTP %v request to %v failed with error", data.Method, data.URL) | ||||
| 	} | ||||
|  | ||||
| 	return c.handleResponse(response, data.URL) | ||||
| 	return c.Send(request) | ||||
| } | ||||
|  | ||||
| // SendRequest sends an http request with a defined method | ||||
| @@ -179,19 +172,22 @@ func (c *Client) Upload(data UploadRequestData) (*http.Response, error) { | ||||
| // On error, any Response can be ignored and the Response.Body | ||||
| // does not need to be closed. | ||||
| func (c *Client) SendRequest(method, url string, body io.Reader, header http.Header, cookies []*http.Cookie) (*http.Response, error) { | ||||
| 	httpClient := c.initialize() | ||||
|  | ||||
| 	request, err := c.createRequest(method, url, body, &header, cookies) | ||||
| 	if err != nil { | ||||
| 		return &http.Response{}, errors.Wrapf(err, "error creating %v request to %v", method, url) | ||||
| 	} | ||||
|  | ||||
| 	return c.Send(request) | ||||
| } | ||||
|  | ||||
| // Send sends an http request | ||||
| func (c *Client) Send(request *http.Request) (*http.Response, error) { | ||||
| 	httpClient := c.initialize() | ||||
| 	response, err := httpClient.Do(request) | ||||
| 	if err != nil { | ||||
| 		return response, errors.Wrapf(err, "error calling %v", url) | ||||
| 		return response, errors.Wrapf(err, "HTTP %v request to %v failed", request.Method, request.URL) | ||||
| 	} | ||||
|  | ||||
| 	return c.handleResponse(response, url) | ||||
| 	return c.handleResponse(response, request.URL.String()) | ||||
| } | ||||
|  | ||||
| // SetOptions sets options used for the http client | ||||
|   | ||||
| @@ -4,6 +4,7 @@ import ( | ||||
| 	"bytes" | ||||
| 	"encoding/base64" | ||||
| 	"encoding/xml" | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"io/ioutil" | ||||
| @@ -23,6 +24,40 @@ import ( | ||||
| 	"github.com/SAP/jenkins-library/pkg/log" | ||||
| ) | ||||
|  | ||||
| func TestSend(t *testing.T) { | ||||
| 	testURL := "https://example.org" | ||||
|  | ||||
| 	request, err := http.NewRequest(http.MethodGet, testURL, nil) | ||||
| 	require.NoError(t, err) | ||||
|  | ||||
| 	t.Run("success", func(t *testing.T) { | ||||
| 		// given | ||||
| 		httpmock.Activate() | ||||
| 		defer httpmock.DeactivateAndReset() | ||||
| 		httpmock.RegisterResponder(http.MethodGet, testURL, httpmock.NewStringResponder(200, `OK`)) | ||||
| 		client := Client{} | ||||
| 		client.SetOptions(ClientOptions{UseDefaultTransport: true}) | ||||
| 		// when | ||||
| 		response, err := client.Send(request) | ||||
| 		// then | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.NotNil(t, response) | ||||
| 	}) | ||||
| 	t.Run("failure", func(t *testing.T) { | ||||
| 		// given | ||||
| 		httpmock.Activate() | ||||
| 		defer httpmock.DeactivateAndReset() | ||||
| 		httpmock.RegisterResponder(http.MethodGet, testURL, httpmock.NewErrorResponder(errors.New("failure"))) | ||||
| 		client := Client{} | ||||
| 		client.SetOptions(ClientOptions{UseDefaultTransport: true}) | ||||
| 		// when | ||||
| 		response, err := client.Send(request) | ||||
| 		// then | ||||
| 		assert.Error(t, err) | ||||
| 		assert.Nil(t, response) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func TestDefaultTransport(t *testing.T) { | ||||
| 	const testURL string = "https://localhost/api" | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user