Skip to content

Commit

Permalink
small refactor on dgclient for better support for changing the client
Browse files Browse the repository at this point in the history
  • Loading branch information
bubbajoe committed May 12, 2024
1 parent bd88d95 commit 3d32574
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 38 deletions.
7 changes: 4 additions & 3 deletions cmd/dgate-cli/commands/run_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"fmt"
"net/http"
"os"
"runtime"
"runtime/debug"
Expand Down Expand Up @@ -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"),
),
Expand All @@ -89,6 +89,7 @@ func Run(client dgclient.DGateClient, version string) error {
dgclient.WithVerboseLogging(
ctx.Bool("verbose"),
),
authOption,
)
},
Action: func(ctx *cli.Context) error {
Expand Down
7 changes: 6 additions & 1 deletion cmd/dgate-cli/commands/run_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"errors"
"net/http"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/dgclient/collection_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/dgclient/dgclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/dgclient/document_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/dgclient/domain_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/dgclient/module_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/dgclient/namespace_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/dgclient/route_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/dgclient/secret_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/dgclient/service_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 3d32574

Please sign in to comment.