Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set default transport when caller provides uninitialized one #179

Merged
merged 1 commit into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 49 additions & 24 deletions kong/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kong
import (
"context"
"io/ioutil"
"net/http"
"os"
"testing"

Expand Down Expand Up @@ -57,31 +58,55 @@ func TestRootJSON(T *testing.T) {
}

func TestDo(T *testing.T) {
assert := assert.New(T)
require := require.New(T)

client, err := NewTestClient(nil, nil)
assert.NoError(err)
assert.NotNil(client)

req, err := client.NewRequest("GET", "/does-not-exist", nil, nil)
assert.NoError(err)
require.NotNil(req)
resp, err := client.Do(context.Background(), req, nil)
assert.True(IsNotFoundErr(err))
require.NotNil(resp)
assert.Equal(404, resp.StatusCode)
testcases := []struct {
name string
httpClientFunc func() *http.Client
}{
{
name: "nil http.Client",
httpClientFunc: func() *http.Client { return nil },
},
{
name: "default/uninitialized http.Client",
httpClientFunc: func() *http.Client { return &http.Client{} },
},
{
name: "default/uninitialized http.Client with HTTPClientWithHeaders",
httpClientFunc: func() *http.Client { return HTTPClientWithHeaders(&http.Client{}, nil) },
},
}

req, err = client.NewRequest("POST", "/", nil, nil)
assert.NoError(err)
require.NotNil(req)
resp, err = client.Do(context.Background(), req, nil)
require.NotNil(err)
require.NotNil(resp)
body, err := ioutil.ReadAll(resp.Body)
assert.NoError(err)
assert.Empty(body)
assert.Equal(405, resp.StatusCode)
for _, tc := range testcases {
tc := tc

T.Run(tc.name, func(T *testing.T) {
assert := assert.New(T)
require := require.New(T)

client, err := NewTestClient(nil, tc.httpClientFunc())
require.NoError(err)
require.NotNil(client)

req, err := client.NewRequest("GET", "/does-not-exist", nil, nil)
assert.NoError(err)
require.NotNil(req)
resp, err := client.Do(context.Background(), req, nil)
assert.True(IsNotFoundErr(err), "got %v", err)
require.NotNil(resp)
assert.Equal(404, resp.StatusCode)

req, err = client.NewRequest("POST", "/", nil, nil)
assert.NoError(err)
require.NotNil(req)
resp, err = client.Do(context.Background(), req, nil)
require.NotNil(err)
require.NotNil(resp)
body, err := ioutil.ReadAll(resp.Body)
assert.NoError(err)
assert.Empty(body)
assert.Equal(405, resp.StatusCode)
})
}
}

func TestMain(m *testing.M) {
Expand Down
4 changes: 2 additions & 2 deletions kong/custom_entity_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (s *CustomEntityService) Create(ctx context.Context,
o := entity.Object()
// Necessary to Marshal an empty map
// as {} and not null
if o == nil || len(o) == 0 {
if len(o) == 0 {
o = make(map[string]interface{})
}
req, err := s.client.NewRequest(method, queryPath, nil, o)
Expand Down Expand Up @@ -123,7 +123,7 @@ func (s *CustomEntityService) Update(ctx context.Context,
o := entity.Object()
// Necessary to Marshal an empty map
// as {} and not null
if o == nil || len(o) == 0 {
if len(o) == 0 {
o = make(map[string]interface{})
}
req, err := s.client.NewRequest("PATCH", queryPath, nil, o)
Expand Down
5 changes: 5 additions & 0 deletions kong/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ func HTTPClientWithHeaders(client *http.Client,
res.Transport = defaultTransport
} else {
res = client
// If a client with nil transport has been provided then set it to http's
// default transport so that the caller is still able to use it.
if res.Transport == nil {
res.Transport = http.DefaultTransport.(*http.Transport)
}
}
res.Transport = headerRoundTripper{
headers: headers,
Expand Down
24 changes: 24 additions & 0 deletions kong/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kong

import (
"net/http"
"net/http/httptest"
"reflect"
"testing"

Expand Down Expand Up @@ -524,3 +525,26 @@ func TestFillUpstreamsDefaults(T *testing.T) {
})
}
}

func TestHTTPClientWithHeaders(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}))
defer srv.Close()

assert.NotPanics(t,
func() {
client := HTTPClientWithHeaders(&http.Client{}, nil)
assert.NotNil(t, client)
},
"creating Kong's HTTP client using default/uninitialized http.Client shouldn't panic",
)

assert.NotPanics(t,
func() {
client := HTTPClientWithHeaders(nil, nil)
assert.NotNil(t, client)
},
"creating Kong's HTTP client using nil http.Client shouldn't panic",
)
}