feat: 完善代理池与抢购链路,发布 0.5.0

This commit is contained in:
yml
2026-11-20 00:48:21 +08:00
parent 2f087ac7f1
commit 98b3dc4b16
28 changed files with 1522 additions and 204 deletions
+56
View File
@@ -2,6 +2,7 @@ package client
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"net/url"
@@ -36,6 +37,61 @@ func TestGenerateCSRFUseCTNReusesCookie(t *testing.T) {
}
}
func TestServerTimeSampleBypassesProxyClient(t *testing.T) {
timeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"error":0,"data":1795103912}`))
}))
defer timeServer.Close()
act := *activity.Get("peace")
cli := &DouyuClient{
HTTPClient: &http.Client{
Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {
return nil, errors.New("Proxy Authentication Required")
}),
},
TimeHTTPClient: timeServer.Client(),
Activity: &act,
TimeAPIURL: timeServer.URL,
Trace: &trace.Recorder{},
}
sample, err := cli.ServerTimeSample()
if err != nil {
t.Fatalf("ServerTimeSample() error = %v", err)
}
if sample.RawCurrentTime != 1795103912 {
t.Fatalf("RawCurrentTime=%d, want 1795103912", sample.RawCurrentTime)
}
if _, err := cli.ProbeServerTime(); err == nil {
t.Fatal("ProbeServerTime() should use the proxied business client")
}
}
func TestParseServerTimestamp(t *testing.T) {
cases := []struct {
name string
raw string
want int64
}{
{name: "timestamp number", raw: `1795103912`, want: 1795103912},
{name: "timestamp string", raw: `"1795103912"`, want: 1795103912},
{name: "storedetail compatibility", raw: `{"preExchange":{"currentTime":"1795103912"}}`, want: 1795103912},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, err := parseServerTimestamp(json.RawMessage(tc.raw))
if err != nil {
t.Fatalf("parseServerTimestamp() error = %v", err)
}
if got != tc.want {
t.Fatalf("parseServerTimestamp() = %d, want %d", got, tc.want)
}
})
}
}
func TestPostMultipartUseCTNPreservesCSRFCookie(t *testing.T) {
var gotCookie string
var gotContentType string