package proxy import ( "context" "net/url" "os" "testing" "time" ) func TestIPZanRequestURLAlwaysUsesJSON(t *testing.T) { provider := NewIPZanProvider() raw := "https://service.ipzan.com/core-extract?mode=auth&format=txt&num=1" got, err := provider.RequestURL(raw, 5) if err != nil { t.Fatal(err) } u, err := url.Parse(got) if err != nil { t.Fatal(err) } if format := u.Query().Get("format"); format != "json" { t.Fatalf("format=%q, want json", format) } if num := u.Query().Get("num"); num != "5" { t.Fatalf("num=%q, want 5", num) } } func TestIPZanWhitelistRateLimitIsRetryable(t *testing.T) { for _, message := range []string{ "访问频率过快,休息一下 白名单仅支持1秒访一次", "添加白名单失败: 访问频率过快", "提取频率过快", } { if isIPZanFatalMessage(message) { t.Fatalf("%q should be retryable", message) } } } func TestWhitelistGuardSerializesAndRemembersSuccessfulAdd(t *testing.T) { const ( baseURL = "https://example.test" no = "guard-test" ip = "203.0.113.99" ) first, err := acquireWhitelistLock(context.Background(), baseURL, no, ip) if err != nil { t.Fatal(err) } first.markAdded() t.Cleanup(func() { _ = os.Remove(first.path) _ = os.Remove(first.markerPath) }) ctx, cancel := context.WithTimeout(context.Background(), 80*time.Millisecond) defer cancel() if _, err := acquireWhitelistLock(ctx, baseURL, no, ip); err == nil { t.Fatal("second process should wait while the first guard is held") } first.release() second, err := acquireWhitelistLock(context.Background(), baseURL, no, ip) if err != nil { t.Fatal(err) } defer second.release() if !second.recentlyAdded() { t.Fatal("successful whitelist add should be reused by sibling instances") } }