1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-11-29 22:48:19 +02:00

pkg/http: Fix leaking goroutines in tests

By using the context created by the test, the goroutines produced in
http.Client is actually closed when cancelled and such, not leaked.

Signed-off-by: Josef Johansson <josef86@gmail.com>
This commit is contained in:
Josef Johansson
2024-03-03 20:17:36 +01:00
parent bb6aba4a00
commit 8f7209ba1a
3 changed files with 42 additions and 32 deletions

View File

@@ -2,6 +2,7 @@ package http
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"encoding/pem"
@@ -18,7 +19,7 @@ import (
var ipv4CertData, ipv6CertData []byte
var ipv4CertDataSource, ipv4KeyDataSource options.SecretSource
var ipv6CertDataSource, ipv6KeyDataSource options.SecretSource
var client *http.Client
var transport *http.Transport
func TestHTTPSuite(t *testing.T) {
logger.SetOutput(GinkgoWriter)
@@ -28,6 +29,17 @@ func TestHTTPSuite(t *testing.T) {
RunSpecs(t, "HTTP")
}
func httpGet(ctx context.Context, url string) (*http.Response, error) {
c := &http.Client{
Transport: transport.Clone(),
}
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
var _ = BeforeSuite(func() {
By("Generating a ipv4 self-signed cert for TLS tests", func() {
certBytes, keyBytes, err := util.GenerateCert("127.0.0.1")
@@ -70,11 +82,7 @@ var _ = BeforeSuite(func() {
certpool.AddCert(ipv4certificate)
certpool.AddCert(ipv6certificate)
transport := http.DefaultTransport.(*http.Transport).Clone()
transport = http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig.RootCAs = certpool
client = &http.Client{
Transport: transport,
}
})
})