From 3dc3bca2dde8d018f9af96e01b55f0b2d362ca00 Mon Sep 17 00:00:00 2001 From: Luca Burgazzoli Date: Thu, 24 Aug 2023 13:36:44 +0200 Subject: [PATCH] fix: avoid using http.DefaultClient as it is a shared instance --- go.mod | 1 + go.sum | 2 ++ test/support/test.go | 17 +++++++++++++++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 9e6de49..d6137b5 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 822b1a0..887f99b 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/test/support/test.go b/test/support/test.go index 909b2ee..702fa81 100644 --- a/test/support/test.go +++ b/test/support/test.go @@ -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) } }