-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhost_matcher_test.go
More file actions
162 lines (129 loc) · 3.97 KB
/
Copy pathhost_matcher_test.go
File metadata and controls
162 lines (129 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package groxy
import "testing"
func TestMatchAllHosts(t *testing.T) {
matcher := MatchAllHosts()
for _, host := range []string{"example.com", "example.com:443", ""} {
if !matcher(host) {
t.Fatalf("MatchAllHosts()(%q) = false, want true", host)
}
}
}
func TestMatchHosts_ExactMatch(t *testing.T) {
matcher := MatchHosts("example.com")
if !matcher("example.com") {
t.Fatal("expected exact host to match")
}
if !matcher("EXAMPLE.COM") {
t.Fatal("expected exact host match to be case-insensitive")
}
if matcher("api.example.com") {
t.Fatal("expected different host not to match")
}
}
func TestMatchHosts_StripsPort(t *testing.T) {
matcher := MatchHosts("example.com")
if !matcher("example.com:443") {
t.Fatal("expected host with port to match")
}
}
func TestMatchHosts_WildcardMatch(t *testing.T) {
matcher := MatchHosts("*.example.com")
if !matcher("api.example.com") {
t.Fatal("expected wildcard subdomain to match")
}
if !matcher("v1.api.example.com") {
t.Fatal("expected nested wildcard subdomain to match")
}
if matcher("example.com") {
t.Fatal("expected wildcard not to match root domain")
}
if matcher("example.org") {
t.Fatal("expected different domain not to match")
}
}
func TestMatchHosts_IgnoresEmptyPatterns(t *testing.T) {
matcher := MatchHosts("", " ")
if matcher("example.com") {
t.Fatal("expected empty patterns not to match")
}
}
func TestMatchHosts_IPv6WithPort(t *testing.T) {
matcher := MatchHosts("2001:db8::1")
if !matcher("[2001:db8::1]:443") {
t.Fatal("expected IPv6 host with port to match")
}
}
func TestMatchHostsPrefix_MatchesPrefix(t *testing.T) {
matcher := MatchHostsPrefix("internal-")
for _, host := range []string{"internal-api.example.com", "internal-api.example.com:443", "internal-db"} {
if !matcher(host) {
t.Fatalf("MatchHostsPrefix()(%q) = false, want true", host)
}
}
}
func TestMatchHostsPrefix_CaseInsensitive(t *testing.T) {
matcher := MatchHostsPrefix("INTERNAL-")
if !matcher("internal-api.example.com") {
t.Fatal("expected case-insensitive prefix match")
}
}
func TestMatchHostsPrefix_RejectsDifferentPrefix(t *testing.T) {
matcher := MatchHostsPrefix("internal-")
for _, host := range []string{"api.example.com", "", " "} {
if matcher(host) {
t.Fatalf("MatchHostsPrefix()(%q) = true, want false", host)
}
}
}
func TestMatchHostsSuffix_MatchesSuffix(t *testing.T) {
matcher := MatchHostsSuffix(".internal")
for _, host := range []string{"foo.internal", "bar.internal:443", "v1.api.internal"} {
if !matcher(host) {
t.Fatalf("MatchHostsSuffix()(%q) = false, want true", host)
}
}
}
func TestMatchHostsSuffix_CaseInsensitive(t *testing.T) {
matcher := MatchHostsSuffix(".INTERNAL")
if !matcher("foo.internal") {
t.Fatal("expected case-insensitive suffix match")
}
}
func TestMatchHostsSuffix_RejectsDifferentSuffix(t *testing.T) {
matcher := MatchHostsSuffix(".internal")
for _, host := range []string{"example.com", "", " "} {
if matcher(host) {
t.Fatalf("MatchHostsSuffix()(%q) = true, want false", host)
}
}
}
func TestMatchHostsRegex_MatchesPattern(t *testing.T) {
matcher := MatchHostsRegex(`^api\d+\.example\.com$`)
for _, host := range []string{"api1.example.com", "api42.example.com:443"} {
if !matcher(host) {
t.Fatalf("MatchHostsRegex()(%q) = false, want true", host)
}
}
}
func TestMatchHostsRegex_CaseInsensitive(t *testing.T) {
matcher := MatchHostsRegex(`^api\.example\.com$`)
if !matcher("API.EXAMPLE.COM") {
t.Fatal("expected case-insensitive regex match")
}
}
func TestMatchHostsRegex_RejectsNonMatching(t *testing.T) {
matcher := MatchHostsRegex(`^api\d+\.example\.com$`)
for _, host := range []string{"api.example.com", "example.com", "", " "} {
if matcher(host) {
t.Fatalf("MatchHostsRegex()(%q) = true, want false", host)
}
}
}
func TestMatchHostsRegex_PanicsOnInvalidPattern(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("expected panic for invalid regex pattern")
}
}()
_ = MatchHostsRegex(`[invalid`)
}