diff --git a/api/client/webclient/webclient_test.go b/api/client/webclient/webclient_test.go index ae3b78232687f..f5b36f08596ea 100644 --- a/api/client/webclient/webclient_test.go +++ b/api/client/webclient/webclient_test.go @@ -22,7 +22,6 @@ import ( "net" "net/http" "net/http/httptest" - "os" "testing" "github.com/stretchr/testify/require" @@ -292,10 +291,14 @@ func TestExtract(t *testing.T) { } func TestNewWebClientRespectHTTPProxy(t *testing.T) { - os.Setenv("HTTPS_PROXY", "localhost:9999") - defer os.Unsetenv("HTTPS_PROXY") + t.Setenv("HTTPS_PROXY", "localhost:9999") client := newWebClient(false /* insecure */, nil /* pool */) - _, err := client.Get("https://example.com") + resp, err := client.Get("https://example.com") + defer func() { + if resp != nil { + resp.Body.Close() + } + }() // Client should try to proxy through nonexistent server at localhost. require.Error(t, err) require.Contains(t, err.Error(), "proxyconnect") diff --git a/lib/client/https_client_test.go b/lib/client/https_client_test.go index e8ba11315392d..446695287c9c4 100644 --- a/lib/client/https_client_test.go +++ b/lib/client/https_client_test.go @@ -17,17 +17,20 @@ limitations under the License. package client import ( - "os" "testing" "github.com/stretchr/testify/require" ) func TestNewInsecureWebClientHTTPProxy(t *testing.T) { - os.Setenv("HTTPS_PROXY", "localhost:9999") - defer os.Unsetenv("HTTPS_PROXY") + t.Setenv("HTTPS_PROXY", "localhost:9999") client := NewInsecureWebClient() - _, err := client.Get("https://example.com") + resp, err := client.Get("https://example.com") + defer func() { + if resp != nil { + resp.Body.Close() + } + }() // Client should try to proxy through nonexistent server at localhost. require.Error(t, err) require.Contains(t, err.Error(), "proxyconnect") @@ -35,10 +38,14 @@ func TestNewInsecureWebClientHTTPProxy(t *testing.T) { } func TestNewClientWithPoolHTTPProxy(t *testing.T) { - os.Setenv("HTTPS_PROXY", "localhost:9999") - defer os.Unsetenv("HTTPS_PROXY") + t.Setenv("HTTPS_PROXY", "localhost:9999") client := newClientWithPool(nil) - _, err := client.Get("https://example.com") + resp, err := client.Get("https://example.com") + defer func() { + if resp != nil { + resp.Body.Close() + } + }() // Client should try to proxy through nonexistent server at localhost. require.Error(t, err) require.Contains(t, err.Error(), "proxyconnect")