Skip to content

Commit

Permalink
fix nil ref and remove WithHttpClient hook for dgclient
Browse files Browse the repository at this point in the history
  • Loading branch information
bubbajoe committed May 12, 2024
1 parent 38f3df2 commit bd88d95
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 33 deletions.
34 changes: 15 additions & 19 deletions pkg/dgclient/dgclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type DGateClient interface {
Init(baseUrl string, opts ...Options) error
Init(string, *http.Client, ...Options) error
DGateNamespaceClient
DGateModuleClient
DGateRouteClient
Expand All @@ -32,7 +32,11 @@ func NewDGateClient() DGateClient {
return &dgateClient{}
}

func (d *dgateClient) Init(baseUrl string, opts ...Options) error {
func (d *dgateClient) Init(
baseUrl string,
client *http.Client,
opts ...Options,
) error {
if !strings.Contains(baseUrl, "://") {
baseUrl = "http://" + baseUrl
}
Expand All @@ -47,25 +51,18 @@ func (d *dgateClient) Init(baseUrl string, opts ...Options) error {
d.baseUrl = bUrl
}

for _, opt := range opts {
if opt != nil {
opt(d)
}
}
if d.client == nil {
if client == nil {
d.client = http.DefaultClient
} else if d.client.Transport == nil {
d.client.Transport = http.DefaultTransport
}
return nil
}

func WithHttpClient(client *http.Client) Options {
return func(dc DGateClient) {
if d, ok := dc.(*dgateClient); ok {
d.client = client
for _, opt := range opts {
if opt != nil {
opt(d)
}
}
return nil
}

type customTransport struct {
Expand Down Expand Up @@ -117,12 +114,11 @@ func WithBasicAuth(username, password string) Options {
func WithFollowRedirect(follow bool) Options {
return func(dc DGateClient) {
if d, ok := dc.(*dgateClient); ok {
if follow {
d.client.CheckRedirect = nil
} else {
d.client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
d.client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if follow {
d.client.CheckRedirect = nil
}
return http.ErrUseLastResponse
}
}
}
Expand Down
22 changes: 8 additions & 14 deletions pkg/dgclient/dgclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ func TestDGClient_OptionsWithRedirect(t *testing.T) {
}))
defer server.Close()

err := client.Init(server.URL,
dgclient.WithHttpClient(server.Client()),
err := client.Init(server.URL, server.Client(),
dgclient.WithFollowRedirect(true),
)
if err != nil {
Expand Down Expand Up @@ -55,8 +54,7 @@ func TestDGClient_OptionsRedirectError(t *testing.T) {
}))
defer server.Close()

err := client.Init(server.URL,
dgclient.WithHttpClient(server.Client()),
err := client.Init(server.URL, server.Client(),
dgclient.WithBasicAuth("user", "password"),
dgclient.WithFollowRedirect(false),
)
Expand Down Expand Up @@ -84,8 +82,7 @@ func TestDGClient_OptionsWithBasicAuth(t *testing.T) {
}))
defer server.Close()

err := client.Init(server.URL,
dgclient.WithHttpClient(server.Client()),
err := client.Init(server.URL, server.Client(),
dgclient.WithVerboseLogging(true),
dgclient.WithBasicAuth("user", "password"),
)
Expand Down Expand Up @@ -113,8 +110,7 @@ func TestDGClient_OptionsBasicAuthError(t *testing.T) {
}))
defer server.Close()

err := client.Init(server.URL,
dgclient.WithHttpClient(server.Client()),
err := client.Init(server.URL, server.Client(),
dgclient.WithBasicAuth("user", "wrongpassword"),
dgclient.WithVerboseLogging(true),
)
Expand Down Expand Up @@ -142,8 +138,7 @@ func TestDGClient_OptionsWithUserAgent(t *testing.T) {
}))
defer server.Close()

err := client.Init(server.URL,
dgclient.WithHttpClient(server.Client()),
err := client.Init(server.URL, server.Client(),
dgclient.WithVerboseLogging(true),
dgclient.WithUserAgent("test"),
)
Expand Down Expand Up @@ -171,8 +166,7 @@ func TestDGClient_OptionsWithUserAgent2(t *testing.T) {
}))
defer server.Close()

err := client.Init(server.URL,
dgclient.WithHttpClient(server.Client()),
err := client.Init(server.URL, server.Client(),
dgclient.WithUserAgent("test"),
dgclient.WithVerboseLogging(true),
)
Expand All @@ -188,15 +182,15 @@ func TestDGClient_OptionsWithUserAgent2(t *testing.T) {

func TestDGClient_Init_ParseURLError(t *testing.T) {
var client = dgclient.NewDGateClient()
err := client.Init("://#/:asdm")
err := client.Init("://#/:asdm", nil)
if err == nil {
t.Fatal("expected error")
}
}

func TestDGClient_Init_EmptyHostError(t *testing.T) {
var client = dgclient.NewDGateClient()
err := client.Init("")
err := client.Init("", nil)
if err == nil {
t.Fatal("expected error")
}
Expand Down

0 comments on commit bd88d95

Please sign in to comment.