Skip to content

Commit 5177a8e

Browse files
committed
rename functions and address possible leaks
1 parent 3191c55 commit 5177a8e

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

internal/api/proxy_test.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414
"time"
1515
)
1616

17-
// startCONNECTProxy starts an HTTP or HTTPS CONNECT proxy on a random port.
17+
// startProxy starts an HTTP or HTTPS CONNECT proxy on a random port.
1818
// It returns the proxy URL and a channel that receives the protocol observed by
1919
// the proxy handler for each CONNECT request.
20-
func startCONNECTProxy(t *testing.T, useTLS bool) (proxyURL *url.URL, obsCh <-chan string) {
20+
func startProxy(t *testing.T, useTLS bool) (proxyURL *url.URL, obsCh <-chan string) {
2121
t.Helper()
2222

2323
ch := make(chan string, 10)
@@ -57,6 +57,10 @@ func startCONNECTProxy(t *testing.T, useTLS bool) (proxyURL *url.URL, obsCh <-ch
5757
go func() { io.Copy(destConn, clientConn); done <- struct{}{} }()
5858
go func() { io.Copy(clientConn, destConn); done <- struct{}{} }()
5959
<-done
60+
// Close both sides so the remaining goroutine unblocks.
61+
clientConn.Close()
62+
destConn.Close()
63+
<-done
6064
}))
6165

6266
if useTLS {
@@ -70,9 +74,9 @@ func startCONNECTProxy(t *testing.T, useTLS bool) (proxyURL *url.URL, obsCh <-ch
7074
return pURL, ch
7175
}
7276

73-
// startCONNECTProxyWithAuth is like startCONNECTProxy but requires
77+
// startProxyWithAuth is like startProxy but requires
7478
// Proxy-Authorization with the given username and password.
75-
func startCONNECTProxyWithAuth(t *testing.T, useTLS bool, wantUser, wantPass string) (proxyURL *url.URL) {
79+
func startProxyWithAuth(t *testing.T, useTLS bool, wantUser, wantPass string) (proxyURL *url.URL) {
7680
t.Helper()
7781

7882
srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -112,6 +116,9 @@ func startCONNECTProxyWithAuth(t *testing.T, useTLS bool, wantUser, wantPass str
112116
go func() { io.Copy(destConn, clientConn); done <- struct{}{} }()
113117
go func() { io.Copy(clientConn, destConn); done <- struct{}{} }()
114118
<-done
119+
clientConn.Close()
120+
destConn.Close()
121+
<-done
115122
}))
116123

117124
if useTLS {
@@ -148,9 +155,10 @@ func startTargetServer(t *testing.T) *httptest.Server {
148155

149156
func TestWithProxyTransport_HTTPProxy(t *testing.T) {
150157
target := startTargetServer(t)
151-
proxyURL, obsCh := startCONNECTProxy(t, false)
158+
proxyURL, obsCh := startProxy(t, false)
152159

153160
transport := withProxyTransport(newTestTransport(), proxyURL, "")
161+
t.Cleanup(transport.CloseIdleConnections)
154162
client := &http.Client{Transport: transport, Timeout: 10 * time.Second}
155163

156164
resp, err := client.Get(target.URL)
@@ -179,9 +187,10 @@ func TestWithProxyTransport_HTTPProxy(t *testing.T) {
179187

180188
func TestWithProxyTransport_HTTPSProxy(t *testing.T) {
181189
target := startTargetServer(t)
182-
proxyURL, obsCh := startCONNECTProxy(t, true)
190+
proxyURL, obsCh := startProxy(t, true)
183191

184192
transport := withProxyTransport(newTestTransport(), proxyURL, "")
193+
t.Cleanup(transport.CloseIdleConnections)
185194
client := &http.Client{Transport: transport, Timeout: 10 * time.Second}
186195

187196
resp, err := client.Get(target.URL)
@@ -212,8 +221,9 @@ func TestWithProxyTransport_ProxyAuth(t *testing.T) {
212221
target := startTargetServer(t)
213222

214223
t.Run("http proxy with auth", func(t *testing.T) {
215-
proxyURL := startCONNECTProxyWithAuth(t, false, "user", "pass")
224+
proxyURL := startProxyWithAuth(t, false, "user", "pass")
216225
transport := withProxyTransport(newTestTransport(), proxyURL, "")
226+
t.Cleanup(transport.CloseIdleConnections)
217227
client := &http.Client{Transport: transport, Timeout: 10 * time.Second}
218228

219229
resp, err := client.Get(target.URL)
@@ -229,8 +239,9 @@ func TestWithProxyTransport_ProxyAuth(t *testing.T) {
229239
})
230240

231241
t.Run("https proxy with auth", func(t *testing.T) {
232-
proxyURL := startCONNECTProxyWithAuth(t, true, "user", "s3cret")
242+
proxyURL := startProxyWithAuth(t, true, "user", "s3cret")
233243
transport := withProxyTransport(newTestTransport(), proxyURL, "")
244+
t.Cleanup(transport.CloseIdleConnections)
234245
client := &http.Client{Transport: transport, Timeout: 10 * time.Second}
235246

236247
resp, err := client.Get(target.URL)
@@ -250,9 +261,10 @@ func TestWithProxyTransport_HTTPSProxy_HTTP2ToOrigin(t *testing.T) {
250261
// Verify that when tunneling through an HTTPS proxy, the connection to
251262
// the origin target still negotiates HTTP/2 (not downgraded to HTTP/1.1).
252263
target := startTargetServer(t)
253-
proxyURL, _ := startCONNECTProxy(t, true)
264+
proxyURL, _ := startProxy(t, true)
254265

255266
transport := withProxyTransport(newTestTransport(), proxyURL, "")
267+
t.Cleanup(transport.CloseIdleConnections)
256268
client := &http.Client{Transport: transport, Timeout: 10 * time.Second}
257269

258270
resp, err := client.Get(target.URL)
@@ -289,6 +301,7 @@ func TestWithProxyTransport_ProxyRejectsConnect(t *testing.T) {
289301

290302
proxyURL, _ := url.Parse(srv.URL)
291303
transport := withProxyTransport(newTestTransport(), proxyURL, "")
304+
t.Cleanup(transport.CloseIdleConnections)
292305
client := &http.Client{Transport: transport, Timeout: 10 * time.Second}
293306

294307
_, err := client.Get("https://example.com")

0 commit comments

Comments
 (0)