Skip to content

Commit 2d64121

Browse files
authored
Checking for DefaultTransport in NewClient (#663)
* new tests * do not compare against defaulttransport
1 parent daab234 commit 2d64121

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,10 +882,10 @@ func generateListCacheURL(endpoint string, opts *ListOptions) (string, error) {
882882
}
883883

884884
func hasCustomTransport(hc *http.Client) bool {
885-
if hc == nil {
885+
if hc == nil || hc.Transport == nil {
886886
return false
887887
}
888-
if hc.Transport != http.DefaultTransport.(*http.Transport) {
888+
if _, ok := hc.Transport.(*http.Transport); !ok {
889889
log.Println("[WARN] Custom transport is not allowed with a custom root CA.")
890890
return true
891891
}

client_test.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"reflect"
1313
"strings"
1414
"testing"
15+
"time"
1516

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

587-
for _, setCA := range []bool{false, true} {
588-
if setCA {
589-
t.Setenv(APIHostCert, caFile.Name())
590-
}
591-
592-
client := NewClient(nil)
593-
594-
transport, err := client.resty.Transport()
595-
if err != nil {
596-
t.Fatal(err)
597-
}
598-
if setCA && (transport.TLSClientConfig == nil || transport.TLSClientConfig.RootCAs == nil) {
599-
t.Error("expected root CAs to be set")
600-
}
601-
if !setCA && transport.TLSClientConfig != nil {
602-
t.Errorf("didn't set a custom CA, but client TLS config is not nil: %#v", transport.TLSClientConfig)
603-
}
588+
tests := []struct {
589+
name string
590+
setCA bool
591+
httpClient *http.Client
592+
}{
593+
{"do not set CA", false, nil},
594+
{"set CA", true, nil},
595+
{"do not set CA, use timeout", false, &http.Client{Timeout: time.Second}},
596+
{"set CA, use timeout", true, &http.Client{Timeout: time.Second}},
597+
}
598+
599+
for _, test := range tests {
600+
t.Run(test.name, func(t *testing.T) {
601+
if test.setCA {
602+
t.Setenv(APIHostCert, caFile.Name())
603+
}
604+
605+
client := NewClient(test.httpClient)
606+
transport, err := client.resty.Transport()
607+
if err != nil {
608+
t.Fatal(err)
609+
}
610+
if test.setCA && (transport.TLSClientConfig == nil || transport.TLSClientConfig.RootCAs == nil) {
611+
t.Error("expected root CAs to be set")
612+
}
613+
if !test.setCA && transport.TLSClientConfig != nil {
614+
t.Errorf("didn't set a custom CA, but client TLS config is not nil: %#v", transport.TLSClientConfig)
615+
}
616+
})
604617
}
605618
}

0 commit comments

Comments
 (0)