github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/mock/httpClient_test.go (about) 1 //go:build unit && !release 2 // +build unit,!release 3 4 package mock 5 6 import ( 7 "io" 8 "net/http" 9 "testing" 10 11 piperhttp "github.com/SAP/jenkins-library/pkg/http" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestSendRequest(t *testing.T) { 16 t.Parallel() 17 t.Run("SendRequest", func(t *testing.T) { 18 utils := HttpClientMock{} 19 method := "PUT" 20 url := "https://localhost" 21 var header http.Header 22 var r io.Reader 23 var cookies []*http.Cookie 24 25 _, err := utils.SendRequest(method, url, r, header, cookies) 26 assert.Error(t, err) 27 28 }) 29 } 30 31 func TestDownloadFile(t *testing.T) { 32 t.Parallel() 33 t.Run("DownloadFile", func(t *testing.T) { 34 utils := HttpClientMock{ 35 HTTPFileUtils: &FilesMock{}, 36 } 37 url := "https://localhost" 38 filename := "testFile" 39 var header http.Header 40 var cookies []*http.Cookie 41 err := utils.DownloadFile(url, filename, header, cookies) 42 assert.NoError(t, err) 43 content, err := utils.HTTPFileUtils.FileRead(filename) 44 assert.NoError(t, err) 45 assert.Equal(t, "some content", string(content)) 46 }) 47 } 48 49 func TestSetOption(t *testing.T) { 50 t.Parallel() 51 t.Run("SetOption", func(t *testing.T) { 52 utils := HttpClientMock{} 53 options := []piperhttp.ClientOptions{ 54 { 55 Username: "user", 56 Password: "pwd", 57 }, 58 { 59 Username: "user2", 60 Password: "pwd2", 61 }, 62 } 63 64 for _, option := range options { 65 utils.SetOptions(option) 66 } 67 assert.Equal(t, options, utils.ClientOptions) 68 }) 69 } 70 71 func TestUpload(t *testing.T) { 72 t.Parallel() 73 t.Run("Upload", func(t *testing.T) { 74 utils := HttpClientMock{} 75 data := piperhttp.UploadRequestData{} 76 77 _, err := utils.Upload(data) 78 assert.Error(t, err) 79 80 }) 81 } 82 83 func TestUploadRequest(t *testing.T) { 84 t.Parallel() 85 t.Run("UploadRequest", func(t *testing.T) { 86 utils := HttpClientMock{ 87 ReturnFileUploadStatus: 200, 88 FileUploads: map[string]string{ 89 "key": "value", 90 }, 91 } 92 method := "PUT" 93 url := "https://localhost" 94 file := "test-7.8.9.tgz" 95 fieldName := "" 96 uploadType := "" 97 var header http.Header 98 var cookies []*http.Cookie 99 returnFileUploadStatus := 200 100 101 response, err := utils.UploadRequest(method, url, file, fieldName, header, cookies, uploadType) 102 assert.NoError(t, err) 103 assert.Equal(t, returnFileUploadStatus, response.StatusCode) 104 105 }) 106 } 107 108 func TestUploadFile(t *testing.T) { 109 t.Parallel() 110 t.Run("UploadFile", func(t *testing.T) { 111 utils := HttpClientMock{ 112 ReturnFileUploadStatus: 200, 113 FileUploads: map[string]string{ 114 "key": "value", 115 }, 116 } 117 url := "https://localhost" 118 file := "test-7.8.9.tgz" 119 fieldName := "" 120 uploadType := "" 121 var header http.Header 122 var cookies []*http.Cookie 123 returnFileUploadStatus := 200 124 125 response, err := utils.UploadFile(url, file, fieldName, header, cookies, uploadType) 126 assert.NoError(t, err) 127 assert.Equal(t, returnFileUploadStatus, response.StatusCode) 128 129 }) 130 }