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

improved Upload to handle http.Method

This commit is contained in:
Sascha Vornheder 2020-01-22 14:22:04 +01:00
parent 48a76bc4d2
commit 1ae1be9cb9
2 changed files with 37 additions and 2 deletions

View File

@ -41,12 +41,18 @@ type Sender interface {
// Uploader provides an interface to the piper http client for uid/pwd and token authenticated requests with upload capabilities
type Uploader interface {
SendRequest(method, url string, body io.Reader, header http.Header, cookies []*http.Cookie) (*http.Response, error)
UploadRequest(method, url, file, fieldName string, header http.Header, cookies []*http.Cookie) (*http.Response, error)
UploadFile(url, file, fieldName string, header http.Header, cookies []*http.Cookie) (*http.Response, error)
SetOptions(options ClientOptions)
}
// UploadFile uploads a file's content as multipart-form POST request to the specified URL
func (c *Client) UploadFile(url, file, fieldName string, header http.Header, cookies []*http.Cookie) (*http.Response, error) {
return c.UploadRequest(http.MethodPost, url, file, fieldName, header, cookies)
}
// UploadRequest uploads a file's content as multipart-form with given http method request to the specified URL
func (c *Client) UploadRequest(method, url, file, fieldName string, header http.Header, cookies []*http.Cookie) (*http.Response, error) {
httpClient := c.initialize()
bodyBuffer := &bytes.Buffer{}
@ -69,7 +75,6 @@ func (c *Client) UploadFile(url, file, fieldName string, header http.Header, coo
}
err = bodyWriter.Close()
method := http.MethodPost
request, err := c.createRequest(method, url, bodyBuffer, &header, cookies)
if err != nil {
c.logger.Debugf("New %v request to %v", method, url)

View File

@ -160,6 +160,7 @@ func TestUploadFile(t *testing.T) {
cookies []*http.Cookie
expected string
}{
{clientOptions: ClientOptions{}, method: "PUT", expected: "OK"},
{clientOptions: ClientOptions{}, method: "POST", expected: "OK"},
{clientOptions: ClientOptions{}, method: "POST", header: map[string][]string{"Testheader": []string{"Test1", "Test2"}}, expected: "OK"},
{clientOptions: ClientOptions{}, cookies: []*http.Cookie{{Name: "TestCookie1", Value: "TestValue1"}, {Name: "TestCookie2", Value: "TestValue2"}}, method: "POST", expected: "OK"},
@ -168,7 +169,7 @@ func TestUploadFile(t *testing.T) {
client := Client{logger: log.Entry().WithField("package", "SAP/jenkins-library/pkg/http")}
for key, test := range tt {
t.Run(fmt.Sprintf("Row %v", key+1), func(t *testing.T) {
t.Run(fmt.Sprintf("UploadFile Row %v", key+1), func(t *testing.T) {
client.SetOptions(test.clientOptions)
response, err := client.UploadFile(server.URL, testFile.Name(), "Field1", test.header, test.cookies)
assert.NoError(t, err, "Error occurred but none expected")
@ -193,6 +194,35 @@ func TestUploadFile(t *testing.T) {
assert.Equal(t, client.username, passedUsername)
}
if len(client.password) > 0 {
assert.Equal(t, client.password, passedPassword)
}
})
t.Run(fmt.Sprintf("UploadRequest Row %v", key+1), func(t *testing.T) {
client.SetOptions(test.clientOptions)
response, err := client.UploadRequest(test.method, server.URL, testFile.Name(), "Field1", test.header, test.cookies)
assert.NoError(t, err, "Error occurred but none expected")
content, err := ioutil.ReadAll(response.Body)
assert.NoError(t, err, "Error occurred but none expected")
assert.Equal(t, test.expected, string(content), "Returned content incorrect")
response.Body.Close()
assert.Equal(t, testFile.Name(), multipartHeader.Filename, "Uploaded file incorrect")
assert.Equal(t, fileContents, passedFileContents, "Uploaded file incorrect")
for k, h := range test.header {
assert.Containsf(t, passedHeaders, k, "Header %v not contained", k)
assert.Equalf(t, h, passedHeaders[k], "Header %v contains different value")
}
if len(test.cookies) > 0 {
assert.Equal(t, test.cookies, passedCookies, "Passed cookies not correct")
}
if len(client.username) > 0 {
assert.Equal(t, client.username, passedUsername)
}
if len(client.password) > 0 {
assert.Equal(t, client.password, passedPassword)
}