Files
MyGoNavi/internal/proxy/proxy_test.go
Syngnat 88b427c41a 🔥 remove(go-test): 删除后端读取源码文本断言的测试函数
- 移除 99 个仅读取同包 .go 源码做字符串包含断言的测试函数,共 3746 行
- 判据为函数内出现 os.ReadFile 读取 .go 文件且剥离字符串字面量后不调用任何生产代码
- 保留 2 个同时调用生产接口的测试函数,清理 45 个文件中失去引用的 import
- go build、go vet 通过,测试失败集合由 40 项减至 39 项且无新增,减少项为已长期失败的墓碑用例
2026-07-26 21:37:22 +08:00

87 lines
2.2 KiB
Go

package proxy
import (
"context"
"strings"
"testing"
"GoNavi-Wails/internal/connection"
"GoNavi-Wails/shared/i18n"
)
func TestNormalizeConfigSupportsSocks5hAlias(t *testing.T) {
cfg, err := NormalizeConfig(connection.ProxyConfig{
Type: "SOCKS5H",
Host: "127.0.0.1",
Port: 1080,
})
if err != nil {
t.Fatalf("NormalizeConfig returned error: %v", err)
}
if cfg.Type != "socks5" {
t.Fatalf("expected normalized proxy type socks5, got %s", cfg.Type)
}
}
func TestForwarderCacheKeyIncludesCredentialFingerprint(t *testing.T) {
base := connection.ProxyConfig{
Type: "socks5",
Host: "127.0.0.1",
Port: 1080,
User: "tester",
Password: "first-password",
}
other := base
other.Password = "second-password"
keyA := forwarderCacheKey(base, "db.internal", 3306)
keyB := forwarderCacheKey(other, "db.internal", 3306)
if keyA == keyB {
t.Fatalf("expected different cache key for different credentials")
}
if strings.Contains(keyA, base.Password) || strings.Contains(keyB, other.Password) {
t.Fatalf("cache key should not contain raw password")
}
}
func TestNormalizeConfigUsesCurrentLanguageForValidationErrors(t *testing.T) {
SetBackendLanguage(i18n.LanguageEnUS)
t.Cleanup(func() {
SetBackendLanguage(i18n.LanguageZhCN)
})
_, err := NormalizeConfig(connection.ProxyConfig{
Type: "Shadowsocks",
Host: "127.0.0.1",
Port: 1080,
})
if err == nil {
t.Fatal("expected NormalizeConfig to reject unsupported proxy type")
}
const want = "Unsupported proxy type: Shadowsocks"
if err.Error() != want {
t.Fatalf("expected localized validation error %q, got %q", want, err.Error())
}
}
func TestDialContextUsesCurrentLanguageForHTTPConnectWrapper(t *testing.T) {
SetBackendLanguage(i18n.LanguageEnUS)
t.Cleanup(func() {
SetBackendLanguage(i18n.LanguageZhCN)
})
_, err := DialContext(context.Background(), connection.ProxyConfig{
Type: "http",
Host: "127.0.0.1",
Port: 1,
}, "tcp", "example.com:443")
if err == nil {
t.Fatal("expected DialContext to fail when proxy endpoint is unreachable")
}
if !strings.HasPrefix(err.Error(), "Failed to connect to HTTP proxy:") {
t.Fatalf("expected localized HTTP proxy wrapper, got %q", err.Error())
}
}