From 170bc8454702ef8b5f1475b7444bf5ce3efce4d4 Mon Sep 17 00:00:00 2001 From: "zeed.w.zeed" Date: Tue, 24 Mar 2026 13:17:45 +0800 Subject: [PATCH] refactor(tests): simplify Pico channel test configuration with helper function --- pkg/channels/pico/pico_http_test.go | 10 +++++-- pkg/config/security_integration_test.go | 2 +- web/backend/api/gateway_host.go | 37 +++++++++++++++++++++++++ web/backend/api/pico.go | 4 +-- 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/pkg/channels/pico/pico_http_test.go b/pkg/channels/pico/pico_http_test.go index 3df71b52f..36e7c0f77 100644 --- a/pkg/channels/pico/pico_http_test.go +++ b/pkg/channels/pico/pico_http_test.go @@ -14,11 +14,15 @@ import ( "github.com/sipeed/picoclaw/pkg/config" ) +func testPicoConfig(token string) config.PicoConfig { + var c config.PicoConfig + c.SetToken(token) + return c +} + func newTestPicoHTTP(t *testing.T) *PicoChannel { t.Helper() - cfg := config.PicoConfig{} - cfg.SetToken("tok") - ch, err := NewPicoChannel(cfg, bus.NewMessageBus()) + ch, err := NewPicoChannel(testPicoConfig("tok"), bus.NewMessageBus()) if err != nil { t.Fatal(err) } diff --git a/pkg/config/security_integration_test.go b/pkg/config/security_integration_test.go index 002988f2f..f864574f5 100644 --- a/pkg/config/security_integration_test.go +++ b/pkg/config/security_integration_test.go @@ -19,7 +19,7 @@ import ( func TestJSONUnmarshalPrivateFields(t *testing.T) { type testStruct struct { PublicField string `json:"public"` - privateField string + privateField string // unexported: encoding/json ignores even if JSON has "private" } data := `{"public": "pub", "privateField": "priv"}` diff --git a/web/backend/api/gateway_host.go b/web/backend/api/gateway_host.go index 592571a28..ddba7d42c 100644 --- a/web/backend/api/gateway_host.go +++ b/web/backend/api/gateway_host.go @@ -93,6 +93,19 @@ func requestWSScheme(r *http.Request) string { return "ws" } +func requestHTTPScheme(r *http.Request) string { + if forwarded := strings.TrimSpace(r.Header.Get("X-Forwarded-Proto")); forwarded != "" { + proto := strings.ToLower(strings.TrimSpace(strings.Split(forwarded, ",")[0])) + if proto == "https" { + return "https" + } + } + if r.TLS != nil { + return "https" + } + return "http" +} + func (h *Handler) buildWsURL(r *http.Request, cfg *config.Config) string { host := h.effectiveGatewayBindHost(cfg) if host == "" || host == "0.0.0.0" { @@ -106,3 +119,27 @@ func (h *Handler) buildWsURL(r *http.Request, cfg *config.Config) string { } return requestWSScheme(r) + "://" + net.JoinHostPort(host, strconv.Itoa(wsPort)) + "/pico/ws" } + +func (h *Handler) buildPicoEventsURL(r *http.Request, cfg *config.Config) string { + host := h.effectiveGatewayBindHost(cfg) + if host == "" || host == "0.0.0.0" { + host = requestHostName(r) + } + webPort := h.serverPort + if webPort == 0 { + webPort = 18800 + } + return requestHTTPScheme(r) + "://" + net.JoinHostPort(host, strconv.Itoa(webPort)) + "/pico/events" +} + +func (h *Handler) buildPicoSendURL(r *http.Request, cfg *config.Config) string { + host := h.effectiveGatewayBindHost(cfg) + if host == "" || host == "0.0.0.0" { + host = requestHostName(r) + } + webPort := h.serverPort + if webPort == 0 { + webPort = 18800 + } + return requestHTTPScheme(r) + "://" + net.JoinHostPort(host, strconv.Itoa(webPort)) + "/pico/send" +} diff --git a/web/backend/api/pico.go b/web/backend/api/pico.go index b60ad8cd9..7ef3cf378 100644 --- a/web/backend/api/pico.go +++ b/web/backend/api/pico.go @@ -67,7 +67,7 @@ func (h *Handler) handleGetPicoToken(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]any{ - "token": cfg.Channels.Pico.Token, + "token": cfg.Channels.Pico.Token(), "ws_url": wsURL, "events_url": eventsURL, "send_url": sendURL, @@ -168,7 +168,7 @@ func (h *Handler) handlePicoSetup(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]any{ - "token": cfg.Channels.Pico.Token, + "token": cfg.Channels.Pico.Token(), "ws_url": wsURL, "events_url": eventsURL, "send_url": sendURL,