返回 DeepSeek-Reasonix
forward_test.go
根目录 / internal / remote / forward / forward_test.go
1 package forward
2
3 import "testing"
4
5 func TestParseShorthand(t *testing.T) {
6 cases := []struct {
7 in string
8 bind, targ string
9 wantErr bool
10 }{
11 {"8080", "127.0.0.1:8080", "127.0.0.1:8080", false},
12 {"8080:example.com:80", "127.0.0.1:8080", "example.com:80", false},
13 {"127.0.0.1:8080:db:5432", "127.0.0.1:8080", "db:5432", false},
14 {"0.0.0.0:9000:svc:9000", "0.0.0.0:9000", "svc:9000", false},
15 {":8080:svc:80", "127.0.0.1:8080", "svc:80", false},
16 {"8080:svc:", "", "", true},
17 {"8080::80", "", "", true},
18 {"0", "", "", true},
19 {"8080:svc:0", "", "", true},
20 {"", "", "", true},
21 {"a:b:c:d:e", "", "", true},
22 }
23 for _, c := range cases {
24 s, err := ParseShorthand(Local, c.in)
25 if c.wantErr {
26 if err == nil {
27 t.Errorf("ParseShorthand(%q): expected error", c.in)
28 }
29 continue
30 }
31 if err != nil {
32 t.Errorf("ParseShorthand(%q): %v", c.in, err)
33 continue
34 }
35 if s.BindAddr != c.bind || s.TargetAddr != c.targ {
36 t.Errorf("ParseShorthand(%q) = bind %q target %q, want %q / %q", c.in, s.BindAddr, s.TargetAddr, c.bind, c.targ)
37 }
38 }
39 }
40
41 func TestSpecValidate(t *testing.T) {
42 valid := Spec{Direction: Local, BindAddr: "127.0.0.1:0", TargetAddr: "svc:80"}
43 if err := valid.Validate(); err != nil {
44 t.Fatalf("valid spec rejected: %v", err)
45 }
46 for _, spec := range []Spec{
47 {Direction: Local, BindAddr: "127.0.0.1", TargetAddr: "svc:80"},
48 {Direction: Local, BindAddr: "127.0.0.1:8000", TargetAddr: "svc"},
49 {Direction: Local, BindAddr: "127.0.0.1:8000", TargetAddr: "svc:0"},
50 {Direction: Direction(99), BindAddr: "127.0.0.1:8000", TargetAddr: "svc:80"},
51 } {
52 if err := spec.Validate(); err == nil {
53 t.Errorf("invalid spec accepted: %+v", spec)
54 }
55 }
56 }
57
58 func TestNonLoopbackBind(t *testing.T) {
59 loop, _ := ParseShorthand(Local, "8080")
60 if loop.NonLoopbackBind() {
61 t.Error("127.0.0.1 flagged as non-loopback")
62 }
63 open, _ := ParseShorthand(Local, "0.0.0.0:8080:svc:80")
64 if !open.NonLoopbackBind() {
65 t.Error("0.0.0.0 not flagged as non-loopback")
66 }
67 }
68
69 func TestParseDirection(t *testing.T) {
70 for _, s := range []string{"local", "-L", "l"} {
71 if d, err := ParseDirection(s); err != nil || d != Local {
72 t.Errorf("ParseDirection(%q) = %v, %v", s, d, err)
73 }
74 }
75 for _, s := range []string{"remote", "-R", "R"} {
76 if d, err := ParseDirection(s); err != nil || d != Remote {
77 t.Errorf("ParseDirection(%q) = %v, %v", s, d, err)
78 }
79 }
80 if _, err := ParseDirection("dynamic"); err == nil {
81 t.Error("dynamic direction accepted")
82 }
83 }
84
85 func TestDefaultName(t *testing.T) {
86 s := Spec{Direction: Local, BindAddr: "127.0.0.1:8080", TargetAddr: "svc:80"}
87 if got := s.DefaultName(); got != "L:127.0.0.1:8080->svc:80" {
88 t.Errorf("DefaultName = %q", got)
89 }
90 named := Spec{Name: "web", Direction: Remote}
91 if got := named.DefaultName(); got != "web" {
92 t.Errorf("DefaultName with name = %q", got)
93 }
94 }
95
95 lines GO