mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
11207186c8
- Modify buildWsURL to use web server port (18800) instead of gateway port (18790) - Add WebSocket proxy handler to forward /pico/ws to gateway - Gateway port is read from config (cfg.Gateway.Port), defaults to 18790 - This allows WebSocket connections through the same port as the web UI, avoiding the need to expose extra ports for Tailscale/Docker
113 lines
3.4 KiB
Go
113 lines
3.4 KiB
Go
package api
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/sipeed/picoclaw/pkg/config"
|
|
"github.com/sipeed/picoclaw/web/backend/launcherconfig"
|
|
)
|
|
|
|
func TestGatewayHostOverrideUsesExplicitRuntimePublic(t *testing.T) {
|
|
configPath := filepath.Join(t.TempDir(), "config.json")
|
|
launcherPath := launcherconfig.PathForAppConfig(configPath)
|
|
if err := launcherconfig.Save(launcherPath, launcherconfig.Config{
|
|
Port: 18800,
|
|
Public: false,
|
|
}); err != nil {
|
|
t.Fatalf("launcherconfig.Save() error = %v", err)
|
|
}
|
|
|
|
h := NewHandler(configPath)
|
|
h.SetServerOptions(18800, true, true, nil)
|
|
|
|
if got := h.gatewayHostOverride(); got != "0.0.0.0" {
|
|
t.Fatalf("gatewayHostOverride() = %q, want %q", got, "0.0.0.0")
|
|
}
|
|
}
|
|
|
|
func TestBuildWsURLUsesRequestHostWhenLauncherPublicSaved(t *testing.T) {
|
|
configPath := filepath.Join(t.TempDir(), "config.json")
|
|
launcherPath := launcherconfig.PathForAppConfig(configPath)
|
|
if err := launcherconfig.Save(launcherPath, launcherconfig.Config{
|
|
Port: 18800,
|
|
Public: true,
|
|
}); err != nil {
|
|
t.Fatalf("launcherconfig.Save() error = %v", err)
|
|
}
|
|
|
|
h := NewHandler(configPath)
|
|
h.SetServerOptions(18800, false, false, nil)
|
|
|
|
cfg := config.DefaultConfig()
|
|
cfg.Gateway.Host = "127.0.0.1"
|
|
cfg.Gateway.Port = 18790
|
|
|
|
req := httptest.NewRequest("GET", "http://launcher.local/api/pico/token", nil)
|
|
req.Host = "192.168.1.9:18800"
|
|
|
|
if got := h.buildWsURL(req, cfg); got != "ws://192.168.1.9:18800/pico/ws" {
|
|
t.Fatalf("buildWsURL() = %q, want %q", got, "ws://192.168.1.9:18800/pico/ws")
|
|
}
|
|
}
|
|
|
|
func TestGatewayProbeHostUsesLoopbackForWildcardBind(t *testing.T) {
|
|
if got := gatewayProbeHost("0.0.0.0"); got != "127.0.0.1" {
|
|
t.Fatalf("gatewayProbeHost() = %q, want %q", got, "127.0.0.1")
|
|
}
|
|
}
|
|
|
|
func TestBuildWsURLUsesWSSWhenForwardedProtoIsHTTPS(t *testing.T) {
|
|
configPath := filepath.Join(t.TempDir(), "config.json")
|
|
h := NewHandler(configPath)
|
|
|
|
cfg := config.DefaultConfig()
|
|
cfg.Gateway.Host = "0.0.0.0"
|
|
cfg.Gateway.Port = 18790
|
|
|
|
req := httptest.NewRequest("GET", "http://launcher.local/api/pico/token", nil)
|
|
req.Host = "chat.example.com"
|
|
req.Header.Set("X-Forwarded-Proto", "https")
|
|
|
|
if got := h.buildWsURL(req, cfg); got != "wss://chat.example.com:18800/pico/ws" {
|
|
t.Fatalf("buildWsURL() = %q, want %q", got, "wss://chat.example.com:18800/pico/ws")
|
|
}
|
|
}
|
|
|
|
func TestBuildWsURLUsesWSSWhenRequestIsTLS(t *testing.T) {
|
|
configPath := filepath.Join(t.TempDir(), "config.json")
|
|
h := NewHandler(configPath)
|
|
|
|
cfg := config.DefaultConfig()
|
|
cfg.Gateway.Host = "0.0.0.0"
|
|
cfg.Gateway.Port = 18790
|
|
|
|
req := httptest.NewRequest("GET", "https://launcher.local/api/pico/token", nil)
|
|
req.Host = "secure.example.com"
|
|
req.TLS = &tls.ConnectionState{}
|
|
|
|
if got := h.buildWsURL(req, cfg); got != "wss://secure.example.com:18800/pico/ws" {
|
|
t.Fatalf("buildWsURL() = %q, want %q", got, "wss://secure.example.com:18800/pico/ws")
|
|
}
|
|
}
|
|
|
|
func TestBuildWsURLPrefersForwardedHTTPOverTLS(t *testing.T) {
|
|
configPath := filepath.Join(t.TempDir(), "config.json")
|
|
h := NewHandler(configPath)
|
|
|
|
cfg := config.DefaultConfig()
|
|
cfg.Gateway.Host = "0.0.0.0"
|
|
cfg.Gateway.Port = 18790
|
|
|
|
req := httptest.NewRequest("GET", "https://launcher.local/api/pico/token", nil)
|
|
req.Host = "chat.example.com"
|
|
req.TLS = &tls.ConnectionState{}
|
|
req.Header.Set("X-Forwarded-Proto", "http")
|
|
|
|
if got := h.buildWsURL(req, cfg); got != "ws://chat.example.com:18800/pico/ws" {
|
|
t.Fatalf("buildWsURL() = %q, want %q", got, "ws://chat.example.com:18800/pico/ws")
|
|
}
|
|
}
|