feat(client): 配置拉取请求中添加 token 认证字段

- 请求 payload 中新增 token 字段,将配置中的 Token 传递给服务端
- 新增 client_test.go 验证 fetchChanged 正确发送 token 和 lastTime
This commit is contained in:
2026-09-03 21:15:18 +08:00
parent a62dbfe74c
commit c0a7631d5c
2 changed files with 46 additions and 2 deletions
+1
View File
@@ -181,6 +181,7 @@ func (c *Client) fetchChanged() error {
"version": c.config.Version, "version": c.config.Version,
"lastTime": c.lastTime, "lastTime": c.lastTime,
"ignoreGlobal": c.config.IgnoreGlobal, "ignoreGlobal": c.config.IgnoreGlobal,
"token": c.config.Token,
} }
payloadBytes, _ := json.Marshal(payload) payloadBytes, _ := json.Marshal(payload)
+43
View File
@@ -0,0 +1,43 @@
package client
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestFetchChangedSendsToken(t *testing.T) {
var received map[string]interface{}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/fetchChangedConfigs" {
t.Errorf("unexpected path: %s", r.URL.Path)
}
if err := json.NewDecoder(r.Body).Decode(&received); err != nil {
t.Errorf("decode request body: %v", err)
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"code":0,"data":{}}`))
}))
defer srv.Close()
c := New(&Config{
BaseURL: srv.URL,
AppName: "demo-app",
Env: "daily",
Token: "test-token",
})
c.lastTime = 1699999999999
if err := c.fetchChanged(); err != nil {
t.Fatalf("fetchChanged() error = %v", err)
}
if got := received["token"]; got != "test-token" {
t.Fatalf("token = %v, want %q", got, "test-token")
}
if got := received["lastTime"]; got != float64(1699999999999) {
t.Fatalf("lastTime = %v, want %v", got, 1699999999999)
}
}