From c0a7631d5c93f4f43c98fa5dc763686df7ca22d4 Mon Sep 17 00:00:00 2001 From: Odboy Date: Thu, 3 Sep 2026 21:15:18 +0800 Subject: [PATCH] =?UTF-8?q?feat(client):=20=E9=85=8D=E7=BD=AE=E6=8B=89?= =?UTF-8?q?=E5=8F=96=E8=AF=B7=E6=B1=82=E4=B8=AD=E6=B7=BB=E5=8A=A0=20token?= =?UTF-8?q?=20=E8=AE=A4=E8=AF=81=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 请求 payload 中新增 token 字段,将配置中的 Token 传递给服务端 - 新增 client_test.go 验证 fetchChanged 正确发送 token 和 lastTime --- kenaito-conf-go-client/client.go | 5 ++-- kenaito-conf-go-client/client_test.go | 43 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 kenaito-conf-go-client/client_test.go diff --git a/kenaito-conf-go-client/client.go b/kenaito-conf-go-client/client.go index eabc3b8..7b8b795 100644 --- a/kenaito-conf-go-client/client.go +++ b/kenaito-conf-go-client/client.go @@ -181,6 +181,7 @@ func (c *Client) fetchChanged() error { "version": c.config.Version, "lastTime": c.lastTime, "ignoreGlobal": c.config.IgnoreGlobal, + "token": c.config.Token, } payloadBytes, _ := json.Marshal(payload) @@ -206,8 +207,8 @@ func (c *Client) fetchChanged() error { } var wrapper struct { - Code int `json:"code"` - Data map[string]string `json:"data"` + Code int `json:"code"` + Data map[string]string `json:"data"` } if err := json.Unmarshal(body, &wrapper); err != nil { return err diff --git a/kenaito-conf-go-client/client_test.go b/kenaito-conf-go-client/client_test.go new file mode 100644 index 0000000..74e316d --- /dev/null +++ b/kenaito-conf-go-client/client_test.go @@ -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) + } +}