-
Notifications
You must be signed in to change notification settings - Fork 1
/
url_test.go
289 lines (265 loc) · 13.5 KB
/
url_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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package utils
import (
"net/url"
"testing"
"github.com/stretchr/testify/require"
)
var data1 = []string{
"https://www.example.com/", // valid
"https://www.example.com", // missing the last slash
"http://www.example.com", // missing https
"www.example.com/", // missing protocol
"www.example.com", // missing protocol and slash
"www.example.com?one=two", // query string
"www.example.com#one=two", // query string
"www.example.com?one=two&two=http", // query string no protocol
"https://www.example.com?one=two&two=http", // query string two query params, no /
"https://www.example.com/?one=two&two=http", // query string two query params, with /
"http://www.example.com?one=two&two=https://yup.com?he", // query string without / and with weird query param
"http://www.example.com/?one=two&two=https://yup.com?he", // query string with / and with weird query param
"http://www.example.com/?one=two&two=https://yup.com?he",
}
var data2 = []string{
"https://www.example.com/data/", // valid
"https://www.example.com/data", // missing the last slash
"http://www.example.com/data", // missing https
"www.example.com/data/", // missing protocol
"www.example.com/data", // missing protocol and slash
"www.example.com/data?one=two", // query string
"www.example.com/data#one=two", // query string with #
"www.example.com/data?one=two&two=http", // query string no protocol
"https://www.example.com/data?one=two&two=http", // query string two query params, no /
"https://www.example.com/data/?one=two&two=http", // query string two query params, with /
"http://www.example.com/data?one=two&two=https://yup.com?he", // query string without / and with wierd query param
"http://www.example.com/data/?one=two&two=https://yup.com?he", // query string with / and with wierd query param
}
func BenchmarkTruncateUrl(b *testing.B) {
for _, url := range data2 {
u := []byte(url)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
TruncateUrl(u)
}
}
}
func TestTruncateUrlSimple(t *testing.T) {
t.Parallel()
assert := require.New(t)
for _, url := range data1 {
result := TruncateUrl([]byte(url))
assert.Equal([]byte("https://www.example.com/"), result, "URL not matches")
}
}
func TestTruncateUrlDeepLink(t *testing.T) {
t.Parallel()
assert := require.New(t)
for _, url := range data2 {
result := TruncateUrl([]byte(url))
assert.Equal([]byte("https://www.example.com/data/"), result, "URL not matches")
}
}
func TestTruncateUrlSpecialCasesSuccess(t *testing.T) {
// Arrange
t.Parallel()
assert := require.New(t)
data := [][]byte{
GooglePlay,
ITunesApple,
}
// Act
for _, url := range data {
result := TruncateUrl(url)
// Assert
assert.Equal(url, result)
}
}
func BenchmarkTrimUrlForScylla(b *testing.B) {
for _, url := range data2 {
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _, _ = TrimUrlForScylla(url)
}
}
}
func TestTrimUrlForScylla(t *testing.T) {
assert := require.New(t)
tests := []struct {
fullUrl string
wantUrl string
wantHost string
}{
{
fullUrl: "https://www.spanishdict.com/translate/we%20are%20not%20100%25",
wantUrl: "https://www.spanishdict.com/translate/we%20are%20not%20100%25/",
wantHost: "www.spanishdict.com",
},
{
fullUrl: "http://google.com",
wantUrl: "https://google.com/",
wantHost: "google.com",
},
{
fullUrl: " http://google.com ",
wantUrl: "https://google.com/",
wantHost: "google.com",
},
{
fullUrl: "http://google.com/",
wantUrl: "https://google.com/",
wantHost: "google.com",
},
{
fullUrl: "https://www.example.com/path?param=value",
wantUrl: "https://www.example.com/path/",
wantHost: "www.example.com",
},
{
fullUrl: "https://worldtravelling.com/30-stars-we-cant-believe-are-the-same-age/3/?utm_source=Facebook&utm_medium=FB&utm_campaign=DUP GZM_Big4_Vidazoo_CB_Stars The Same Age_P16_RSE - vv6WT WT FB WW An&utm_term=23854019217350509&layout=inf3&vtype=3&fbclid=IwAR3gbeafMqfoDzOPVu2B3P5QEgKtuydi3LmSU4SOft8xa3Akdzo7M0pUtec_aem_th_Aa0DfW8EaIsTtH4kOPKcCwfqRdQUA0TMYlHcRLLLVU1XMA8B43-t-prW7yMcfGw-_MNhLI8vE0TnopF5fjCUJRk4_KDT9WtJ_XXguF0o8qy4Lw",
wantUrl: "https://worldtravelling.com/30-stars-we-cant-believe-are-the-same-age/3/",
wantHost: "worldtravelling.com",
},
{
fullUrl: "https://www.zajenata.bg/напуснах-съпруга-си-заради-това-което-той-искаше-да-наÐfbclid=IwAR1z7R9Na9aasrmffWud0nzSCXwsH8TRa1qswcNFA9XtrM3uKvzAGPYkfMU",
wantUrl: "https://www.zajenata.bg/напуснах-съпруга-си-заради-това-което-той-искаше-да-наÐfbclid=IwAR1z7R9Na9aasrmffWud0nzSCXwsH8TRa1qswcNFA9XtrM3uKvzAGPYkfMU/",
wantHost: "www.zajenata.bg",
},
{
fullUrl: "https://www-lavanguardia-com.cdn.ampproject.org/v/s/www.lavanguardia.com/historiayvida/edad-moderna/20240225/9525175/caravaggio-pintor-homicida-vida-obra-puro-drama.amp.html?amp_gsa=1&_js_v=a9&usqp=mq331AQGsAEggAID#amp_tf=De %1$s&aoh=17090467534993&csi=0&referrer=https://www.google.com&share=https://www.lavanguardia.com/historiayvida/edad-moderna/20240225/9525175/caravaggio-pintor-homicida-vida-obra-puro-drama.html",
wantUrl: "https://www-lavanguardia-com.cdn.ampproject.org/v/s/www.lavanguardia.com/historiayvida/edad-moderna/20240225/9525175/caravaggio-pintor-homicida-vida-obra-puro-drama.amp.html/",
wantHost: "www-lavanguardia-com.cdn.ampproject.org",
},
{
fullUrl: "https://www.football.london/chelsea-fc/news/var-confirm-controversial-moises-caicedo-28701601.amp#amp_tf=From %1$s&aoh=17090476429887&csi=1&referrer=https://www.google.com&share=https://www.football.london/chelsea-fc/news/var-confirm-controversial-moises-caicedo-28701601",
wantUrl: "https://www.football.london/chelsea-fc/news/var-confirm-controversial-moises-caicedo-28701601.amp/",
wantHost: "www.football.london",
},
{
fullUrl: "https://www.proplanta.de/Agrar-Wetter/Saarbr/ufffdcken-7-Tage-Wettervorhersage.html",
wantUrl: "https://www.proplanta.de/Agrar-Wetter/Saarbr/ufffdcken-7-Tage-Wettervorhersage.html/",
wantHost: "www.proplanta.de",
},
{
fullUrl: "https://www.oraridiapertura24.it/filiale/muggi/ufffd-comune%20di%20muggi/ufffd%20(servizi%20demografici)-145894j.html",
wantUrl: "https://www.oraridiapertura24.it/filiale/muggi/ufffd-comune%20di%20muggi/ufffd%20(servizi%20demografici)-145894j.html/",
wantHost: "www.oraridiapertura24.it",
},
{
fullUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล\\ufffd\\ufffd",
wantUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล\\ufffd\\ufffd/",
wantHost: "www.goal.com",
},
{
fullUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล\ufffd\ufffd",
wantUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล\ufffd\ufffd/",
wantHost: "www.goal.com",
},
{
fullUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล\xf0\x8c\xbc",
wantUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล/",
wantHost: "www.goal.com",
},
{
fullUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล\xf8\xa1\xa1\xa1\xa1",
wantUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล/",
wantHost: "www.goal.com",
},
{
fullUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล\ufffd\ufffd",
wantUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล��/",
wantHost: "www.goal.com",
},
{
fullUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล��",
wantUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล\ufffd\ufffd/",
wantHost: "www.goal.com",
},
{
fullUrl: "https://www.goal.com/th/ข่าว/ʘԊ ԋꙨ ꙩんԽ խแดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล",
wantUrl: "https://www.goal.com/th/ข่าว/ʘԊ ԋꙨ ꙩんԽ խแดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล/",
wantHost: "www.goal.com",
},
{
fullUrl: "https://www.oraridiapertura24.it/filiale/muggi/ufffd-comune%20di%20muggi/ufffd%20(servizi%20demografici)-145894j.html",
wantUrl: "https://www.oraridiapertura24.it/filiale/muggi/ufffd-comune%20di%20muggi/ufffd%20(servizi%20demografici)-145894j.html/",
wantHost: "www.oraridiapertura24.it",
},
{
fullUrl: "https://www.deine-tierwelt.de/kleinanzeigen/voegel-c4114/q-rotfl%E3%BCgelsittich/",
wantUrl: "https://www.deine-tierwelt.de/kleinanzeigen/voegel-c4114/q-rotfl%E3%BCgelsittich/",
wantHost: "www.deine-tierwelt.de",
},
{
fullUrl: "https://www.oraridiapertura24.it/filiale/Salerno-Metropolis Caf\\xe8-1834213C.html/",
wantUrl: "https://www.oraridiapertura24.it/filiale/Salerno-Metropolis Caf\\xe8-1834213C.html/",
wantHost: "www.oraridiapertura24.it",
},
{
fullUrl: "https://coolconversion.com/density-volume-mass/--1--m%C2%B3--of--sulphuric-acid-95%-conc.--in--tonne",
wantUrl: "https://coolconversion.com/density-volume-mass/--1--m%C2%B3--of--sulphuric-acid-95%-conc.--in--tonne/",
wantHost: "coolconversion.com",
},
{
fullUrl: "https://www.spanishdict.com/translate/•\\\\tEn 2016 se mudó al Ministerio de Energía donde trabaje hasta el 2022, en estos 6 años se desempeñó en diferentes roles todos enfocados en energía. En su últimos dos años allá trabajó como Económico Senior y gerente en del equipo de analistas aportando al desarrollo de la política para la nueva tecnología de captura de carbono. ",
wantUrl: "https://www.spanishdict.com/translate/•\\\\tEn 2016 se mudó al Ministerio de Energía donde trabaje hasta el 2022, en estos 6 años se desempeñó en diferentes roles todos enfocados en energía. En su últimos dos años allá trabajó como Económico Senior y gerente en del equipo de analistas aportando al desarrollo de la política para la nueva tecnología de captura de carbono./",
wantHost: "www.spanishdict.com",
},
{
fullUrl: "https://www.spanishdict.com/translate/we are not 100%/",
wantUrl: "https://www.spanishdict.com/translate/we are not 100%/",
wantHost: "www.spanishdict.com",
},
{
fullUrl: "https://www.gala.de/amp/lifestyle/film-tv-musik/joko-winterscheidt--er-holt-sich-seine-show-zurueck-24032084.html&_tf=Von %1$s&aoh=17095534826758&csi=1&referrer=https://www.google.com&share=https://www.gala.de/lifestyle/film-tv-musik/joko-winterscheidt-siegt-gegen-klaas-und-holt-sich-seine-show-zurueck-24032084.html",
wantUrl: "https://www.gala.de/amp/lifestyle/film-tv-musik/joko-winterscheidt--er-holt-sich-seine-show-zurueck-24032084.html&_tf=Von %1$s&aoh=17095534826758&csi=1&referrer=https://www.google.com&share=https://www.gala.de/lifestyle/film-tv-musik/joko-winterscheidt-siegt-gegen-klaas-und-holt-sich-seine-show-zurueck-24032084.html/",
wantHost: "www.gala.de",
},
{
fullUrl: "www.gala.de",
wantUrl: "https://www.gala.de/",
wantHost: "www.gala.de",
},
}
for _, tt := range tests {
t.Run(tt.fullUrl, func(_ *testing.T) {
resultUrl, resultHost, err := TrimUrlForScylla(tt.fullUrl)
assert.NoError(err)
assert.Equal(tt.wantUrl, resultUrl)
assert.Equal(tt.wantHost, resultHost)
// to do check if double parse is changing anything
resultUrl, resultHost, err = TrimUrlForScylla(tt.fullUrl)
assert.NoError(err)
assert.Equal(tt.wantUrl, resultUrl)
assert.Equal(tt.wantHost, resultHost)
resultUrl, resultHost, err = TrimUrlForScylla(resultUrl)
assert.NoError(err)
assert.Equal(tt.wantUrl, resultUrl)
assert.Equal(tt.wantHost, resultHost)
})
}
}
func TestGetDomainFromUrl(t *testing.T) {
assert := require.New(t)
testCases := []struct {
fullUrl string
expectedDomain string
expectedErr error
}{
{"https://www.google.com", "www.google.com", nil},
{"https://www.example.com/path?param=value", "www.example.com", nil},
}
for _, tc := range testCases {
domain, err := GetDomainFromUrl(tc.fullUrl)
assert.Equal(tc.expectedDomain, domain)
assert.Equal(tc.expectedErr, err)
}
}
func TestUrl(t *testing.T) {
u := "https://www.the-crossword-solver.com/word/___+major+%28%22the+great+bear%22+constellation%29/"
parsed, _ := url.Parse(u)
parsed.Host = "the-crossword-solver.com"
parsed.RawPath = "/word/___+major+(\"the+great+bear\"+constellation)"
// + %20
require.Equal(t, "https://the-crossword-solver.com/word/___+major+%28%22the+great+bear%22+constellation%29/", parsed.String())
}