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

This commit is contained in:
zz96 2026-03-24 13:17:45 +08:00
parent ce7259693f
commit 69a834bfb2
4 changed files with 53 additions and 11 deletions

View file

@ -14,9 +14,15 @@ import (
"github.com/sipeed/picoclaw/pkg/config" "github.com/sipeed/picoclaw/pkg/config"
) )
func testPicoConfig(token string) config.PicoConfig {
var c config.PicoConfig
c.SetToken(token)
return c
}
func TestPicoChannel_PostSend_Unauthorized(t *testing.T) { func TestPicoChannel_PostSend_Unauthorized(t *testing.T) {
t.Parallel() t.Parallel()
ch, err := NewPicoChannel(config.PicoConfig{Token: "tok"}, bus.NewMessageBus()) ch, err := NewPicoChannel(testPicoConfig("tok"), bus.NewMessageBus())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -37,7 +43,7 @@ func TestPicoChannel_PostSend_Unauthorized(t *testing.T) {
func TestPicoChannel_PostSend_EmptyContent(t *testing.T) { func TestPicoChannel_PostSend_EmptyContent(t *testing.T) {
t.Parallel() t.Parallel()
ch, err := NewPicoChannel(config.PicoConfig{Token: "tok"}, bus.NewMessageBus()) ch, err := NewPicoChannel(testPicoConfig("tok"), bus.NewMessageBus())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -66,7 +72,7 @@ func TestPicoChannel_PostSend_EmptyContent(t *testing.T) {
func TestPicoChannel_PostSend_MissingSession(t *testing.T) { func TestPicoChannel_PostSend_MissingSession(t *testing.T) {
t.Parallel() t.Parallel()
ch, err := NewPicoChannel(config.PicoConfig{Token: "tok"}, bus.NewMessageBus()) ch, err := NewPicoChannel(testPicoConfig("tok"), bus.NewMessageBus())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -88,7 +94,7 @@ func TestPicoChannel_PostSend_MissingSession(t *testing.T) {
func TestPicoChannel_PostSend_OK(t *testing.T) { func TestPicoChannel_PostSend_OK(t *testing.T) {
t.Parallel() t.Parallel()
ch, err := NewPicoChannel(config.PicoConfig{Token: "tok"}, bus.NewMessageBus()) ch, err := NewPicoChannel(testPicoConfig("tok"), bus.NewMessageBus())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -110,7 +116,7 @@ func TestPicoChannel_PostSend_OK(t *testing.T) {
func TestPicoChannel_GetEvents_Unauthorized(t *testing.T) { func TestPicoChannel_GetEvents_Unauthorized(t *testing.T) {
t.Parallel() t.Parallel()
ch, err := NewPicoChannel(config.PicoConfig{Token: "tok"}, bus.NewMessageBus()) ch, err := NewPicoChannel(testPicoConfig("tok"), bus.NewMessageBus())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -129,7 +135,7 @@ func TestPicoChannel_GetEvents_Unauthorized(t *testing.T) {
} }
func TestPicoChannel_SSE_ReceivesBroadcast(t *testing.T) { func TestPicoChannel_SSE_ReceivesBroadcast(t *testing.T) {
ch, err := NewPicoChannel(config.PicoConfig{Token: "tok"}, bus.NewMessageBus()) ch, err := NewPicoChannel(testPicoConfig("tok"), bus.NewMessageBus())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -172,7 +178,7 @@ func TestPicoChannel_SSE_ReceivesBroadcast(t *testing.T) {
} }
func TestPicoChannel_SSE_SecondConnectionSameSessionReplacesFirst(t *testing.T) { func TestPicoChannel_SSE_SecondConnectionSameSessionReplacesFirst(t *testing.T) {
ch, err := NewPicoChannel(config.PicoConfig{Token: "tok"}, bus.NewMessageBus()) ch, err := NewPicoChannel(testPicoConfig("tok"), bus.NewMessageBus())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -17,10 +17,9 @@ import (
// Test JSON unmarshal of private fields // Test JSON unmarshal of private fields
func TestJSONUnmarshalPrivateFields(t *testing.T) { func TestJSONUnmarshalPrivateFields(t *testing.T) {
//nolint: govet
type testStruct struct { type testStruct struct {
PublicField string `json:"public"` PublicField string `json:"public"`
privateField string `json:"private"` privateField string // unexported: encoding/json ignores even if JSON has "private"
} }
data := `{"public": "pub", "private": "priv"}` data := `{"public": "pub", "private": "priv"}`

View file

@ -93,6 +93,19 @@ func requestWSScheme(r *http.Request) string {
return "ws" 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 { func (h *Handler) buildWsURL(r *http.Request, cfg *config.Config) string {
host := h.effectiveGatewayBindHost(cfg) host := h.effectiveGatewayBindHost(cfg)
if host == "" || host == "0.0.0.0" { 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" 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") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]any{ json.NewEncoder(w).Encode(map[string]any{
"token": cfg.Channels.Pico.Token, "token": cfg.Channels.Pico.Token(),
"ws_url": wsURL, "ws_url": wsURL,
"events_url": eventsURL, "events_url": eventsURL,
"send_url": sendURL, "send_url": sendURL,
@ -168,7 +168,7 @@ func (h *Handler) handlePicoSetup(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]any{ json.NewEncoder(w).Encode(map[string]any{
"token": cfg.Channels.Pico.Token, "token": cfg.Channels.Pico.Token(),
"ws_url": wsURL, "ws_url": wsURL,
"events_url": eventsURL, "events_url": eventsURL,
"send_url": sendURL, "send_url": sendURL,