-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomain_extractor_test.go
225 lines (182 loc) · 5.78 KB
/
domain_extractor_test.go
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package url_test
import (
"testing"
hqgourl "github.com/hueristiq/hq-go-url"
"github.com/hueristiq/hq-go-url/tlds"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDomainExtractor_CompileRegex_Default(t *testing.T) {
t.Parallel()
// Initialize DomainExtractor with default settings.
extractor := hqgourl.NewDomainExtractor()
// Compile the regex.
regex := extractor.CompileRegex()
// Ensure the regex is not nil.
require.NotNil(t, regex)
// Test that the regex matches valid domain patterns.
tests := []struct {
input string
expected bool
}{
{"example.com", true},
{"www.example.com", true},
{"http://www.example.com", true},
{"localhost", true},
{"http://localhost", true},
{"example.localhost", true},
{"http://example.localhost", true},
{"xn--example-q9a.com", true}, // IDN with punycode.
{"example.co.uk", true},
{"invalid_domain", false},
{"just_text", false},
{"http://192.168.0.1/path", false},
{"https://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:17000", false},
{"ftp://example.com", true},
}
for _, tt := range tests {
assert.Equalf(t, tt.expected, regex.MatchString(tt.input), "failed on input: %s", tt.input)
}
}
func TestDomainExtractor_CompileRegex_CustomRootDomainPattern(t *testing.T) {
t.Parallel()
// Initialize DomainExtractor with a custom root domain pattern.
extractor := hqgourl.NewDomainExtractor(
hqgourl.DomainExtractorWithRootDomainPattern(`(?:example|rootdomain)`), // Custom root domain pattern
)
// Compile the regex.
regex := extractor.CompileRegex()
// Ensure the regex is not nil.
require.NotNil(t, regex)
// Test cases for custom root domain pattern.
tests := []struct {
input string
expected bool
}{
{"rootdomain.com", true},
{"my-root-domain.org", false},
{"not_valid_domain", false},
{"example.com", true},
{"www.example.com", true},
{"localhost", false},
}
for _, tt := range tests {
assert.Equalf(t, tt.expected, regex.MatchString(tt.input), "failed on input: %s", tt.input)
}
}
func TestDomainExtractor_CompileRegex_CustomTLDPattern(t *testing.T) {
t.Parallel()
// Initialize DomainExtractor with a custom TLD pattern.
extractor := hqgourl.NewDomainExtractor(
hqgourl.DomainExtractorWithTLDPattern(`(?:com|net|org)`), // Custom TLD pattern
)
// Compile the regex.
regex := extractor.CompileRegex()
// Ensure the regex is not nil.
require.NotNil(t, regex)
// Test cases for custom TLD pattern.
tests := []struct {
input string
expected bool
}{
{"example.com", true},
{"example.org", true},
{"example.net", true},
{"example.co.uk", false},
{"localhost", false},
}
for _, tt := range tests {
assert.Equalf(t, tt.expected, regex.MatchString(tt.input), "failed on input: %s", tt.input)
}
}
func TestDomainExtractor_CompileRegex_CustomRootDomainAndTLDPattern(t *testing.T) {
t.Parallel()
// Initialize DomainExtractor with custom root domain and TLD patterns.
extractor := hqgourl.NewDomainExtractor(
hqgourl.DomainExtractorWithRootDomainPattern(`[a-zA-Z0-9-]+`),
hqgourl.DomainExtractorWithTLDPattern(`(?:com|net)`),
)
// Compile the regex.
regex := extractor.CompileRegex()
// Ensure the regex is not nil.
require.NotNil(t, regex)
// Test cases for custom root domain and TLD pattern.
tests := []struct {
input string
expected bool
}{
{"example.com", true},
{"example.net", true},
{"example.org", false}, // TLD pattern restricts to com/net.
{"localhost", false},
{"subdomain.example.com", true},
}
for _, tt := range tests {
assert.Equalf(t, tt.expected, regex.MatchString(tt.input), "failed on input: %s", tt.input)
}
}
func TestDomainExtractor_CompileRegex_TLDSeparation(t *testing.T) {
t.Parallel()
// Simulate a scenario where the TLDs include both ASCII and Unicode values.
originalTLDs := tlds.Official
tlds.Official = []string{"com", "org", "xn--unicode", "测试"}
// Initialize the DomainExtractor.
extractor := hqgourl.NewDomainExtractor()
// Compile the regex.
regex := extractor.CompileRegex()
// Restore the original TLD list.
t.Cleanup(func() { tlds.Official = originalTLDs })
// Ensure the regex is not nil.
require.NotNil(t, regex)
// Test cases for ASCII and Unicode TLDs.
tests := []struct {
input string
expected bool
}{
{"example.com", true},
{"example.org", true},
{"example.测试", true}, // Unicode TLD.
{"example.xn--unicode", true}, // Punycode.
{"example.co.uk", false}, // TLD not in the list.
{"localhost", true},
}
for _, tt := range tests {
assert.Equalf(t, tt.expected, regex.MatchString(tt.input), "failed on input: %s", tt.input)
}
}
func TestDomainExtractor_CustomPatterns_Failures(t *testing.T) {
t.Parallel()
// Test with invalid root domain and TLD patterns.
extractor := hqgourl.NewDomainExtractor(
hqgourl.DomainExtractorWithRootDomainPattern(`[invalid`), // Invalid regex pattern
hqgourl.DomainExtractorWithTLDPattern(`(`), // Invalid regex pattern
)
// Expecting a panic due to invalid regex patterns.
assert.Panics(t, func() {
extractor.CompileRegex()
}, "Expected panic with invalid regex patterns")
}
func TestDomainExtractor_CustomPatterns_Empty(t *testing.T) {
t.Parallel()
// Test with empty custom root domain and TLD patterns.
extractor := hqgourl.NewDomainExtractor(
hqgourl.DomainExtractorWithRootDomainPattern(""),
hqgourl.DomainExtractorWithTLDPattern(""),
)
// Compile the regex.
regex := extractor.CompileRegex()
// Ensure the regex is not nil.
require.NotNil(t, regex)
// The regex should fall back to default behavior.
tests := []struct {
input string
expected bool
}{
{"example.com", true},
{"localhost", true},
{"invalid_domain", false},
}
for _, tt := range tests {
assert.Equalf(t, tt.expected, regex.MatchString(tt.input), "failed on input: %s", tt.input)
}
}