feat(proxy): 支持品赞分组代理与自动白名单
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -44,58 +41,9 @@ func TestParseProxyURL(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchProxyFromAPI_XKDaili(t *testing.T) {
|
||||
// 模拟星空代理正常返回
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
fmt.Fprint(w, `{ "status":100 ,"info":"成功" , "data": [{"ip": "115.230.134.174", "port": "40014", "prov": "浙江", "city": "嘉兴"}]}`)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
res, err := FetchProxyFromAPI(ts.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("FetchProxyFromAPI failed: %v", err)
|
||||
}
|
||||
if res.IP != "115.230.134.174" || res.Port != "40014" {
|
||||
t.Fatalf("unexpected res: %+v", res)
|
||||
}
|
||||
if res.ProxyURL != "http://115.230.134.174:40014" {
|
||||
t.Fatalf("unexpected proxy url: %s", res.ProxyURL)
|
||||
}
|
||||
if res.Region != "浙江 嘉兴" {
|
||||
t.Fatalf("unexpected region: %s", res.Region)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchProxyFromAPI_Error(t *testing.T) {
|
||||
// 模拟星空代理未加白名单报错
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
fmt.Fprint(w, `{ "status":211 , "info":"当前IP不在白名单内" }`)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
_, err := FetchProxyFromAPI(ts.URL)
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
if err.Error() != "代理平台返回: 当前IP不在白名单内 (状态码=211)" {
|
||||
t.Fatalf("unexpected error string: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchProxyFromAPI_Text(t *testing.T) {
|
||||
// 模拟纯文本返回
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprint(w, "221.122.91.45:3128\r\n")
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
res, err := FetchProxyFromAPI(ts.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("FetchProxyFromAPI failed: %v", err)
|
||||
}
|
||||
if res.ProxyURL != "http://221.122.91.45:3128" {
|
||||
t.Fatalf("unexpected proxy url: %s", res.ProxyURL)
|
||||
func TestMaskProxyURL(t *testing.T) {
|
||||
got := MaskProxyURL("http://user:pass@127.0.0.1:8888")
|
||||
if got != "http://127.0.0.1:8888" {
|
||||
t.Fatalf("MaskProxyURL() = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user