Skip to content

Commit 69221c1

Browse files
committed
add test for https connection rejection - ensure correct error handling in the custom dialer
1 parent 179cc7c commit 69221c1

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

internal/api/proxy_test.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,11 @@ func TestWithProxyTransport_ProxyRejectsConnect(t *testing.T) {
350350
{"502 bad gateway", http.StatusBadGateway, "upstream unreachable", "Bad Gateway"},
351351
}
352352

353+
// Use a local target so we never depend on external DNS.
354+
target := startTargetServer(t)
355+
353356
for _, tt := range tests {
354-
t.Run(tt.name, func(t *testing.T) {
355-
// Start a proxy that always rejects CONNECT with the given status.
357+
t.Run("http proxy/"+tt.name, func(t *testing.T) {
356358
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
357359
http.Error(w, tt.body, tt.statusCode)
358360
}))
@@ -363,14 +365,40 @@ func TestWithProxyTransport_ProxyRejectsConnect(t *testing.T) {
363365
t.Cleanup(transport.CloseIdleConnections)
364366
client := &http.Client{Transport: transport, Timeout: 10 * time.Second}
365367

366-
_, err := client.Get("https://example.com")
368+
_, err := client.Get(target.URL)
367369
if err == nil {
368370
t.Fatal("expected error, got nil")
369371
}
370372
if !strings.Contains(err.Error(), tt.wantErr) {
371373
t.Errorf("error should contain %q, got: %v", tt.wantErr, err)
372374
}
373375
})
376+
377+
t.Run("https proxy/"+tt.name, func(t *testing.T) {
378+
// The HTTPS proxy path uses a custom dialer with its own error
379+
// formatting that includes the status, body, and redacted proxy URL.
380+
srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
381+
http.Error(w, tt.body, tt.statusCode)
382+
}))
383+
srv.StartTLS()
384+
t.Cleanup(srv.Close)
385+
386+
proxyURL, _ := url.Parse(srv.URL)
387+
transport := withProxyTransport(newTestTransport(), proxyURL, "")
388+
t.Cleanup(transport.CloseIdleConnections)
389+
client := &http.Client{Transport: transport, Timeout: 10 * time.Second}
390+
391+
_, err := client.Get(target.URL)
392+
if err == nil {
393+
t.Fatal("expected error, got nil")
394+
}
395+
if !strings.Contains(err.Error(), fmt.Sprintf("%d", tt.statusCode)) {
396+
t.Errorf("error should contain status code %d, got: %v", tt.statusCode, err)
397+
}
398+
if !strings.Contains(err.Error(), tt.body) {
399+
t.Errorf("error should contain body %q, got: %v", tt.body, err)
400+
}
401+
})
374402
}
375403
}
376404

0 commit comments

Comments
 (0)