-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_extractor_test.go
653 lines (582 loc) · 17.9 KB
/
url_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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
package url_test
import (
"testing"
hqgourl "github.com/hueristiq/hq-go-url"
)
func TestNewExtractor(t *testing.T) {
t.Parallel()
extr := hqgourl.NewExtractor()
if extr == nil {
t.Error("NewExtractor() = nil; want non-nil")
}
}
func TestCompileRegex(t *testing.T) {
t.Parallel()
extr := hqgourl.NewExtractor()
regex := extr.CompileRegex()
if regex == nil {
t.Errorf("CompileRegex() = nil; want non-nil")
}
}
// func TestURLExtraction(t *testing.T) {
// t.Parallel()
// hqgourl := hqgourl.NewExtractor()
// regex := hqgourl.CompileRegex()
// testCases := []struct {
// text string
// want []string
// }{
// {
// `
// Localhost URL: http://localhost
// Localhost URL with Port: http://localhost:8000/home
// Standard URL: https://www.example.com
// URL with Port: http://www.example.com:8080/page
// URL with HTTPS and Query: https://www.example.com/search?q=openai
// URL with Path: https://www.example.com/resources/docs/guide
// URL with Fragment: https://www.example.com/about#team
// URL with User Info: https://user:[email protected]
// Complex URL: https://www.example.com:8080/search?q=openai#results
// International URL: https://www.例子.公司.cn
// URL with IPv4 Address: http://192.168.1.1/setup
// URL with IPv4 and Port: http://192.168.1.1:8080/setup
// URL with IPv6 Address: http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]
// URL with IPv6 and Port: http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080
// URL with Special Characters: https://example.com/products?id=50&category=books
// URL with Encoded Characters: https://example.com/search?name=John%20Doe&age=30
// URL with Multiple Subdomains: https://subdomain.example.co.uk
// File Protocol URL: file:///C:/path/to/file.txt
// Mailto Protocol URL: mailto:[email protected]
// FTP URL with Port: ftp://user:[email protected]:21
// Data URL: data:text/plain;base64,aGVsbG8=
// URL with JavaScript Protocol: javascript:alert('Hello World');
// URL with Spaces: https://www.example.com/test space
// URL in Text: Check out this link: https://www.example.com, it's cool!
// Relative URL: /path/to/resource
// URL without Scheme: www.example.com
// URL without Scheme and with Path: www.example.com/resources
// URL without Scheme and Query: www.example.com/search?q=openai
// URL without Scheme, IPv4: 192.168.1.1
// URL without Scheme, IPv4, and Port: 192.168.1.1:8080
// URL without Scheme, IPv6: [2001:0db8:85a3:0000:0000:8a2e:0370:7334]
// URL without Scheme, IPv6, and Port: [2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080
// `,
// []string{
// "http://localhost",
// "http://localhost:8000/home",
// "https://www.example.com",
// "http://www.example.com:8080/page",
// "https://www.example.com/search?q=openai",
// "https://www.example.com/resources/docs/guide",
// "https://www.example.com/about#team",
// "https://user:[email protected]",
// "https://www.example.com:8080/search?q=openai#results",
// "https://www.例子.公司.cn",
// "http://192.168.1.1/setup",
// "http://192.168.1.1:8080/setup",
// "http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]",
// "http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080",
// "https://example.com/products?id=50&category=books",
// "https://example.com/search?name=John%20Doe&age=30",
// "https://subdomain.example.co.uk",
// "file:///C:/path/to/file.txt",
// "mailto:[email protected]",
// "ftp://user:[email protected]:21",
// "text/plain",
// "https://www.example.com/test",
// "https://www.example.com",
// "/path/to/resource",
// "www.example.com",
// "www.example.com/resources",
// "www.example.com/search?q=openai",
// "192.168.1.1",
// "192.168.1.1:8080",
// "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]",
// "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080",
// },
// },
// }
// for _, tc := range testCases {
// got := regex.FindAllString(tc.text, -1)
// if !equalSlices(got, tc.want) {
// t.Errorf("Extracted URLs = %v, want %v", got, tc.want)
// }
// }
// }
func TestURLExtractionWithScheme(t *testing.T) {
t.Parallel()
extr := hqgourl.NewExtractor(
hqgourl.ExtractorWithScheme(),
)
regex := extr.CompileRegex()
testCases := []struct {
text string
want []string
}{
{
`
http://localhost
http://localhost:8000/home
https://www.example.com
http://www.example.com:8080/page
https://www.example.com/search?q=openai
https://www.example.com/resources/docs/guide
https://www.example.com/about#team
https://user:[email protected]
https://www.example.com:8080/search?q=openai#results
https://www.例子.公司.cn
http://中国.中国/中国
http://中国.中国/foo中国
http://192.168.1.1/setup
http://192.168.1.1:8080/setup
http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]
http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080
https://example.com/products?id=50&category=books
https://example.com/search?name=John%20Doe&age=30
http://foo.com/🐼
https://shmibbles.me/tmp/自殺でも?.png
https://subdomain.example.co.uk
foo://example.com
file:///C:/path/to/file.txt
mailto:[email protected]
ftp://user:[email protected]:21
data:text/plain;base64,aGVsbG8=
javascript:alert('Hello World');
sms:123
xmpp:foo@bar
bitcoin:Addr23?amount=1&message=foo
cid:foo-32x32.v2_fe0f1423.png
postgres://user:[email protected]:5432/path?k=v#f
zoommtg://zoom.us/join?confno=1234&pwd=xxx
https://www.example.com/test space
https://www.example.com, it's cool!
/path/to/resource
www.example.com
www.example.com/resources
www.example.com/search?q=openai
192.168.1.1
192.168.1.1:8080
::1
::ffff:0:0
64:ff9b::
64:ff9b:1::
100::
2001::
2001:1::1
2001:1::2
2001:2::
2001:3::
2001:4:112::
2001:10::
2001:20::
2002::
2620:4f:8000::
fc00::
fe80::
[2001:0db8:85a3:0000:0000:8a2e:0370:7334]
[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080
`,
[]string{
"http://localhost",
"http://localhost:8000/home",
"https://www.example.com",
"http://www.example.com:8080/page",
"https://www.example.com/search?q=openai",
"https://www.example.com/resources/docs/guide",
"https://www.example.com/about#team",
"https://user:[email protected]",
"https://www.example.com:8080/search?q=openai#results",
"https://www.例子.公司.cn",
"http://中国.中国/中国",
"http://中国.中国/foo中国",
// "http://उदाहरण.परीकषा",
"http://192.168.1.1/setup",
"http://192.168.1.1:8080/setup",
"http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]",
"http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080",
"https://example.com/products?id=50&category=books",
"https://example.com/search?name=John%20Doe&age=30",
// "http://✪foo.bar/pa✪th©more",
"http://foo.com/🐼",
"https://shmibbles.me/tmp/自殺でも?.png",
"https://subdomain.example.co.uk",
"foo://example.com",
"file:///C:/path/to/file.txt",
"mailto:[email protected]",
"ftp://user:[email protected]:21",
"sms:123",
"xmpp:foo@bar",
"bitcoin:Addr23?amount=1&message=foo",
"cid:foo-32x32.v2_fe0f1423.png",
"mid:[email protected]",
"postgres://user:[email protected]:5432/path?k=v#f",
"zoommtg://zoom.us/join?confno=1234&pwd=xxx",
"https://www.example.com/test",
"https://www.example.com",
},
},
}
for _, tc := range testCases {
got := regex.FindAllString(tc.text, -1)
if !equalSlices(got, tc.want) {
t.Errorf("Extracted URLs = %v, want %v", got, tc.want)
}
}
}
func TestURLExtractionWithSchemePattern(t *testing.T) {
t.Parallel()
extr := hqgourl.NewExtractor(
hqgourl.ExtractorWithSchemePattern(`(?:https?://)`),
)
regex := extr.CompileRegex()
testCases := []struct {
text string
want []string
}{
{
`
http://localhost
http://localhost:8000/home
https://www.example.com
http://www.example.com:8080/page
https://www.example.com/search?q=openai
https://www.example.com/resources/docs/guide
https://www.example.com/about#team
https://user:[email protected]
https://www.example.com:8080/search?q=openai#results
https://www.例子.公司.cn
http://中国.中国/中国
http://中国.中国/foo中国
http://192.168.1.1/setup
http://192.168.1.1:8080/setup
http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]
http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080
https://example.com/products?id=50&category=books
https://example.com/search?name=John%20Doe&age=30
http://foo.com/🐼
https://shmibbles.me/tmp/自殺でも?.png
https://subdomain.example.co.uk
foo://example.com
file:///C:/path/to/file.txt
mailto:[email protected]
ftp://user:[email protected]:21
data:text/plain;base64,aGVsbG8=
javascript:alert('Hello World');
sms:123
xmpp:foo@bar
bitcoin:Addr23?amount=1&message=foo
cid:foo-32x32.v2_fe0f1423.png
postgres://user:[email protected]:5432/path?k=v#f
zoommtg://zoom.us/join?confno=1234&pwd=xxx
https://www.example.com/test space
https://www.example.com, it's cool!
/path/to/resource
www.example.com
www.example.com/resources
www.example.com/search?q=openai
192.168.1.1
192.168.1.1:8080
::1
::ffff:0:0
64:ff9b::
64:ff9b:1::
100::
2001::
2001:1::1
2001:1::2
2001:2::
2001:3::
2001:4:112::
2001:10::
2001:20::
2002::
2620:4f:8000::
fc00::
fe80::
[2001:0db8:85a3:0000:0000:8a2e:0370:7334]
[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080
`,
[]string{
"http://localhost",
"http://localhost:8000/home",
"https://www.example.com",
"http://www.example.com:8080/page",
"https://www.example.com/search?q=openai",
"https://www.example.com/resources/docs/guide",
"https://www.example.com/about#team",
"https://user:[email protected]",
"https://www.example.com:8080/search?q=openai#results",
"https://www.例子.公司.cn",
"http://中国.中国/中国",
"http://中国.中国/foo中国",
// "http://उदाहरण.परीकषा",
"http://192.168.1.1/setup",
"http://192.168.1.1:8080/setup",
"http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]",
"http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080",
"https://example.com/products?id=50&category=books",
"https://example.com/search?name=John%20Doe&age=30",
// "http://✪foo.bar/pa✪th©more",
"http://foo.com/🐼",
"https://shmibbles.me/tmp/自殺でも?.png",
"https://subdomain.example.co.uk",
"https://www.example.com/test",
"https://www.example.com",
},
},
}
for _, tc := range testCases {
got := regex.FindAllString(tc.text, -1)
if !equalSlices(got, tc.want) {
t.Errorf("Extracted URLs = %v, want %v", got, tc.want)
}
}
}
func TestURLExtractionWithHost(t *testing.T) {
t.Parallel()
extr := hqgourl.NewExtractor(
hqgourl.ExtractorWithHost(),
)
regex := extr.CompileRegex()
testCases := []struct {
text string
want []string
}{
{
`
http://localhost
http://localhost:8000/home
https://www.example.com
http://www.example.com:8080/page
https://www.example.com/search?q=openai
https://www.example.com/resources/docs/guide
https://www.example.com/about#team
https://user:[email protected]
https://www.example.com:8080/search?q=openai#results
https://www.例子.公司.cn
http://中国.中国/中国
http://中国.中国/foo中国
http://उदाहरण.परीकषा
http://192.168.1.1/setup
http://192.168.1.1:8080/setup
http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]
http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080
https://example.com/products?id=50&category=books
https://example.com/search?name=John%20Doe&age=30
http://✪foo.bar/pa✪th©more
http://foo.com/🐼
https://shmibbles.me/tmp/自殺でも?.png
https://subdomain.example.co.uk
foo://example.com
file:///C:/path/to/file.txt
mailto:[email protected]
ftp://user:[email protected]:21
data:text/plain;base64,aGVsbG8=
javascript:alert('Hello World');
sms:123
xmpp:foo@bar
bitcoin:Addr23?amount=1&message=foo
cid:foo-32x32.v2_fe0f1423.png
postgres://user:[email protected]:5432/path?k=v#f
zoommtg://zoom.us/join?confno=1234&pwd=xxx
https://www.example.com/test space
https://www.example.com, it's cool!
/path/to/resource
www.example.com
www.example.com/resources
www.example.com/search?q=openai
192.168.1.1
192.168.1.1:8080
::1
::ffff:0:0
64:ff9b::
64:ff9b:1::
100::
2001::
2001:1::1
2001:1::2
2001:2::
2001:3::
2001:4:112::
2001:10::
2001:20::
2002::
2620:4f:8000::
fc00::
fe80::
[2001:0db8:85a3:0000:0000:8a2e:0370:7334]
[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080
`,
[]string{
"http://localhost",
"http://localhost:8000/home",
"https://www.example.com",
"http://www.example.com:8080/page",
"https://www.example.com/search?q=openai",
"https://www.example.com/resources/docs/guide",
"https://www.example.com/about#team",
"https://user:[email protected]",
"https://www.example.com:8080/search?q=openai#results",
"https://www.例子.公司.cn",
"http://中国.中国/中国",
"http://中国.中国/foo中国",
"http://उदाहरण.परीकषा",
"http://192.168.1.1/setup",
"http://192.168.1.1:8080/setup",
"http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]",
"http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080",
"https://example.com/products?id=50&category=books",
"https://example.com/search?name=John%20Doe&age=30",
"http://✪foo.bar/pa✪th©more",
"http://foo.com/🐼",
"https://shmibbles.me/tmp/自殺でも?.png",
"https://subdomain.example.co.uk",
"foo://example.com",
"file:///C:/path/to/file.txt",
"mailto:[email protected]",
"ftp://user:[email protected]:21",
"sms:123",
"xmpp:foo@bar",
"bitcoin:Addr23?amount=1&message=foo",
"cid:foo-32x32.v2_fe0f1423.png",
"mid:[email protected]",
"postgres://user:[email protected]:5432/path?k=v#f",
"zoommtg://zoom.us/join?confno=1234&pwd=xxx",
"https://www.example.com/test",
"https://www.example.com",
"www.example.com",
"www.example.com/resources",
"www.example.com/search?q=openai",
"192.168.1.1",
"192.168.1.1:8080",
"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]",
"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080",
},
},
}
for _, tc := range testCases {
got := regex.FindAllString(tc.text, -1)
if !equalSlices(got, tc.want) {
t.Errorf("Extracted URLs = %v, want %v", got, tc.want)
}
}
}
func TestURLExtractionWithHostPattern(t *testing.T) {
t.Parallel()
extr := hqgourl.NewExtractor(
hqgourl.ExtractorWithHostPattern(`(?:(?:\w+[.])*example\.com` + hqgourl.ExtractorPortOptionalPattern + `)`),
)
regex := extr.CompileRegex()
testCases := []struct {
text string
want []string
}{
{
`
http://localhost
http://localhost:8000/home
https://www.example.com
http://www.example.com:8080/page
https://www.example.com/search?q=openai
https://www.example.com/resources/docs/guide
https://www.example.com/about#team
https://user:[email protected]
https://www.example.com:8080/search?q=openai#results
https://www.例子.公司.cn
http://中国.中国/中国
http://中国.中国/foo中国
http://उदाहरण.परीकषा
http://192.168.1.1/setup
http://192.168.1.1:8080/setup
http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]
http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080
https://example.com/products?id=50&category=books
https://example.com/search?name=John%20Doe&age=30
http://✪foo.bar/pa✪th©more
http://foo.com/🐼
https://shmibbles.me/tmp/自殺でも?.png
https://subdomain.example.co.uk
foo://example.com
file:///C:/path/to/file.txt
mailto:[email protected]
ftp://user:[email protected]:21
data:text/plain;base64,aGVsbG8=
javascript:alert('Hello World');
sms:123
xmpp:foo@bar
bitcoin:Addr23?amount=1&message=foo
cid:foo-32x32.v2_fe0f1423.png
postgres://user:[email protected]:5432/path?k=v#f
zoommtg://zoom.us/join?confno=1234&pwd=xxx
https://www.example.com/test space
https://www.example.com, it's cool!
/path/to/resource
www.example.com
www.example.com/resources
www.example.com/search?q=openai
192.168.1.1
192.168.1.1:8080
::1
::ffff:0:0
64:ff9b::
64:ff9b:1::
100::
2001::
2001:1::1
2001:1::2
2001:2::
2001:3::
2001:4:112::
2001:10::
2001:20::
2002::
2620:4f:8000::
fc00::
fe80::
[2001:0db8:85a3:0000:0000:8a2e:0370:7334]
[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080
`,
[]string{
"https://www.example.com",
"http://www.example.com:8080/page",
"https://www.example.com/search?q=openai",
"https://www.example.com/resources/docs/guide",
"https://www.example.com/about#team",
"https://user:[email protected]",
"https://www.example.com:8080/search?q=openai#results",
"https://example.com/products?id=50&category=books",
"https://example.com/search?name=John%20Doe&age=30",
"foo://example.com",
"mailto:[email protected]",
"ftp://user:[email protected]:21",
"https://www.example.com/test",
"https://www.example.com",
"www.example.com",
"www.example.com/resources",
"www.example.com/search?q=openai",
},
},
}
for _, tc := range testCases {
got := regex.FindAllString(tc.text, -1)
if !equalSlices(got, tc.want) {
t.Errorf("Extracted URLs = %v, want %v", got, tc.want)
}
}
}
// equalSlices checks if two slices of strings are equal.
func equalSlices(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}