Skip to content

Commit 8f5dd34

Browse files
authored
When calling httpclient.New without any options return a new client with default configuration (#1312)
* When calling httpclient.New without any options return a new client with default configuration. * fix tests * test that we do not return the default http client or transport
1 parent 8a2eeb3 commit 8f5dd34

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

backend/httpclient/http_client.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,12 @@ import (
1414
)
1515

1616
// New creates a new http.Client.
17-
// If opts is nil the http.DefaultClient will be returned.
1817
// If no middlewares are provided the DefaultMiddlewares will be used. If you
1918
// provide middlewares you have to manually add the DefaultMiddlewares for it to be
2019
// enabled.
2120
// Note: Middlewares will be executed in the same order as provided.
2221
// Note: If more than one Options is provided a panic is raised.
2322
func New(opts ...Options) (*http.Client, error) {
24-
if opts == nil {
25-
return http.DefaultClient, nil
26-
}
27-
2823
clientOpts := createOptions(opts...)
2924
transport, err := GetTransport(clientOpts)
3025
if err != nil {

backend/httpclient/http_client_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,27 @@ import (
1010
)
1111

1212
func TestNewClient(t *testing.T) {
13-
t.Run("New() without any opts should return http.DefaultClient", func(t *testing.T) {
13+
t.Run("New() without any opts should return expected http client and middlewares", func(t *testing.T) {
1414
client, err := New()
1515
require.NoError(t, err)
16-
require.Same(t, http.DefaultClient, client)
16+
require.NotNil(t, client)
17+
require.NotSame(t, http.DefaultClient, client)
18+
19+
require.Equal(t, 30*time.Second, client.Timeout)
20+
require.NotSame(t, &http.DefaultTransport, &client.Transport)
21+
})
22+
23+
t.Run("New() with opts and no middlewares specified should return expected http client and middlewares", func(t *testing.T) {
24+
client, err := New(Options{})
25+
require.NoError(t, err)
26+
require.NotNil(t, client)
27+
require.NotSame(t, http.DefaultClient, client)
28+
29+
require.Equal(t, 30*time.Second, client.Timeout)
30+
require.NotSame(t, &http.DefaultTransport, &client.Transport)
1731
})
1832

19-
t.Run("New() with opts and no middleware should return expected http client and transport", func(t *testing.T) {
33+
t.Run("New() with opts and empty middlewares should return expected http client and transport", func(t *testing.T) {
2034
client, err := New(Options{
2135
Timeouts: &TimeoutOptions{
2236
Timeout: time.Second,
@@ -35,6 +49,7 @@ func TestNewClient(t *testing.T) {
3549
require.NotNil(t, client)
3650
require.Equal(t, time.Second, client.Timeout)
3751

52+
// this only works when there are no middlewares, otherwise the transport is wrapped
3853
transport, ok := client.Transport.(*http.Transport)
3954
require.True(t, ok)
4055
require.NotNil(t, transport)

0 commit comments

Comments
 (0)