diff options
author | Vladimir Shushlin <v.shushlin@gmail.com> | 2022-02-17 13:36:12 +0300 |
---|---|---|
committer | Vladimir Shushlin <v.shushlin@gmail.com> | 2022-02-18 13:36:52 +0300 |
commit | 92fb5e54ad42ed489c4dd93eec69fb5876d11efe (patch) | |
tree | 65db096448738e749e3e3001e47399ea6af57c36 /test/acceptance | |
parent | ae8fbc5bf6725e6fa5b5a9dec6e4ac5016ec3c6c (diff) |
refactor: Always set request timeout in tests
Diffstat (limited to 'test/acceptance')
-rw-r--r-- | test/acceptance/artifacts_test.go | 5 | ||||
-rw-r--r-- | test/acceptance/auth_test.go | 2 | ||||
-rw-r--r-- | test/acceptance/helpers_test.go | 161 |
3 files changed, 76 insertions, 92 deletions
diff --git a/test/acceptance/artifacts_test.go b/test/acceptance/artifacts_test.go index f087581c..653e31cf 100644 --- a/test/acceptance/artifacts_test.go +++ b/test/acceptance/artifacts_test.go @@ -14,9 +14,6 @@ import ( ) func TestArtifactProxyRequest(t *testing.T) { - transport := (TestHTTPSClient.Transport).(*http.Transport).Clone() - transport.ResponseHeaderTimeout = 5 * time.Second - content := "<!DOCTYPE html><html><head><title>Title of the document</title></head><body></body></html>" contentLength := int64(len(content)) testServer := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -150,8 +147,6 @@ func TestArtifactProxyRequest(t *testing.T) { } func TestPrivateArtifactProxyRequest(t *testing.T) { - setupTransport(t) - testServer := NewGitlabUnstartedServerStub(t, &stubOpts{}) keyFile, certFile := CreateHTTPSFixtureFiles(t) diff --git a/test/acceptance/auth_test.go b/test/acceptance/auth_test.go index 18b73161..c786ccf4 100644 --- a/test/acceptance/auth_test.go +++ b/test/acceptance/auth_test.go @@ -421,8 +421,6 @@ func TestAccessControlProject404DoesNotRedirect(t *testing.T) { type runPagesFunc func(t *testing.T, listeners []ListenSpec, sslCertFile string) func testAccessControl(t *testing.T, runPages runPagesFunc) { - setupTransport(t) - _, certFile := CreateHTTPSFixtureFiles(t) tests := map[string]struct { diff --git a/test/acceptance/helpers_test.go b/test/acceptance/helpers_test.go index 62a5e344..c44058ba 100644 --- a/test/acceptance/helpers_test.go +++ b/test/acceptance/helpers_test.go @@ -34,65 +34,10 @@ import ( // The HTTPS certificate isn't signed by anyone. This http client is set up // so it can talk to servers using it. var ( - // The HTTPS certificate isn't signed by anyone. This http client is set up - // so it can talk to servers using it. - TestHTTPSClient = &http.Client{ - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{RootCAs: TestCertPool}, - }, - } - - // Use HTTP with a very short timeout to repeatedly check for the server to be - // up. Again, ignore HTTP - QuickTimeoutHTTPSClient = &http.Client{ - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{RootCAs: TestCertPool}, - ResponseHeaderTimeout: 100 * time.Millisecond, - }, - } - - // Proxyv2 client - TestProxyv2Client = &http.Client{ - Transport: &http.Transport{ - DialContext: Proxyv2DialContext, - TLSClientConfig: &tls.Config{RootCAs: TestCertPool}, - }, - } - - QuickTimeoutProxyv2Client = &http.Client{ - Transport: &http.Transport{ - DialContext: Proxyv2DialContext, - TLSClientConfig: &tls.Config{RootCAs: TestCertPool}, - ResponseHeaderTimeout: 100 * time.Millisecond, - }, - } - TestCertPool = x509.NewCertPool() // Proxyv2 will create a dummy request with src 10.1.1.1:1000 // and dst 20.2.2.2:2000 - Proxyv2DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { - var d net.Dialer - - conn, err := d.DialContext(ctx, network, addr) - if err != nil { - return nil, err - } - - header := &proxyproto.Header{ - Version: 2, - Command: proxyproto.PROXY, - TransportProtocol: proxyproto.TCPv4, - SourceAddress: net.ParseIP("10.1.1.1"), - SourcePort: 1000, - DestinationAddress: net.ParseIP("20.2.2.2"), - DestinationPort: 2000, - } - - _, err = header.WriteTo(conn) - - return conn, err - } ) type tWriter struct { @@ -151,15 +96,84 @@ func supportedListeners() []ListenSpec { return listeners } -func (l ListenSpec) URL(suffix string) string { - scheme := request.SchemeHTTP +func (l ListenSpec) Scheme() string { if l.Type == request.SchemeHTTPS || l.Type == "https-proxyv2" { - scheme = request.SchemeHTTPS + return request.SchemeHTTPS } + return request.SchemeHTTP +} + +func (l ListenSpec) URL(suffix string) string { suffix = strings.TrimPrefix(suffix, "/") - return fmt.Sprintf("%s://%s/%s", scheme, l.JoinHostPort(), suffix) + return fmt.Sprintf("%s://%s/%s", l.Scheme(), l.JoinHostPort(), suffix) +} + +type dialContext func(ctx context.Context, network, addr string) (net.Conn, error) + +func (l ListenSpec) proxyV2DialContext() dialContext { + return func(ctx context.Context, network, addr string) (net.Conn, error) { + var d net.Dialer + + // bypass DNS resolution by going directly to host and port + conn, err := d.DialContext(ctx, network, l.JoinHostPort()) + if err != nil { + return nil, err + } + + header := &proxyproto.Header{ + Version: 2, + Command: proxyproto.PROXY, + TransportProtocol: proxyproto.TCPv4, + SourceAddress: net.ParseIP("10.1.1.1"), + SourcePort: 1000, + DestinationAddress: net.ParseIP("20.2.2.2"), + DestinationPort: 2000, + } + + _, err = header.WriteTo(conn) + + return conn, err + } +} + +func (l ListenSpec) httpsDialContext() dialContext { + return func(ctx context.Context, network, addr string) (net.Conn, error) { + var d net.Dialer + + // bypass DNS resolution by going directly to host and port + return d.DialContext(ctx, network, l.JoinHostPort()) + } +} + +func (l ListenSpec) dialContext() dialContext { + if l.Type == "https-proxyv2" { + return l.proxyV2DialContext() + } + + return l.httpsDialContext() +} + +func (l ListenSpec) Client() *http.Client { + return &http.Client{ + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{RootCAs: TestCertPool}, + DialContext: l.dialContext(), + ResponseHeaderTimeout: 5 * time.Second, + }, + } +} + +// Use a very short timeout to repeatedly check for the server to be up. +func (l ListenSpec) QuickTimeoutClient() *http.Client { + return &http.Client{ + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{RootCAs: TestCertPool}, + DialContext: l.dialContext(), + ResponseHeaderTimeout: 100 * time.Millisecond, + }, + } } // Returns only once this spec points at a working TCP server @@ -177,12 +191,7 @@ func (l ListenSpec) WaitUntilRequestSucceeds(done chan struct{}) error { return err } - client := QuickTimeoutHTTPSClient - if l.Type == "https-proxyv2" { - client = QuickTimeoutProxyv2Client - } - - response, err := client.Transport.RoundTrip(req) + response, err := l.QuickTimeoutClient().Transport.RoundTrip(req) if err != nil { time.Sleep(100 * time.Millisecond) continue @@ -380,11 +389,7 @@ func GetPageFromListenerWithHeaders(t *testing.T, spec ListenSpec, host, urlSuff func DoPagesRequest(t *testing.T, spec ListenSpec, req *http.Request) (*http.Response, error) { t.Logf("curl -X %s -H'Host: %s' %s", req.Method, req.Host, req.URL) - if spec.Type == "https-proxyv2" { - return TestProxyv2Client.Do(req) - } - - return TestHTTPSClient.Do(req) + return spec.Client().Do(req) } func GetRedirectPage(t *testing.T, spec ListenSpec, host, urlsuffix string) (*http.Response, error) { @@ -419,11 +424,7 @@ func GetRedirectPageWithHeaders(t *testing.T, spec ListenSpec, host, urlsuffix s req.Host = host - if spec.Type == "https-proxyv2" { - return TestProxyv2Client.Transport.RoundTrip(req) - } - - return TestHTTPSClient.Transport.RoundTrip(req) + return spec.Client().Transport.RoundTrip(req) } func ClientWithConfig(tlsConfig *tls.Config) (*http.Client, func()) { @@ -601,13 +602,3 @@ func copyFile(dest, src string) error { _, err = io.Copy(destFile, srcFile) return err } - -func setupTransport(t *testing.T) { - t.Helper() - - transport := (TestHTTPSClient.Transport).(*http.Transport) - defer func(t time.Duration) { - transport.ResponseHeaderTimeout = t - }(transport.ResponseHeaderTimeout) - transport.ResponseHeaderTimeout = 5 * time.Second -} |