1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/mock/httpClient_test.go
Jk1484 ffc931aad1
feat(golangBuild): use 'unit' build tag to include tests during test execution (#4345)
* Added unit tag as argument. Added description to runTests command. Changed code generator to have unit build tag in generated unit test files.

* Added unit build tag to all unit test files.

* added to new unit test unit build tag

* Update verify-go.yml

* small fix

---------

Co-authored-by: Muhammadali Nazarov <Muhammadali.Nazarov@acronis.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2023-05-03 21:02:11 +05:00

131 lines
2.8 KiB
Go

//go:build unit && !release
// +build unit,!release
package mock
import (
"io"
"net/http"
"testing"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/stretchr/testify/assert"
)
func TestSendRequest(t *testing.T) {
t.Parallel()
t.Run("SendRequest", func(t *testing.T) {
utils := HttpClientMock{}
method := "PUT"
url := "https://localhost"
var header http.Header
var r io.Reader
var cookies []*http.Cookie
_, err := utils.SendRequest(method, url, r, header, cookies)
assert.Error(t, err)
})
}
func TestDownloadFile(t *testing.T) {
t.Parallel()
t.Run("DownloadFile", func(t *testing.T) {
utils := HttpClientMock{
HTTPFileUtils: &FilesMock{},
}
url := "https://localhost"
filename := "testFile"
var header http.Header
var cookies []*http.Cookie
err := utils.DownloadFile(url, filename, header, cookies)
assert.NoError(t, err)
content, err := utils.HTTPFileUtils.FileRead(filename)
assert.NoError(t, err)
assert.Equal(t, "some content", string(content))
})
}
func TestSetOption(t *testing.T) {
t.Parallel()
t.Run("SetOption", func(t *testing.T) {
utils := HttpClientMock{}
options := []piperhttp.ClientOptions{
{
Username: "user",
Password: "pwd",
},
{
Username: "user2",
Password: "pwd2",
},
}
for _, option := range options {
utils.SetOptions(option)
}
assert.Equal(t, options, utils.ClientOptions)
})
}
func TestUpload(t *testing.T) {
t.Parallel()
t.Run("Upload", func(t *testing.T) {
utils := HttpClientMock{}
data := piperhttp.UploadRequestData{}
_, err := utils.Upload(data)
assert.Error(t, err)
})
}
func TestUploadRequest(t *testing.T) {
t.Parallel()
t.Run("UploadRequest", func(t *testing.T) {
utils := HttpClientMock{
ReturnFileUploadStatus: 200,
FileUploads: map[string]string{
"key": "value",
},
}
method := "PUT"
url := "https://localhost"
file := "test-7.8.9.tgz"
fieldName := ""
uploadType := ""
var header http.Header
var cookies []*http.Cookie
returnFileUploadStatus := 200
response, err := utils.UploadRequest(method, url, file, fieldName, header, cookies, uploadType)
assert.NoError(t, err)
assert.Equal(t, returnFileUploadStatus, response.StatusCode)
})
}
func TestUploadFile(t *testing.T) {
t.Parallel()
t.Run("UploadFile", func(t *testing.T) {
utils := HttpClientMock{
ReturnFileUploadStatus: 200,
FileUploads: map[string]string{
"key": "value",
},
}
url := "https://localhost"
file := "test-7.8.9.tgz"
fieldName := ""
uploadType := ""
var header http.Header
var cookies []*http.Cookie
returnFileUploadStatus := 200
response, err := utils.UploadFile(url, file, fieldName, header, cookies, uploadType)
assert.NoError(t, err)
assert.Equal(t, returnFileUploadStatus, response.StatusCode)
})
}