github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/sonar/client_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package sonar 5 6 import ( 7 "net/http" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/mock" 12 ) 13 14 func TestCreate(t *testing.T) { 15 testURL := "https://example.org/api/" 16 t.Run("", func(t *testing.T) { 17 // init 18 requester := Requester{ 19 Host: testURL, 20 Username: mock.Anything, 21 } 22 // test 23 request, err := requester.create(http.MethodGet, mock.Anything, &IssuesSearchOption{P: "42"}) 24 // assert 25 assert.NoError(t, err) 26 assert.Empty(t, request.URL.Opaque) 27 assert.Equal(t, http.MethodGet, request.Method) 28 assert.Equal(t, "https", request.URL.Scheme) 29 assert.Equal(t, "example.org", request.URL.Host) 30 assert.Equal(t, "/api/"+mock.Anything, request.URL.Path) 31 assert.Contains(t, request.Header.Get("Authorization"), "Basic ") 32 }) 33 } 34 35 func TestNewAPIClient(t *testing.T) { 36 tests := []struct { 37 name string 38 host string 39 want string 40 }{ 41 {name: mock.Anything, want: "https://example.org/api/", host: "https://example.org"}, 42 {name: mock.Anything, want: "https://example.org/api/", host: "https://example.org/"}, 43 {name: mock.Anything, want: "https://example.org/api/", host: "https://example.org/api"}, 44 {name: mock.Anything, want: "https://example.org/api/", host: "https://example.org/api/"}, 45 } 46 for _, tt := range tests { 47 t.Run(tt.name, func(t *testing.T) { 48 got := NewAPIClient(tt.host, mock.Anything, nil) 49 assert.Equal(t, tt.want, got.Host) 50 }) 51 } 52 }