Skip to content

Commit

Permalink
Merge pull request #16 from lburgazzoli/cleanhttp
Browse files Browse the repository at this point in the history
avoid using http.DefaultClient as it is a shared instance
  • Loading branch information
salaboy committed Aug 31, 2023
2 parents 6409273 + 3dc3bca commit 0f93c3b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ require (
github.com/gosuri/uitable v0.0.4 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
Expand Down
17 changes: 15 additions & 2 deletions test/support/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"testing"
"time"

"github.com/hashicorp/go-cleanhttp"

"github.com/anthhub/forwarder"
"k8s.io/client-go/tools/portforward"

Expand All @@ -33,6 +35,7 @@ type Test interface {
T() *testing.T
Ctx() context.Context
Client() *Client
HTTPClient() *http.Client

NewTestNamespace(...Option[*corev1.Namespace]) *corev1.Namespace
NewDaprControlPlane(*daprAc.DaprControlPlaneSpecApplyConfiguration) *v1alpha1.DaprControlPlane
Expand Down Expand Up @@ -75,6 +78,7 @@ func With(t *testing.T) Test {
WithT: gomega.NewWithT(t),
t: t,
ctx: ctx,
http: cleanhttp.DefaultClient(),
}

answer.SetDefaultEventuallyPollingInterval(500 * time.Millisecond)
Expand All @@ -91,6 +95,7 @@ type T struct {
t *testing.T
client *Client
once sync.Once
http *http.Client

//nolint:containedctx
ctx context.Context
Expand All @@ -115,6 +120,14 @@ func (t *T) Client() *Client {
return t.client
}

func (t *T) HTTPClient() *http.Client {
t.once.Do(func() {
t.http = cleanhttp.DefaultClient()
})

return t.http
}

func (t *T) NewTestNamespace(options ...Option[*corev1.Namespace]) *corev1.Namespace {
t.T().Helper()

Expand Down Expand Up @@ -273,7 +286,7 @@ func (t *T) GET(url string) func(g gomega.Gomega) (*http.Response, error) {
return nil, err
}

return http.DefaultClient.Do(req)
return t.HTTPClient().Do(req)
}
}

Expand All @@ -293,6 +306,6 @@ func (t *T) POST(url string, contentType string, content []byte) func(g gomega.G
req.Header.Add("Content-Type", contentType)
}

return http.DefaultClient.Do(req)
return t.HTTPClient().Do(req)
}
}

0 comments on commit 0f93c3b

Please sign in to comment.