99 lines
2.8 KiB
Go
99 lines
2.8 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"douyu_go/internal/types"
|
|
)
|
|
|
|
func TestSharedProxyConfigRoundTripExcludesRuntimeProxy(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "shared-proxy.json")
|
|
cfg := types.Config{
|
|
ProxyEnabled: true,
|
|
ProxyURL: "http://user:pass@127.0.0.1:9000",
|
|
ProxyAPI: "https://service.ipzan.com/core-extract?mode=auth",
|
|
AccountsPerProxy: 5,
|
|
ProxySparePercent: 25,
|
|
AutoFetchProxyOnSchedule: true,
|
|
ProxyAutoWhitelist: true,
|
|
ProxyWhitelistReplace: true,
|
|
ProxyWhitelistPassword: "login-pass",
|
|
ProxyWhitelistSignKey: "0123456789abcdef",
|
|
}
|
|
if err := SaveSharedProxyConfig(path, cfg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(data), "proxyUrl") || strings.Contains(string(data), "127.0.0.1:9000") {
|
|
t.Fatalf("shared config leaked runtime proxy URL: %s", data)
|
|
}
|
|
|
|
shared, err := LoadSharedProxyConfig(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
local := types.Config{
|
|
ProxyURL: "http://runtime-only:8000",
|
|
}
|
|
ApplySharedProxyConfig(&local, shared)
|
|
if local.ProxyURL != "http://runtime-only:8000" {
|
|
t.Fatalf("ProxyURL=%q, runtime URL should remain local", local.ProxyURL)
|
|
}
|
|
if !local.ProxyEnabled || local.ProxyAPI != cfg.ProxyAPI || local.AccountsPerProxy != 5 {
|
|
t.Fatalf("unexpected merged config: %+v", local)
|
|
}
|
|
}
|
|
|
|
func TestApplySharedProxyConfigSupportsPartialOverrides(t *testing.T) {
|
|
enabled := false
|
|
shared := types.SharedProxyConfig{
|
|
SchemaVersion: SharedProxyConfigSchemaVersion,
|
|
Proxy: types.SharedProxySettings{
|
|
Enabled: &enabled,
|
|
},
|
|
}
|
|
cfg := types.Config{
|
|
ProxyEnabled: true,
|
|
ProxyAPI: "keep-api",
|
|
AccountsPerProxy: 7,
|
|
}
|
|
ApplySharedProxyConfig(&cfg, shared)
|
|
if cfg.ProxyEnabled {
|
|
t.Fatal("shared enabled=false should override local value")
|
|
}
|
|
if cfg.ProxyAPI != "keep-api" || cfg.AccountsPerProxy != 7 {
|
|
t.Fatalf("unspecified fields should remain unchanged: %+v", cfg)
|
|
}
|
|
}
|
|
|
|
func TestLoadSharedProxyConfigRejectsUnsupportedVersion(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "shared-proxy.json")
|
|
if err := os.WriteFile(path, []byte(`{"schemaVersion":2,"proxy":{"enabled":true}}`), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := LoadSharedProxyConfig(path); err == nil {
|
|
t.Fatal("expected unsupported schema error")
|
|
}
|
|
}
|
|
|
|
func TestSharedProxyConfigPointerIsSharedAcrossInstances(t *testing.T) {
|
|
t.Setenv("DOUYU_DATA_DIR", t.TempDir())
|
|
want := filepath.Join(t.TempDir(), "common-proxy.json")
|
|
if err := SaveSharedProxyConfigPointer(want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := LoadSharedProxyConfigPointer()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got != want {
|
|
t.Fatalf("LoadSharedProxyConfigPointer()=%q, want %q", got, want)
|
|
}
|
|
}
|