Skip to content

Commit

Permalink
Checking for DefaultTransport in NewClient (#663)
Browse files Browse the repository at this point in the history
* new tests

* do not compare against defaulttransport
kokes authored Jan 31, 2025
1 parent daab234 commit 2d64121
Showing 2 changed files with 32 additions and 19 deletions.
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
@@ -882,10 +882,10 @@ func generateListCacheURL(endpoint string, opts *ListOptions) (string, error) {
}

func hasCustomTransport(hc *http.Client) bool {
if hc == nil {
if hc == nil || hc.Transport == nil {
return false
}
if hc.Transport != http.DefaultTransport.(*http.Transport) {
if _, ok := hc.Transport.(*http.Transport); !ok {
log.Println("[WARN] Custom transport is not allowed with a custom root CA.")
return true
}
47 changes: 30 additions & 17 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ import (
"reflect"
"strings"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/jarcoal/httpmock"
@@ -584,22 +585,34 @@ func TestClient_CustomRootCAWithoutCustomRoundTripper(t *testing.T) {
}
defer os.Remove(caFile.Name())

for _, setCA := range []bool{false, true} {
if setCA {
t.Setenv(APIHostCert, caFile.Name())
}

client := NewClient(nil)

transport, err := client.resty.Transport()
if err != nil {
t.Fatal(err)
}
if setCA && (transport.TLSClientConfig == nil || transport.TLSClientConfig.RootCAs == nil) {
t.Error("expected root CAs to be set")
}
if !setCA && transport.TLSClientConfig != nil {
t.Errorf("didn't set a custom CA, but client TLS config is not nil: %#v", transport.TLSClientConfig)
}
tests := []struct {
name string
setCA bool
httpClient *http.Client
}{
{"do not set CA", false, nil},
{"set CA", true, nil},
{"do not set CA, use timeout", false, &http.Client{Timeout: time.Second}},
{"set CA, use timeout", true, &http.Client{Timeout: time.Second}},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.setCA {
t.Setenv(APIHostCert, caFile.Name())
}

client := NewClient(test.httpClient)
transport, err := client.resty.Transport()
if err != nil {
t.Fatal(err)
}
if test.setCA && (transport.TLSClientConfig == nil || transport.TLSClientConfig.RootCAs == nil) {
t.Error("expected root CAs to be set")
}
if !test.setCA && transport.TLSClientConfig != nil {
t.Errorf("didn't set a custom CA, but client TLS config is not nil: %#v", transport.TLSClientConfig)
}
})
}
}

0 comments on commit 2d64121

Please sign in to comment.