fix: avoid using http.DefaultClient as it is a shared instance

This commit is contained in:
Luca Burgazzoli 2023-08-24 13:36:44 +02:00
parent a481c4a9ab
commit 3dc3bca2dd
No known key found for this signature in database
GPG Key ID: 238C46A40510C1A9
3 changed files with 18 additions and 2 deletions

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -8,6 +8,8 @@ import (
"testing"
"time"
"github.com/hashicorp/go-cleanhttp"
"github.com/anthhub/forwarder"
"k8s.io/client-go/tools/portforward"
@ -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
@ -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)
@ -91,6 +95,7 @@ type T struct {
t *testing.T
client *Client
once sync.Once
http *http.Client
//nolint:containedctx
ctx context.Context
@ -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()
@ -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)
}
}
@ -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)
}
}