-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdomain_test.go
More file actions
177 lines (163 loc) · 3.95 KB
/
Copy pathdomain_test.go
File metadata and controls
177 lines (163 loc) · 3.95 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//nolint:testpackage // need access to internal functions
package emailscraper
import (
"testing"
)
//nolint:funlen // table-driven test with many cases
func TestPrepareAllowedDomain(t *testing.T) {
t.Parallel()
tests := []struct {
name string
url string
wantDomains []string
wantErr bool
}{
{
name: "simple domain",
url: "example.com",
wantDomains: []string{"example.com", "www.example.com"},
wantErr: false,
},
{
name: "with www prefix",
url: "www.example.com",
wantDomains: []string{"example.com", "www.example.com"},
wantErr: false,
},
{
name: "with https protocol",
url: "https://example.com",
wantDomains: []string{"example.com", "www.example.com"},
wantErr: false,
},
{
name: "with http protocol",
url: "http://example.com",
wantDomains: []string{"example.com", "www.example.com"},
wantErr: false,
},
{
name: "with path",
url: "example.com/path/to/page",
wantDomains: []string{"example.com", "www.example.com"},
wantErr: false,
},
{
name: "with https and path",
url: "https://example.com/contact",
wantDomains: []string{"example.com", "www.example.com"},
wantErr: false,
},
{
name: "with www and path",
url: "www.example.com/about",
wantDomains: []string{"example.com", "www.example.com"},
wantErr: false,
},
{
name: "subdomain preserved",
url: "blog.example.com",
wantDomains: []string{"blog.example.com", "www.blog.example.com"},
wantErr: false,
},
{
name: "with query string",
url: "example.com?foo=bar",
wantDomains: []string{"example.com", "www.example.com"},
wantErr: false,
},
{
name: "with port number",
url: "example.com:8080",
wantDomains: []string{"example.com", "www.example.com"},
wantErr: false,
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
got, err := prepareAllowedDomain(testCase.url)
if (err != nil) != testCase.wantErr {
t.Errorf("prepareAllowedDomain(%q) error = %v, wantErr %v", testCase.url, err, testCase.wantErr)
return
}
if !testCase.wantErr && !equalStringSlices(got, testCase.wantDomains) {
t.Errorf("prepareAllowedDomain(%q) = %v, want %v", testCase.url, got, testCase.wantDomains)
}
})
}
}
//nolint:funlen // table-driven test with many cases
func TestTrimProtocol(t *testing.T) {
t.Parallel()
tests := []struct {
name string
url string
expected string
}{
{
name: "https protocol",
url: "https://example.com",
expected: "example.com",
},
{
name: "http protocol",
url: "http://example.com",
expected: "example.com",
},
{
name: "no protocol",
url: "example.com",
expected: "example.com",
},
{
name: "https with path",
url: "https://example.com/path",
expected: "example.com/path",
},
{
name: "http with path",
url: "http://example.com/path/to/page",
expected: "example.com/path/to/page",
},
{
name: "https with query",
url: "https://example.com?foo=bar",
expected: "example.com?foo=bar",
},
{
name: "empty string",
url: "",
expected: "",
},
{
name: "only https",
url: "https://",
expected: "",
},
{
name: "only http",
url: "http://",
expected: "",
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
if got := trimProtocol(testCase.url); got != testCase.expected {
t.Errorf("trimProtocol(%q) = %q, want %q", testCase.url, got, testCase.expected)
}
})
}
}
func equalStringSlices(first, second []string) bool {
if len(first) != len(second) {
return false
}
for idx := range first {
if first[idx] != second[idx] {
return false
}
}
return true
}