mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-11-26 02:11:51 +08:00
fix: clean up proxy settings and empty pool handling
This commit is contained in:
1 parent
05dd5b40e8
commit
4b037bf5b0
5 files changed
+502
-107
No files matched your search
@@ -227,7 +227,7 @@ func (m *Manager) persistProxyPoolLocked() error {
|
||||
|
||||
func (m *Manager) ProxyPoolSnapshot() ([]ProxyPoolEntry, string, []ProxySwitchLog) {
|
||||
if m.proxyPool == nil {
|
||||
return nil, "", nil
|
||||
return []ProxyPoolEntry{}, "", []ProxySwitchLog{}
|
||||
}
|
||||
m.proxyPool.mu.Lock()
|
||||
defer m.proxyPool.mu.Unlock()
|
||||
@@ -241,7 +241,7 @@ func (m *Manager) ProxyPoolSnapshot() ([]ProxyPoolEntry, string, []ProxySwitchLo
|
||||
}
|
||||
entries = append(entries, p)
|
||||
}
|
||||
logs := append([]ProxySwitchLog(nil), m.proxyPool.logs...)
|
||||
logs := append([]ProxySwitchLog{}, m.proxyPool.logs...)
|
||||
for i := range logs {
|
||||
logs[i].From, logs[i].To = maskSecrets(logs[i].From), maskSecrets(logs[i].To)
|
||||
}
|
||||
|
||||
@@ -41,6 +41,35 @@ func TestProxyPoolSnapshotMasksCredentialsAndDisabledHealth(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyPoolEmptyResponseUsesArrays(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
pool *proxyPoolState
|
||||
}{
|
||||
{name: "uninitialized"},
|
||||
{name: "empty", pool: &proxyPoolState{}},
|
||||
{name: "member without switches", pool: &proxyPoolState{entries: []proxyPoolEntry{{ProxyPoolEntry: ProxyPoolEntry{ID: "a"}, RawURL: "http://a"}}}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
s := &Server{m: &Manager{proxyPool: tc.pool}}
|
||||
w := httptest.NewRecorder()
|
||||
s.getProxyPool(w, httptest.NewRequest(http.MethodGet, "/api/settings/proxy-pool", nil))
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var payload map[string]json.RawMessage
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, field := range []string{"entries", "switch_log"} {
|
||||
if raw := strings.TrimSpace(string(payload[field])); !strings.HasPrefix(raw, "[") {
|
||||
t.Errorf("%s must be a JSON array, got %s", field, raw)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyPoolSequentialSkipsUnhealthyAndDisabled(t *testing.T) {
|
||||
m := &Manager{proxyPool: &proxyPoolState{active: "a", entries: []proxyPoolEntry{
|
||||
{ProxyPoolEntry: ProxyPoolEntry{ID: "a", Enabled: true, Health: "healthy"}, RawURL: "http://a"},
|
||||
|
||||
Reference in new issue
Block a user