Files
cdp_hub/internal/domain/browser_test.go
T

66 lines
2.3 KiB
Go

package domain
import (
"errors"
"path/filepath"
"testing"
)
func TestLaunchSpecNormalizeCanonicalizesSafeFields(t *testing.T) {
profile := filepath.Join(t.TempDir(), "profile")
normalized, err := (LaunchSpec{
Kind: BrowserChrome,
Executable: " chrome.exe ",
UserDataDir: profile + string(filepath.Separator),
ProfileDirectory: " Default ",
TargetURL: " https://example.com/path ",
ExtraArgs: []string{"--no-first-run"},
}).Normalize()
if err != nil {
t.Fatalf("Normalize() error = %v", err)
}
if normalized.Executable != "chrome.exe" || normalized.UserDataDir != filepath.Clean(profile) || normalized.ProfileDirectory != "Default" || normalized.TargetURL != "https://example.com/path" {
t.Fatalf("normalized spec = %#v", normalized)
}
normalized.ExtraArgs[0] = "changed"
if normalized.ExtraArgs[0] == "" {
t.Fatal("extra args unexpectedly empty")
}
}
func TestLaunchSpecNormalizeRejectsUnsafeValues(t *testing.T) {
base := LaunchSpec{Kind: BrowserEdge, UserDataDir: filepath.Join(t.TempDir(), "profile"), TargetURL: "https://example.com"}
cases := []LaunchSpec{
{UserDataDir: base.UserDataDir, TargetURL: base.TargetURL},
{Kind: base.Kind, UserDataDir: "relative", TargetURL: base.TargetURL},
{Kind: base.Kind, UserDataDir: base.UserDataDir, TargetURL: "file:///tmp/test"},
{Kind: base.Kind, UserDataDir: base.UserDataDir, TargetURL: "https://user@example.com"},
}
for i, spec := range cases {
if _, err := spec.Normalize(); !errors.Is(err, ErrInvalidLaunchSpec) {
t.Errorf("case %d error = %v, want ErrInvalidLaunchSpec", i, err)
}
}
}
func TestLaunchSpecNormalizeAllowsAnEmptyTargetURL(t *testing.T) {
profile := filepath.Join(t.TempDir(), "profile")
normalized, err := (LaunchSpec{Kind: BrowserChrome, UserDataDir: profile}).Normalize()
if err != nil {
t.Fatal(err)
}
if normalized.TargetURL != "" {
t.Fatalf("target URL = %q, want empty", normalized.TargetURL)
}
}
func TestLaunchSpecNormalizeRejectsInvalidRemoteDebugPort(t *testing.T) {
profile := filepath.Join(t.TempDir(), "profile")
for _, port := range []int{1, MinRemoteDebugPort - 1, MaxRemoteDebugPort + 1} {
_, err := (LaunchSpec{Kind: BrowserChrome, UserDataDir: profile, RemoteDebugPort: port}).Normalize()
if !errors.Is(err, ErrInvalidLaunchSpec) {
t.Errorf("port %d error = %v", port, err)
}
}
}