-
Notifications
You must be signed in to change notification settings - Fork 0
/
domain_parser_test.go
120 lines (83 loc) · 2.83 KB
/
domain_parser_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
package url_test
import (
"testing"
hqgourl "github.com/hueristiq/hq-go-url"
"github.com/stretchr/testify/assert"
)
// Test parsing of a valid domain with subdomain, SLD, and TLD.
func TestDomainParser_Parse_ValidDomain(t *testing.T) {
t.Parallel()
domain := "www.example.com"
parser := hqgourl.NewDomainParser()
parsed := parser.Parse(domain)
assert.NotNil(t, parsed)
assert.Equal(t, "www", parsed.Subdomain)
assert.Equal(t, "example", parsed.SLD)
assert.Equal(t, "com", parsed.TLD)
}
// Test parsing of a domain without subdomain.
func TestDomainParser_Parse_DomainWithoutSubdomain(t *testing.T) {
t.Parallel()
domain := "example.com"
parser := hqgourl.NewDomainParser()
parsed := parser.Parse(domain)
assert.NotNil(t, parsed)
assert.Equal(t, "", parsed.Subdomain) // No subdomain.
assert.Equal(t, "example", parsed.SLD)
assert.Equal(t, "com", parsed.TLD)
}
// Test parsing of a domain without a valid TLD.
func TestDomainParser_Parse_InvalidTLD(t *testing.T) {
t.Parallel()
domain := "example.invalidtld"
parser := hqgourl.NewDomainParser()
parsed := parser.Parse(domain)
assert.NotNil(t, parsed)
assert.Equal(t, "", parsed.Subdomain) // No subdomain.
assert.Equal(t, "example.invalidtld", parsed.SLD) // Treat the whole domain as SLD.
assert.Equal(t, "", parsed.TLD)
}
// Test parsing of a domain with a pseudo-TLD.
func TestDomainParser_Parse_PseudoTLD(t *testing.T) {
t.Parallel()
domain := "example.local"
parser := hqgourl.NewDomainParser()
parsed := parser.Parse(domain)
assert.NotNil(t, parsed)
assert.Equal(t, "", parsed.Subdomain)
assert.Equal(t, "example", parsed.SLD)
assert.Equal(t, "local", parsed.TLD) // Recognized pseudo-TLD.
}
// Test parsing of a single-word domain (no TLD or subdomain).
func TestDomainParser_Parse_SingleWordDomain(t *testing.T) {
t.Parallel()
domain := "localhost"
parser := hqgourl.NewDomainParser()
parsed := parser.Parse(domain)
assert.NotNil(t, parsed)
assert.Equal(t, "", parsed.Subdomain)
assert.Equal(t, "localhost", parsed.SLD) // No TLD, treated as SLD.
assert.Equal(t, "", parsed.TLD)
}
// Test parsing with custom TLDs.
func TestDomainParserWithCustomTLDs(t *testing.T) {
t.Parallel()
domain := "example.custom"
parser := hqgourl.NewDomainParser(hqgourl.DomainParserWithTLDs("custom"))
parsed := parser.Parse(domain)
assert.NotNil(t, parsed)
assert.Equal(t, "", parsed.Subdomain)
assert.Equal(t, "example", parsed.SLD)
assert.Equal(t, "custom", parsed.TLD) // Recognizes custom TLD.
}
// Test parsing an empty domain string.
func TestDomainParser_Parse_EmptyString(t *testing.T) {
t.Parallel()
domain := ""
parser := hqgourl.NewDomainParser()
parsed := parser.Parse(domain)
assert.NotNil(t, parsed)
assert.Equal(t, "", parsed.Subdomain)
assert.Equal(t, "", parsed.SLD) // No SLD for an empty domain.
assert.Equal(t, "", parsed.TLD)
}