mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-18 23:01:23 +00:00
fix: use dual-stack fallback for proxy probe
This commit is contained in:
1 parent
e625ce3b3b
commit
de094fb63e
2 files changed
+24
-25
No files matched your search
@@ -71,24 +71,24 @@ func (s *ProxyProbeServiceSuite) TestProbeProxy_Success_IPAPI() {
|
||||
require.Equal(s.T(), "CC", info.CountryCode)
|
||||
}
|
||||
|
||||
func (s *ProxyProbeServiceSuite) TestProbeProxy_Success_HTTPBinFallback() {
|
||||
func (s *ProxyProbeServiceSuite) TestProbeProxy_Success_IPifyFallback() {
|
||||
s.setupProxyServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// ip-api 失败
|
||||
if strings.Contains(r.RequestURI, "ip-api.com") {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
// httpbin 成功
|
||||
if strings.Contains(r.RequestURI, "httpbin.org") {
|
||||
// ipify 成功
|
||||
if strings.Contains(r.RequestURI, "api64.ipify.org") {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = io.WriteString(w, `{"origin": "5.6.7.8"}`)
|
||||
_, _ = io.WriteString(w, `{"ip": "5.6.7.8"}`)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
}))
|
||||
|
||||
info, latencyMs, err := s.prober.ProbeProxy(s.ctx, s.proxySrv.URL)
|
||||
require.NoError(s.T(), err, "ProbeProxy should fallback to httpbin")
|
||||
require.NoError(s.T(), err, "ProbeProxy should fallback to ipify")
|
||||
require.GreaterOrEqual(s.T(), latencyMs, int64(0), "unexpected latency")
|
||||
require.Equal(s.T(), "5.6.7.8", info.IP)
|
||||
}
|
||||
@@ -110,8 +110,8 @@ func (s *ProxyProbeServiceSuite) TestProbeProxy_InvalidJSON() {
|
||||
_, _ = io.WriteString(w, "not-json")
|
||||
return
|
||||
}
|
||||
// httpbin 也返回无效响应
|
||||
if strings.Contains(r.RequestURI, "httpbin.org") {
|
||||
// ipify 也返回无效响应
|
||||
if strings.Contains(r.RequestURI, "api64.ipify.org") {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = io.WriteString(w, "not-json")
|
||||
return
|
||||
@@ -151,17 +151,17 @@ func (s *ProxyProbeServiceSuite) TestParseIPAPI_Failure() {
|
||||
require.ErrorContains(s.T(), err, "rate limited")
|
||||
}
|
||||
|
||||
func (s *ProxyProbeServiceSuite) TestParseHTTPBin_Success() {
|
||||
body := []byte(`{"origin": "9.8.7.6"}`)
|
||||
info, latencyMs, err := s.prober.parseHTTPBin(body, 50)
|
||||
func (s *ProxyProbeServiceSuite) TestParseIPify_Success() {
|
||||
body := []byte(`{"ip": "2001:db8::1"}`)
|
||||
info, latencyMs, err := s.prober.parseIPify(body, 50)
|
||||
require.NoError(s.T(), err)
|
||||
require.Equal(s.T(), int64(50), latencyMs)
|
||||
require.Equal(s.T(), "9.8.7.6", info.IP)
|
||||
require.Equal(s.T(), "2001:db8::1", info.IP)
|
||||
}
|
||||
|
||||
func (s *ProxyProbeServiceSuite) TestParseHTTPBin_NoIP() {
|
||||
body := []byte(`{"origin": ""}`)
|
||||
_, _, err := s.prober.parseHTTPBin(body, 50)
|
||||
func (s *ProxyProbeServiceSuite) TestParseIPify_NoIP() {
|
||||
body := []byte(`{"ip": ""}`)
|
||||
_, _, err := s.prober.parseIPify(body, 50)
|
||||
require.Error(s.T(), err)
|
||||
require.ErrorContains(s.T(), err, "no IP found")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user