fix: use mock proxy in TestProxy

This commit is contained in:
dudaodong committed 2026-10-13 11:09:26 +08:00
1 parent 227501958f
commit 13b7b1c61e
1 file changed
+28 -8
+28 -8
View File
@@ -412,21 +412,41 @@ func TestHttpClient_AsyncSendRequest(t *testing.T) {
} }
func TestProxy(t *testing.T) { func TestProxy(t *testing.T) {
t.Parallel()
// Local target server
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
}))
defer target.Close()
// Local proxy server (basic forwarding stub)
proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// In real proxy tests, you'd validate request form and forward.
// Keep deterministic: just return OK.
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("proxied"))
}))
defer proxy.Close()
proxyURL, err := url.Parse(proxy.URL)
if err != nil {
t.Fatalf("failed to parse proxy URL: %v", err)
}
config := &HttpClientConfig{ config := &HttpClientConfig{
HandshakeTimeout: 20 * time.Second, HandshakeTimeout: 20 * time.Second,
ResponseTimeout: 40 * time.Second, ResponseTimeout: 40 * time.Second,
// Use the proxy ip to add it here Proxy: proxyURL,
//Proxy: &url.URL{
// Scheme: "http",
// Host: "46.17.63.166:18888",
//},
} }
httpClient := NewHttpClientWithConfig(config) httpClient := NewHttpClientWithConfig(config)
resp, err := httpClient.Get("https://www.ipplus360.com/getLocation") resp, err := httpClient.Get(target.URL)
if err != nil { if err != nil {
t.Log("net error: ", err.Error()) t.Fatalf("request failed: %v", err)
return
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
t.Fatalf("expected %d, got %d", http.StatusOK, resp.StatusCode) t.Fatalf("expected %d, got %d", http.StatusOK, resp.StatusCode)