package oci import ( "testing" ) func TestTransportForStageTimeouts(t *testing.T) { tests := []struct { name string spec *ProxySpec wantProxy bool // CONNECT 分支应设 Proxy 函数 }{ {name: "http CONNECT 代理", spec: &ProxySpec{Type: "http", Host: "127.0.0.1", Port: 8080}, wantProxy: true}, {name: "https CONNECT 代理", spec: &ProxySpec{Type: "https", Host: "127.0.0.1", Port: 8443}, wantProxy: true}, {name: "socks5 代理", spec: &ProxySpec{Type: "socks5", Host: "127.0.0.1", Port: 1080, Username: "u", Password: "p"}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { tr := transportFor(tt.spec) if tr == nil { t.Fatal("transportFor 返回 nil") } if (tr.Proxy != nil) != tt.wantProxy { t.Fatalf("Proxy 函数存在性 = %v, 期望 %v", tr.Proxy != nil, tt.wantProxy) } if tr.DialContext == nil { t.Fatal("应设置带超时的 DialContext") } if tr.TLSHandshakeTimeout != proxyTLSHandshakeTimeout { t.Fatalf("TLSHandshakeTimeout = %v, 期望 %v", tr.TLSHandshakeTimeout, proxyTLSHandshakeTimeout) } if tr.MaxIdleConnsPerHost != proxyMaxIdleConnsPerHost || tr.IdleConnTimeout != proxyIdleConnTimeout { t.Fatalf("连接池参数未设置: PerHost=%d IdleTimeout=%v", tr.MaxIdleConnsPerHost, tr.IdleConnTimeout) } }) } } func TestProxyHTTPClientReuse(t *testing.T) { tests := []struct { name string a, b *ProxySpec wantSame bool }{ { name: "同配置复用同一实例", a: &ProxySpec{Type: "socks5", Host: "10.0.0.1", Port: 1080}, b: &ProxySpec{Type: "socks5", Host: "10.0.0.1", Port: 1080}, wantSame: true, }, { name: "不同配置各自实例", a: &ProxySpec{Type: "socks5", Host: "10.0.0.1", Port: 1080}, b: &ProxySpec{Type: "socks5", Host: "10.0.0.2", Port: 1080}, }, { name: "同地址不同凭据不复用", a: &ProxySpec{Type: "socks5", Host: "10.0.0.1", Port: 1080, Username: "u1", Password: "p1"}, b: &ProxySpec{Type: "socks5", Host: "10.0.0.1", Port: 1080, Username: "u2", Password: "p2"}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { ca, cb := proxyHTTPClient(tt.a), proxyHTTPClient(tt.b) if ca == nil || cb == nil { t.Fatal("proxyHTTPClient 返回 nil") } if (ca == cb) != tt.wantSame { t.Fatalf("实例复用 = %v, 期望 %v", ca == cb, tt.wantSame) } }) } } func TestProxyHTTPClientInvalidSpec(t *testing.T) { tests := []struct { name string spec *ProxySpec }{ {name: "nil 配置", spec: nil}, {name: "缺 host", spec: &ProxySpec{Type: "socks5", Port: 1080}}, {name: "非法端口", spec: &ProxySpec{Type: "socks5", Host: "10.0.0.1"}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if c := proxyHTTPClient(tt.spec); c != nil { t.Fatalf("非法配置应返回 nil,得到 %v", c) } }) } }