-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlds_test.go
113 lines (108 loc) · 2.1 KB
/
tlds_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
package puri
import (
"net/url"
"testing"
)
func TestHasTLDInUrl(t *testing.T) {
tests := []struct {
name string
uri string
wantTLD *string
wantErr bool
}{
{
name: "Has .COM TLD",
uri: "http://example.com",
wantTLD: p(".COM"),
wantErr: false,
},
{
name: "Has .DE TLD",
uri: "http://example.de/blahblah?k=v",
wantTLD: p(".DE"),
wantErr: false,
},
{
name: "Has no TLD",
uri: "http://example",
wantTLD: nil,
wantErr: true,
},
{
name: "Has .ORG TLD in path",
uri: "http://example.org/blah",
wantTLD: p(".ORG"),
wantErr: false,
},
// Add more test cases as needed
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
uri, _ := url.Parse(tt.uri)
got, err := NewTLDS().hasTLDInUrl(*uri)
if tt.wantErr {
if err == nil {
t.Error("want error but got nil")
}
} else {
if err != nil {
t.Errorf("don't want error but got: %v", err)
}
if *tt.wantTLD != *got {
t.Errorf("tld doesn't match, want: %s, got: %s", *tt.wantTLD, *got)
}
}
})
}
}
func TestHasTLDInString(t *testing.T) {
tests := []struct {
name string
uri string
wantTLD *string
wantErr bool
}{
{
name: "Has .COM TLD",
uri: "http://example.com",
wantTLD: p(".COM"),
wantErr: false,
},
{
name: "Has .DE TLD",
uri: "http://example.de/blahblah?k=v",
wantTLD: p(".DE"),
wantErr: false,
},
{
name: "Has no TLD",
uri: "http://example",
wantTLD: nil,
wantErr: true,
},
{
name: "Has .ORG TLD in path",
uri: "http://example.org/blah",
wantTLD: p(".ORG"),
wantErr: false,
},
// Add more test cases as needed
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := IANA.hasTLDInString(tt.uri)
if tt.wantErr {
if err == nil {
t.Error("want error but got nil")
}
} else {
if err != nil {
t.Errorf("don't want error but got: %v", err)
}
if *tt.wantTLD != *got {
t.Errorf("tld doesn't match, want: %s, got: %s", *tt.wantTLD, *got)
}
}
})
}
}