refactor(tests): simplify Pico channel test configuration with helper function

This commit is contained in:
zeed.w.zeed 2026-03-24 13:17:45 +08:00 committed by zeed-w-beez
parent c5d5783b29
commit 170bc84547
No known key found for this signature in database
GPG key ID: 80D9A061738877DD
4 changed files with 47 additions and 6 deletions

View file

@ -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)
}

View file

@ -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"}`

View file

@ -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"
}

View file

@ -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,