提取 proxyurl.Parse() 公共包,将分散在 6 处的代理 URL 验证逻辑 统一收敛,确保无效代理配置在创建时立即失败,永不静默回退直连。 主要变更: - 新增 proxyurl 包:统一 TrimSpace → url.Parse → Host 校验 → Scheme 白名单 - socks5:// 自动升级为 socks5h://,防止 DNS 泄漏(大小写不敏感) - antigravity: http.ProxyURL → proxyutil.ConfigureTransportProxy 支持 SOCKS5 - openai_oauth: 删除 newOpenAIOAuthHTTPClient,收编至 httpclient.GetClient - 移除未使用的 ProxyStrict 字段(fail-fast 已是全局默认行为) - 补充 15 个 proxyurl 测试 + pricing/usage fail-fast 测试
121 lines
3.3 KiB
Go
121 lines
3.3 KiB
Go
package repository
|
|
|
|
import (
|
|
"reflect"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
"unsafe"
|
|
|
|
"github.com/imroc/req/v3"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func forceHTTPVersion(t *testing.T, client *req.Client) string {
|
|
t.Helper()
|
|
transport := client.GetTransport()
|
|
field := reflect.ValueOf(transport).Elem().FieldByName("forceHttpVersion")
|
|
require.True(t, field.IsValid(), "forceHttpVersion field not found")
|
|
require.True(t, field.CanAddr(), "forceHttpVersion field not addressable")
|
|
return reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem().String()
|
|
}
|
|
|
|
func TestGetSharedReqClient_ForceHTTP2SeparatesCache(t *testing.T) {
|
|
sharedReqClients = sync.Map{}
|
|
base := reqClientOptions{
|
|
ProxyURL: "http://proxy.local:8080",
|
|
Timeout: time.Second,
|
|
}
|
|
clientDefault, err := getSharedReqClient(base)
|
|
require.NoError(t, err)
|
|
|
|
force := base
|
|
force.ForceHTTP2 = true
|
|
clientForce, err := getSharedReqClient(force)
|
|
require.NoError(t, err)
|
|
|
|
require.NotSame(t, clientDefault, clientForce)
|
|
require.NotEqual(t, buildReqClientKey(base), buildReqClientKey(force))
|
|
}
|
|
|
|
func TestGetSharedReqClient_ReuseCachedClient(t *testing.T) {
|
|
sharedReqClients = sync.Map{}
|
|
opts := reqClientOptions{
|
|
ProxyURL: "http://proxy.local:8080",
|
|
Timeout: 2 * time.Second,
|
|
}
|
|
first, err := getSharedReqClient(opts)
|
|
require.NoError(t, err)
|
|
second, err := getSharedReqClient(opts)
|
|
require.NoError(t, err)
|
|
require.Same(t, first, second)
|
|
}
|
|
|
|
func TestGetSharedReqClient_IgnoresNonClientCache(t *testing.T) {
|
|
sharedReqClients = sync.Map{}
|
|
opts := reqClientOptions{
|
|
ProxyURL: " http://proxy.local:8080 ",
|
|
Timeout: 3 * time.Second,
|
|
}
|
|
key := buildReqClientKey(opts)
|
|
sharedReqClients.Store(key, "invalid")
|
|
|
|
client, err := getSharedReqClient(opts)
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, client)
|
|
loaded, ok := sharedReqClients.Load(key)
|
|
require.True(t, ok)
|
|
require.IsType(t, "invalid", loaded)
|
|
}
|
|
|
|
func TestGetSharedReqClient_ImpersonateAndProxy(t *testing.T) {
|
|
sharedReqClients = sync.Map{}
|
|
opts := reqClientOptions{
|
|
ProxyURL: " http://proxy.local:8080 ",
|
|
Timeout: 4 * time.Second,
|
|
Impersonate: true,
|
|
}
|
|
client, err := getSharedReqClient(opts)
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, client)
|
|
require.Equal(t, "http://proxy.local:8080|4s|true|false", buildReqClientKey(opts))
|
|
}
|
|
|
|
func TestGetSharedReqClient_InvalidProxyURL(t *testing.T) {
|
|
sharedReqClients = sync.Map{}
|
|
opts := reqClientOptions{
|
|
ProxyURL: "://missing-scheme",
|
|
Timeout: time.Second,
|
|
}
|
|
_, err := getSharedReqClient(opts)
|
|
require.Error(t, err)
|
|
require.Contains(t, err.Error(), "invalid proxy URL")
|
|
}
|
|
|
|
func TestGetSharedReqClient_ProxyURLMissingHost(t *testing.T) {
|
|
sharedReqClients = sync.Map{}
|
|
opts := reqClientOptions{
|
|
ProxyURL: "http://",
|
|
Timeout: time.Second,
|
|
}
|
|
_, err := getSharedReqClient(opts)
|
|
require.Error(t, err)
|
|
require.Contains(t, err.Error(), "proxy URL missing host")
|
|
}
|
|
|
|
func TestCreateOpenAIReqClient_Timeout120Seconds(t *testing.T) {
|
|
sharedReqClients = sync.Map{}
|
|
client, err := createOpenAIReqClient("http://proxy.local:8080")
|
|
require.NoError(t, err)
|
|
require.Equal(t, 120*time.Second, client.GetClient().Timeout)
|
|
}
|
|
|
|
func TestCreateGeminiReqClient_ForceHTTP2Disabled(t *testing.T) {
|
|
sharedReqClients = sync.Map{}
|
|
client, err := createGeminiReqClient("http://proxy.local:8080")
|
|
require.NoError(t, err)
|
|
require.Equal(t, "", forceHTTPVersion(t, client))
|
|
}
|