diff --git a/cmd/dgate-cli/commands/run_cmd.go b/cmd/dgate-cli/commands/run_cmd.go index c48a2c5..e8e728b 100644 --- a/cmd/dgate-cli/commands/run_cmd.go +++ b/cmd/dgate-cli/commands/run_cmd.go @@ -2,6 +2,7 @@ package commands import ( "fmt" + "net/http" "os" "runtime" "runtime/debug" @@ -75,9 +76,8 @@ func Run(client dgclient.DGateClient, version string) error { auth, string(password), ) } - return client.Init( - ctx.String("admin"), - authOption, + return client.Init(ctx.String("admin"), + http.DefaultClient, dgclient.WithFollowRedirect( ctx.Bool("follow"), ), @@ -89,6 +89,7 @@ func Run(client dgclient.DGateClient, version string) error { dgclient.WithVerboseLogging( ctx.Bool("verbose"), ), + authOption, ) }, Action: func(ctx *cli.Context) error { diff --git a/cmd/dgate-cli/commands/run_cmd_test.go b/cmd/dgate-cli/commands/run_cmd_test.go index 24f810f..fbd12af 100644 --- a/cmd/dgate-cli/commands/run_cmd_test.go +++ b/cmd/dgate-cli/commands/run_cmd_test.go @@ -2,6 +2,7 @@ package commands import ( "errors" + "net/http" "os" "strings" "testing" @@ -189,7 +190,11 @@ type mockDGClient struct { var _ dgclient.DGateClient = &mockDGClient{} -func (m *mockDGClient) Init(baseUrl string, opts ...dgclient.Options) error { +func (m *mockDGClient) Init( + baseUrl string, + client *http.Client, + opts ...dgclient.Options, +) error { args := m.Called(baseUrl) return args.Error(0) } diff --git a/pkg/dgclient/collection_client_test.go b/pkg/dgclient/collection_client_test.go index 32fddc6..575ebd9 100644 --- a/pkg/dgclient/collection_client_test.go +++ b/pkg/dgclient/collection_client_test.go @@ -22,7 +22,7 @@ func TestDGClient_GetCollection(t *testing.T) { }) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -40,7 +40,7 @@ func TestDGClient_CreateCollection(t *testing.T) { w.WriteHeader(http.StatusCreated) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -59,7 +59,7 @@ func TestDGClient_DeleteCollection(t *testing.T) { w.WriteHeader(http.StatusNoContent) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -83,7 +83,7 @@ func TestDGClient_ListCollection(t *testing.T) { }) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } diff --git a/pkg/dgclient/dgclient.go b/pkg/dgclient/dgclient.go index b65af41..ba429fe 100644 --- a/pkg/dgclient/dgclient.go +++ b/pkg/dgclient/dgclient.go @@ -53,7 +53,10 @@ func (d *dgateClient) Init( if client == nil { d.client = http.DefaultClient - } else if d.client.Transport == nil { + } else { + d.client = client + } + if d.client.Transport == nil { d.client.Transport = http.DefaultTransport } diff --git a/pkg/dgclient/document_client_test.go b/pkg/dgclient/document_client_test.go index 4892196..2f01387 100644 --- a/pkg/dgclient/document_client_test.go +++ b/pkg/dgclient/document_client_test.go @@ -23,7 +23,7 @@ func TestDGClient_GetDocument(t *testing.T) { }) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -41,7 +41,7 @@ func TestDGClient_CreateDocument(t *testing.T) { w.WriteHeader(http.StatusCreated) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -60,7 +60,7 @@ func TestDGClient_DeleteAllDocument(t *testing.T) { w.WriteHeader(http.StatusNoContent) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -77,7 +77,7 @@ func TestDGClient_DeleteDocument(t *testing.T) { w.WriteHeader(http.StatusNoContent) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -101,7 +101,7 @@ func TestDGClient_ListDocument(t *testing.T) { }) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } diff --git a/pkg/dgclient/domain_client_test.go b/pkg/dgclient/domain_client_test.go index 7cadcae..ecea3cb 100644 --- a/pkg/dgclient/domain_client_test.go +++ b/pkg/dgclient/domain_client_test.go @@ -22,7 +22,7 @@ func TestDGClient_GetDomain(t *testing.T) { }) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -40,7 +40,7 @@ func TestDGClient_CreateDomain(t *testing.T) { w.WriteHeader(http.StatusCreated) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -59,7 +59,7 @@ func TestDGClient_DeleteDomain(t *testing.T) { w.WriteHeader(http.StatusNoContent) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -83,7 +83,7 @@ func TestDGClient_ListDomain(t *testing.T) { }) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } diff --git a/pkg/dgclient/module_client_test.go b/pkg/dgclient/module_client_test.go index 9bcc59b..82dc763 100644 --- a/pkg/dgclient/module_client_test.go +++ b/pkg/dgclient/module_client_test.go @@ -22,7 +22,7 @@ func TestDGClient_GetModule(t *testing.T) { }) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -40,7 +40,7 @@ func TestDGClient_CreateModule(t *testing.T) { w.WriteHeader(http.StatusCreated) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -59,7 +59,7 @@ func TestDGClient_DeleteModule(t *testing.T) { w.WriteHeader(http.StatusNoContent) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -83,7 +83,7 @@ func TestDGClient_ListModule(t *testing.T) { }) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } diff --git a/pkg/dgclient/namespace_client_test.go b/pkg/dgclient/namespace_client_test.go index d761889..0903a9b 100644 --- a/pkg/dgclient/namespace_client_test.go +++ b/pkg/dgclient/namespace_client_test.go @@ -22,7 +22,7 @@ func TestDGClient_GetNamespace(t *testing.T) { }) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -40,7 +40,7 @@ func TestDGClient_CreateNamespace(t *testing.T) { w.WriteHeader(http.StatusCreated) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -59,7 +59,7 @@ func TestDGClient_DeleteNamespace(t *testing.T) { w.WriteHeader(http.StatusNoContent) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -83,7 +83,7 @@ func TestDGClient_ListNamespace(t *testing.T) { }) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } diff --git a/pkg/dgclient/route_client_test.go b/pkg/dgclient/route_client_test.go index 6efff1c..adc9eef 100644 --- a/pkg/dgclient/route_client_test.go +++ b/pkg/dgclient/route_client_test.go @@ -24,7 +24,7 @@ func TestDGClient_GetRoute(t *testing.T) { }) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -44,7 +44,7 @@ func TestDGClient_CreateRoute(t *testing.T) { w.WriteHeader(http.StatusCreated) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -65,7 +65,7 @@ func TestDGClient_DeleteRoute(t *testing.T) { w.WriteHeader(http.StatusNoContent) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -91,7 +91,7 @@ func TestDGClient_ListRoute(t *testing.T) { }) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } diff --git a/pkg/dgclient/secret_client_test.go b/pkg/dgclient/secret_client_test.go index 2ab769a..6ac1535 100644 --- a/pkg/dgclient/secret_client_test.go +++ b/pkg/dgclient/secret_client_test.go @@ -22,7 +22,7 @@ func TestDGClient_GetSecret(t *testing.T) { }) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -40,7 +40,7 @@ func TestDGClient_CreateSecret(t *testing.T) { w.WriteHeader(http.StatusCreated) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -59,7 +59,7 @@ func TestDGClient_DeleteSecret(t *testing.T) { w.WriteHeader(http.StatusNoContent) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -83,7 +83,7 @@ func TestDGClient_ListSecret(t *testing.T) { }) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } diff --git a/pkg/dgclient/service_client_test.go b/pkg/dgclient/service_client_test.go index 2ba4483..23e4ef3 100644 --- a/pkg/dgclient/service_client_test.go +++ b/pkg/dgclient/service_client_test.go @@ -22,7 +22,7 @@ func TestDGClient_GetService(t *testing.T) { }) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -40,7 +40,7 @@ func TestDGClient_CreateService(t *testing.T) { w.WriteHeader(http.StatusCreated) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -59,7 +59,7 @@ func TestDGClient_DeleteService(t *testing.T) { w.WriteHeader(http.StatusNoContent) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) } @@ -83,7 +83,7 @@ func TestDGClient_ListService(t *testing.T) { }) })) client := dgclient.NewDGateClient() - err := client.Init(server.URL) + err := client.Init(server.URL, server.Client()) if err != nil { t.Fatal(err) }