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.go
Oliver Nocon 5da174aeb0
feat(kubernetesDeploy): add infrastructure extensibility (#3853)
* feat(kubernetesDeploy): add infrastructure extensibility

* update comment

* update error handling

* remove trailing spaces

* chore: refactor to use reuse capabilities

* chore: add tests

* fix: use proper download function

* fix: expose credentials via groovy step

* fix: test

* chore: remove comment

* chore: address CodeClimate findings
2022-06-29 12:00:37 +02:00

61 lines
1.9 KiB
Go

//go:build !release
// +build !release
package mock
import (
"fmt"
"io"
"net/http"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
)
// HttpClientMock mock struct
type HttpClientMock struct {
ClientOptions []piperhttp.ClientOptions // set by mock
HTTPFileUtils *FilesMock
FileUploads map[string]string // set by mock
ReturnFileUploadStatus int // expected to be set upfront
ReturnFileUploadError error // expected to be set upfront
}
// SendRequest mock
func (utils *HttpClientMock) SendRequest(method string, url string, r io.Reader, header http.Header, cookies []*http.Cookie) (*http.Response, error) {
return nil, fmt.Errorf("not implemented")
}
// SetOptions mock
func (utils *HttpClientMock) SetOptions(options piperhttp.ClientOptions) {
utils.ClientOptions = append(utils.ClientOptions, options)
}
// Upload mock
func (utils *HttpClientMock) Upload(data piperhttp.UploadRequestData) (*http.Response, error) {
return nil, fmt.Errorf("not implemented")
}
// UploadRequest mock
func (utils *HttpClientMock) UploadRequest(method, url, file, fieldName string, header http.Header, cookies []*http.Cookie, uploadType string) (*http.Response, error) {
utils.FileUploads[file] = url
response := http.Response{
StatusCode: utils.ReturnFileUploadStatus,
}
return &response, utils.ReturnFileUploadError
}
// UploadFile mock
func (utils *HttpClientMock) UploadFile(url, file, fieldName string, header http.Header, cookies []*http.Cookie, uploadType string) (*http.Response, error) {
return utils.UploadRequest(http.MethodPut, url, file, fieldName, header, cookies, uploadType)
}
// DownloadFile mock
func (utils *HttpClientMock) DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error {
if utils.HTTPFileUtils != nil {
utils.HTTPFileUtils.AddFile(filename, []byte("some content"))
}
return nil
}