-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sanitize_test.go
1030 lines (895 loc) · 27 KB
/
sanitize_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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package sanitize
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestAlpha tests the alpha sanitize method
func TestAlpha(t *testing.T) {
t.Parallel()
var tests = []struct {
name string
input string
expected string
typeCase bool
}{
{"regular string", "Test This String-!123", "TestThisString", false},
{"various symbols", `~!@#$%^&*()-_Symbols=+[{]};:'"<>,./?`, "Symbols", false},
{"carriage returns", "\nThis\nThat", "ThisThat", false},
{"quotes and ticks", "“This is a quote with tick`s … ” ☺ ", "Thisisaquotewithticks", false},
{"spaces", "Test This String-!123", "Test This String", true},
{"symbols and spaces", `~!@#$%^&*()-_Symbols=+[{]};:'"<>,./?`, "Symbols", true},
{"quotes and spaces", "“This is a quote with tick`s … ” ☺ ", "This is a quote with ticks ", true},
{"carriage returns", "\nThis\nThat", `
This
That`, true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := Alpha(test.input, test.typeCase)
assert.Equal(t, test.expected, output)
})
}
}
// BenchmarkAlphaNoSpaces benchmarks the Alpha method
func BenchmarkAlpha(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Alpha("This is the test string.", false)
}
}
// BenchmarkAlpha_WithSpaces benchmarks the Alpha method
func BenchmarkAlpha_WithSpaces(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Alpha("This is the test string.", true)
}
}
// ExampleAlpha example using Alpha() and no spaces flag
func ExampleAlpha() {
fmt.Println(Alpha("Example String!", false))
// Output: ExampleString
}
// ExampleAlpha_withSpaces example using Alpha with spaces flag
func ExampleAlpha_withSpaces() {
fmt.Println(Alpha("Example String!", true))
// Output: Example String
}
// TestAlphaNumeric tests the alphanumeric sanitize method
func TestAlphaNumeric(t *testing.T) {
t.Parallel()
var tests = []struct {
name string
input string
expected string
typeCase bool
}{
{"regular string", "Test This String-!123", "TestThisString123", false},
{"symbols", `~!@#$%^&*()-_Symbols=+[{]};:'"<>,./?`, "Symbols", false},
{"carriage returns", "\nThis1\nThat2", "This1That2", false},
{"quotes and ticks", "“This is a quote with tick`s … ” ☺ 342", "Thisisaquotewithticks342", false},
{"string with spaces", "Test This String-! 123", "Test This String 123", true},
{"symbols and spaces", `~!@#$%^&*()-_Symbols 123=+[{]};:'"<>,./?`, "Symbols 123", true},
{"ticks and spaces", "“This is a quote with tick`s…”☺ 123", "This is a quote with ticks 123", true},
{"carriage return and spaces", "\nThis1\nThat2", `
This1
That2`, true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := AlphaNumeric(test.input, test.typeCase)
assert.Equal(t, test.expected, output)
})
}
}
// BenchmarkAlphaNumeric benchmarks the AlphaNumeric method
func BenchmarkAlphaNumeric(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = AlphaNumeric("This is the test string 12345.", false)
}
}
// BenchmarkAlphaNumeric_WithSpaces benchmarks the AlphaNumeric method
func BenchmarkAlphaNumeric_WithSpaces(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = AlphaNumeric("This is the test string 12345.", true)
}
}
// ExampleAlphaNumeric example using AlphaNumeric() with no spaces
func ExampleAlphaNumeric() {
fmt.Println(AlphaNumeric("Example String 2!", false))
// Output: ExampleString2
}
// ExampleAlphaNumeric_withSpaces example using AlphaNumeric() with spaces
func ExampleAlphaNumeric_withSpaces() {
fmt.Println(AlphaNumeric("Example String 2!", true))
// Output: Example String 2
}
// TestBitcoinAddress will test all permutations
func TestBitcoinAddress(t *testing.T) {
t.Parallel()
var tests = []struct {
name string
input string
expected string
}{
{"remove symbol", ":1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs", "1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs"},
{"remove spaces", " 1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs ", "1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs"},
{"remove spaces 2", " 1K6c7 LGpdB 8LwoGNVfG5 1dRV 9UUEijbrWs ", "1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs"},
{"remove symbols 2", "$#:1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs!", "1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs"},
{"remove symbols 3", "$#:1K6c_7LGpd^B8Lw_oGN=VfG+51_dRV9-UUEijbrWs!", "1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs"},
// No uppercase letter O, uppercase letter I, lowercase letter l, and the number 0
{"uppercase letters", "OIl01K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs!", "1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := BitcoinAddress(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// BenchmarkBitcoinAddress benchmarks the BitcoinAddress method
func BenchmarkBitcoinAddress(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = BitcoinAddress("1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs")
}
}
// ExampleBitcoinAddress example using BitcoinAddress()
func ExampleBitcoinAddress() {
fmt.Println(BitcoinAddress(":1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs!"))
// Output: 1K6c7LGpdB8LwoGNVfG51dRV9UUEijbrWs
}
// TestBitcoinCashAddress will test all permutations of using BitcoinCashAddress()
func TestBitcoinCashAddress(t *testing.T) {
t.Parallel()
var tests = []struct {
name string
input string
expected string
}{
{"remove symbols", "$#:qze7yy2au5vuznvn8lzj5y0j5t066vhs75e3m0eptz!", "qze7yy2au5vuznvn8lzj5y0j5t066vhs75e3m0eptz"},
{"remove spaces", " $#:qze7yy2 au5vuznvn8lzj5y0j5t066 vhs75e3m0eptz! ", "qze7yy2au5vuznvn8lzj5y0j5t066vhs75e3m0eptz"},
// No letters o, b, i, or number 1
{"remove ignored characters", "pqbq3728yw0y47sOqn6l2na30mcw6zm78idzq5ucqzc371", "pqq3728yw0y47sqn6l2na30mcw6zm78dzq5ucqzc37"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := BitcoinCashAddress(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// BenchmarkBitcoinCashAddress benchmarks the BitcoinCashAddress() method
func BenchmarkBitcoinCashAddress(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = BitcoinCashAddress("qze7yy2au5vuznvn8lzj5y0j5t066vhs75e3m0eptz")
}
}
// ExampleBitcoinCashAddress example using BitcoinCashAddress() `cashaddr`
func ExampleBitcoinCashAddress() {
fmt.Println(BitcoinAddress("qze7yy2au5vuznvn8lzj5y0j5t066vhs75e3m0eptz!"))
// Output: qze7yy2au5vuznvn8zj5yj5t66vhs75e3meptz
}
// TestCustom tests the custom sanitize method
func TestCustom(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expected string
regex string
}{
{"ThisWorks123!", "ThisWorks123", `[^a-zA-Z0-9]`},
{"ThisWorks1.23!", "1.23", `[^0-9.-]`},
{"ThisWorks1.23!", "ThisWorks123", `[^0-9a-zA-Z]`},
}
for _, test := range tests {
output := Custom(test.input, test.regex)
assert.Equal(t, test.expected, output)
}
}
// BenchmarkCustom benchmarks the Custom method
func BenchmarkCustom(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Custom("This is the test string 12345.", `[^a-zA-Z0-9]`)
}
}
// ExampleCustom example using Custom() using an alpha regex
func ExampleCustom() {
fmt.Println(Custom("Example String 2!", `[^a-zA-Z]`))
// Output: ExampleString
}
// ExampleCustom_numeric example using Custom() using a numeric regex
func ExampleCustom_numeric() {
fmt.Println(Custom("Example String 2!", `[^0-9]`))
// Output: 2
}
// TestDecimal tests the decimal sanitize method
func TestDecimal(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expected string
}{
{" String: 1.23 ", "1.23"},
{" String: 001.2300 ", "001.2300"},
{" $-1.034234 Price", "-1.034234"},
{" $-1%.034234e Price", "-1.034234"},
{"/n<< $-1.034234 >>/n", "-1.034234"},
}
for _, test := range tests {
output := Decimal(test.input)
assert.Equal(t, test.expected, output)
}
}
// BenchmarkDecimal benchmarks the Decimal method
func BenchmarkDecimal(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Decimal("String: -123.12345")
}
}
// ExampleDecimal example using Decimal() for a positive number
func ExampleDecimal() {
fmt.Println(Decimal("$ 99.99!"))
// Output: 99.99
}
// ExampleDecimal_negative example using Decimal() for a negative number
func ExampleDecimal_negative() {
fmt.Println(Decimal("$ -99.99!"))
// Output: -99.99
}
// TestDomain tests the domain sanitize method
func TestDomain(t *testing.T) {
t.Parallel()
t.Run("valid cases", func(t *testing.T) {
var tests = []struct {
name string
input string
expected string
preserveCase bool
removeWww bool
}{
{
"no domain name",
"",
"",
true,
true,
},
{
"remove leading http",
"http://IAmaDomain.com",
"IAmaDomain.com",
true,
false,
},
{
"remove leading http and lowercase",
"http://IAmaDomain.com",
"iamadomain.com",
false,
false,
},
{
"full url with params",
"https://IAmaDomain.com/?this=that#plusThis",
"iamadomain.com",
false,
false,
},
{
"full url with params, remove www",
"http://www.IAmaDomain.com/?this=that#plusThis",
"iamadomain.com",
false,
true,
},
{
"full url with params, leave www",
"http://www.IAmaDomain.com/?this=that#plusThis",
"www.iamadomain.com",
false,
false,
},
{
"caps domain, remove www",
"WWW.DOMAIN.COM",
"domain.com",
false,
true,
},
{
"mixed caps domain, remove www",
"WwW.DOMAIN.COM",
"domain.com",
false,
true,
},
{
"domain with tabs and spaces",
` domain.com`,
"domain.com",
false,
true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output, err := Domain(test.input, test.preserveCase, test.removeWww)
require.NoError(t, err)
assert.Equal(t, test.expected, output)
})
}
})
t.Run("invalid cases", func(t *testing.T) {
var tests = []struct {
name string
input string
expected string
preserveCase bool
removeWww bool
}{
{
"spaces in domain",
"http://www.I am a domain.com",
"http://www.I am a domain.com",
true,
true,
},
{
"symbol in domain",
"!I_am a domain.com",
"http://!I_am a domain.com",
true,
true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output, err := Domain(test.input, test.preserveCase, test.removeWww)
require.Error(t, err)
assert.Equal(t, test.expected, output)
})
}
})
}
// BenchmarkDomain benchmarks the Domain method
func BenchmarkDomain(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = Domain("https://Example.COM/?param=value", false, false)
}
}
// BenchmarkDomain_PreserveCase benchmarks the Domain method
func BenchmarkDomain_PreserveCase(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = Domain("https://Example.COM/?param=value", true, false)
}
}
// BenchmarkDomain_RemoveWww benchmarks the Domain method
func BenchmarkDomain_RemoveWww(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = Domain("https://Example.COM/?param=value", false, true)
}
}
// ExampleDomain example using Domain()
func ExampleDomain() {
fmt.Println(Domain("https://www.Example.COM/?param=value", false, false))
// Output: www.example.com <nil>
}
// ExampleDomain_preserveCase example using Domain() and preserving the case
func ExampleDomain_preserveCase() {
fmt.Println(Domain("https://www.Example.COM/?param=value", true, false))
// Output: www.Example.COM <nil>
}
// ExampleDomain_removeWww example using Domain() and removing the www subdomain
func ExampleDomain_removeWww() {
fmt.Println(Domain("https://www.Example.COM/?param=value", false, true))
// Output: example.com <nil>
}
// TestEmail tests the email sanitize method
func TestEmail(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expected string
preserveCase bool
}{
{"mailto:[email protected]", "[email protected]", false},
{"[email protected]", "[email protected]", false},
{"[email protected]", "[email protected]", false},
{"[email protected]", "[email protected]", false},
{" test_ME @GmAil.com ", "[email protected]", false},
{" <<test_ME @GmAil.com!>> ", "[email protected]", false},
{" [email protected] ", "[email protected]", false},
{" [email protected] ", "[email protected]", true},
}
for _, test := range tests {
output := Email(test.input, test.preserveCase)
assert.Equal(t, test.expected, output)
}
}
// BenchmarkEmail benchmarks the Email method
func BenchmarkEmail(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Email("mailto:[email protected] ", false)
}
}
// BenchmarkEmail_PreserveCase benchmarks the Email method
func BenchmarkEmail_PreserveCase(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Email("mailto:[email protected] ", true)
}
}
// ExampleEmail example using Email()
func ExampleEmail() {
fmt.Println(Email("mailto:[email protected]", false))
// Output: [email protected]
}
// ExampleEmail_preserveCase example using Email() and preserving the case
func ExampleEmail_preserveCase() {
fmt.Println(Email("mailto:[email protected]", true))
// Output: [email protected]
}
// TestFirstToUpper tests the first to upper method
func TestFirstToUpper(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expected string
}{
{"thisworks", "Thisworks"},
{"Thisworks", "Thisworks"},
{"this", "This"},
{"t", "T"},
{"tt", "Tt"},
}
for _, test := range tests {
output := FirstToUpper(test.input)
assert.Equal(t, test.expected, output)
}
}
// BenchmarkFirstToUpper benchmarks the FirstToUpper method
func BenchmarkFirstToUpper(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = FirstToUpper("make this upper")
}
}
// ExampleFirstToUpper example using FirstToUpper()
func ExampleFirstToUpper() {
fmt.Println(FirstToUpper("this works"))
// Output: This works
}
// TestFormalName tests the formal name method
func TestFormalName(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expected string
}{
{"Mark Mc'Cuban-Host", "Mark Mc'Cuban-Host"},
{"Mark Mc'Cuban-Host the SR.", "Mark Mc'Cuban-Host the SR."},
{"Mark Mc'Cuban-Host the Second.", "Mark Mc'Cuban-Host the Second."},
{"Johnny Apple.Seed, Martin", "Johnny Apple.Seed, Martin"},
{"Does #Not Work!", "Does Not Work"},
}
for _, test := range tests {
output := FormalName(test.input)
assert.Equal(t, test.expected, output)
}
}
// BenchmarkFormalName benchmarks the FormalName method
func BenchmarkFormalName(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = FormalName("John McDonald Jr.")
}
}
// ExampleFormalName example using FormalName()
func ExampleFormalName() {
fmt.Println(FormalName("John McDonald Jr.!"))
// Output: John McDonald Jr.
}
// TestHTML tests the HTML sanitize method
func TestHTML(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expected string
}{
{"<b>This works?</b>", "This works?"},
{"<html><b>This works?</b><i></i></br></html>", "This works?"},
{"<html><b class='test'>This works?</b><i></i></br></html>", "This works?"},
}
for _, test := range tests {
output := HTML(test.input)
assert.Equal(t, test.expected, output)
}
}
// BenchmarkHTML benchmarks the HTML method
func BenchmarkHTML(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = FormalName("<html><b>Test This!</b></html>")
}
}
// ExampleHTML example using HTML()
func ExampleHTML() {
fmt.Println(HTML("<body>This Works?</body>"))
// Output: This Works?
}
// TestIPAddress tests the ip address sanitize method
func TestIPAddress(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expected string
}{
{"192.168.3.6", "192.168.3.6"},
{"255.255.255.255", "255.255.255.255"},
{"304.255.255.255", ""},
{"fail", ""},
{"192-123-122-123", ""},
{"2602:305:bceb:1bd0:44ef:fedb:4f8f:da4f", "2602:305:bceb:1bd0:44ef:fedb:4f8f:da4f"},
{"2602:305:bceb:1bd0:44ef:2:2:2", "2602:305:bceb:1bd0:44ef:2:2:2"},
{"2:2:2:2:2:2:2:2", "2:2:2:2:2:2:2:2"},
{"192.2", ""},
{"192.2!", ""},
{"IP: 192.168.0.1 ", ""},
{" 192.168.0.1 ", "192.168.0.1"},
{" ##!192.168.0.1!## ", "192.168.0.1"},
{` 192.168.1.1`, "192.168.1.1"},
{`2001:0db8:85a3:0000:0000:8a2e:0370:7334`, "2001:db8:85a3::8a2e:370:7334"}, // Gets parsed and changes the display, see: https://en.wikipedia.org/wiki/IPv6_address
{`2001:0db8::0001:0000`, "2001:db8::1:0"}, // Gets parsed and changes the display, see: https://en.wikipedia.org/wiki/IPv6_address
{`2001:db8:0:0:1:0:0:1`, "2001:db8::1:0:0:1"}, // Gets parsed and changes the display, see: https://en.wikipedia.org/wiki/IPv6_address
{`2001:db8:0000:1:1:1:1:1`, "2001:db8:0:1:1:1:1:1"}, // Gets parsed and changes the display, see: https://en.wikipedia.org/wiki/IPv6_address
{`0:0:0:0:0:0:0:1`, "::1"}, // Gets parsed and changes the display, see: https://en.wikipedia.org/wiki/IPv6_address
{`0:0:0:0:0:0:0:0`, "::"}, // Gets parsed and changes the display, see: https://en.wikipedia.org/wiki/IPv6_address
}
for _, test := range tests {
output := IPAddress(test.input)
assert.Equal(t, test.expected, output)
}
}
// BenchmarkIPAddress benchmarks the IPAddress method
func BenchmarkIPAddress(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = IPAddress(" 192.168.0.1 ")
}
}
// BenchmarkIPAddress_V6 benchmarks the IPAddress method
func BenchmarkIPAddress_IPV6(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = IPAddress(" 2602:305:bceb:1bd0:44ef:fedb:4f8f:da4f ")
}
}
// ExampleIPAddress example using IPAddress() for IPV4 address
func ExampleIPAddress() {
fmt.Println(IPAddress(" 192.168.0.1 "))
// Output: 192.168.0.1
}
// ExampleIPAddress_ipv6 example using IPAddress() for IPV6 address
func ExampleIPAddress_ipv6() {
fmt.Println(IPAddress(" 2602:305:bceb:1bd0:44ef:fedb:4f8f:da4f "))
// Output: 2602:305:bceb:1bd0:44ef:fedb:4f8f:da4f
}
// TestNumeric tests the numeric sanitize method
func TestNumeric(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expected string
}{
{" > Test This String-!1234", "1234"},
{" $1.00 Price!", "100"},
}
for _, test := range tests {
output := Numeric(test.input)
assert.Equal(t, test.expected, output)
}
}
// BenchmarkNumeric benchmarks the numeric method
func BenchmarkNumeric(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Numeric(" 192.168.0.1 ")
}
}
// ExampleNumeric example using Numeric()
func ExampleNumeric() {
fmt.Println(Numeric("This:123 + 90!"))
// Output: 12390
}
// TestPathName tests the path name sanitize method
func TestPathName(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expected string
}{
{"My BadPath (10)", "MyBadPath10"},
{"My BadPath (10)[]()!$", "MyBadPath10"},
{"My_Folder-Path-123_TEST", "My_Folder-Path-123_TEST"},
}
for _, test := range tests {
output := PathName(test.input)
assert.Equal(t, test.expected, output)
}
}
// BenchmarkPathName benchmarks the PathName method
func BenchmarkPathName(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = PathName("/This-Path-Name_Works-/")
}
}
// ExampleNumeric example using PathName()
func ExamplePathName() {
fmt.Println(PathName("/This-Works_Now-123/!"))
// Output: This-Works_Now-123
}
// TestPunctuation tests the punctuation method
func TestPunctuation(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expected string
}{
{"Mark Mc'Cuban-Host", "Mark Mc'Cuban-Host"},
{"Johnny Apple.Seed, Martin", "Johnny Apple.Seed, Martin"},
{"Does #Not Work!", "Does #Not Work!"},
{"Does #Not Work!?", "Does #Not Work!?"},
{"Does #Not Work! & this", "Does #Not Work! & this"},
{`[@"Does" 'this' work?@]this`, `"Does" 'this' work?this`},
{"Does, 123^* Not & Work!?", "Does, 123 Not & Work!?"},
}
for _, test := range tests {
output := Punctuation(test.input)
assert.Equal(t, test.expected, output)
}
}
// BenchmarkPunctuation benchmarks the Punctuation method
func BenchmarkPunctuation(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Punctuation("Does this work? They're doing it?")
}
}
// ExamplePunctuation example using Punctuation()
func ExamplePunctuation() {
fmt.Println(Punctuation(`[@"Does" 'this' work?@] this too`))
// Output: "Does" 'this' work? this too
}
// TestScientificNotation tests the scientific notation sanitize method
func TestScientificNotation(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expected string
}{
{" String: 1.23 ", "1.23"},
{" String: 1.23e-3 ", "1.23e-3"},
{" String: -1.23e-3 ", "-1.23e-3"},
{" String: 001.2300 ", "001.2300"},
{" $-1.034234 word", "-1.034234"},
{" $-1%.034234e word", "-1.034234e"},
{"/n<< $-1.034234 >>/n", "-1.034234"},
}
for _, test := range tests {
output := ScientificNotation(test.input)
assert.Equal(t, test.expected, output)
}
}
// BenchmarkDecimal benchmarks the ScientificNotation method
func BenchmarkScientificNotation(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = ScientificNotation("String: -1.096e-3")
}
}
// ExampleDecimal example using Decimal() for a positive number
func ExampleScientificNotation() {
fmt.Println(ScientificNotation("$ 1.096e-3!"))
// Output: 1.096e-3
}
// TestScripts tests the script removal
func TestScripts(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expected string
}{
{"this <script>$('#something').hide()</script>", "this "},
{"this <script type='text/javascript'>$('#something').hide()</script>", "this "},
{`this <script type="text/javascript" class="something">$('#something').hide();</script>`, "this "},
{`this <iframe width="50" class="something"></iframe>`, "this "},
{`this <embed width="50" class="something"></embed>`, "this "},
{`this <object width="50" class="something"></object>`, "this "},
}
for _, test := range tests {
output := Scripts(test.input)
assert.Equal(t, test.expected, output)
}
}
// BenchmarkScripts benchmarks the Scripts method
func BenchmarkScripts(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Scripts("<script>$(){ var remove='me'; }</script>")
}
}
// ExampleScripts example using Scripts()
func ExampleScripts() {
fmt.Println(Scripts(`Does<script>This</script>Work?`))
// Output: DoesWork?
}
// TestSingleLine test the single line sanitize method
func TestSingleLine(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expected string
}{
{`Mark
Mc'Cuban-Host`, "Mark Mc'Cuban-Host"},
{`Mark
Mc'Cuban-Host
something else`, "Mark Mc'Cuban-Host something else"},
{` Mark
Mc'Cuban-Host
something else`, " Mark Mc'Cuban-Host something else"},
}
for _, test := range tests {
output := SingleLine(test.input)
assert.Equal(t, test.expected, output)
}
}
// BenchmarkSingleLine benchmarks the SingleLine method
func BenchmarkSingleLine(b *testing.B) {
testString := `This line
That Line
Another Line`
for i := 0; i < b.N; i++ {
_ = SingleLine(testString)
}
}
// ExampleSingleLine example using SingleLine()
func ExampleSingleLine() {
fmt.Println(SingleLine(`Does
This
Work?`))
// Output: Does This Work?
}
// TestTime tests the time sanitize method
func TestTime(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expected string
}{
{"t00:00d -EST", "00:00"},
{"t00:00:00d -EST", "00:00:00"},
{"SOMETHING t00:00:00d -EST DAY", "00:00:00"},
}
for _, test := range tests {
output := Time(test.input)
assert.Equal(t, test.expected, output)
}
}
// BenchmarkTime benchmarks the Time method
func BenchmarkTime(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Time("Time is 05:10:23")
}
}
// ExampleTime example using Time()
func ExampleTime() {
fmt.Println(Time(`Time 01:02:03!`))
// Output: 01:02:03
}
// TestURI tests the URI sanitize method
func TestURI(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expected string
}{
{"Test?=what! &this=that", "Test?=what&this=that"},
{"Test?=what! &this=/that/!()*^", "Test?=what&this=/that/"},
{"/This/Works/?that=123&this#page10%", "/This/Works/?that=123&this#page10%"},
}
for _, test := range tests {
output := URI(test.input)
assert.Equal(t, test.expected, output)
}
}
// BenchmarkURI benchmarks the URI method
func BenchmarkURI(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = XSS("/Test/This/Url/?param=value")
}
}
// ExampleURI example using URI()
func ExampleURI() {
fmt.Println(URI("/This/Works?^No&this"))
// Output: /This/Works?No&this
}
// TestURL tests the URL sanitize method
func TestURL(t *testing.T) {
t.Parallel()
var tests = []struct {
name string
input string
expected string
}{
{"remove spaces", "Test?=what! &this=that#works", "Test?=what&this=that#works"},
{"no dollar signs", "/this/test?param$", "/this/test?param"},
{"using at sign", "https://medium.com/@username/some-title-that-is-a-article", "https://medium.com/@username/some-title-that-is-a-article"},
{"removing symbols", "https://domain.com/this/test?param$!@()[]{}'<>", "https://domain.com/this/test?param@"},
{"params and anchors", "https://domain.com/this/test?this=value&another=123%#page", "https://domain.com/this/test?this=value&another=123%#page"},
{"allow commas", "https://domain.com/this/test,this,value", "https://domain.com/this/test,this,value"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := URL(test.input)
assert.Equal(t, test.expected, output)
})
}
}
// BenchmarkURL benchmarks the URL method
func BenchmarkURL(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = URL("/Test/This/Url/?param=value")
}
}
// ExampleURL example using URL()
func ExampleURL() {
fmt.Println(URL("https://Example.com/This/Works?^No&this"))
// Output: https://Example.com/This/Works?No&this
}
// TestXML tests the XML sanitize method
func TestXML(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expected string
}{
{`<?xml version="1.0" encoding="UTF-8"?><note>Something</note>`, "Something"},
{`<body>This works?</body><title>Something</title>`, "This works?Something"},
}
for _, test := range tests {
output := XML(test.input)
assert.Equal(t, test.expected, output)
}
}
// BenchmarkXML benchmarks the XML method
func BenchmarkXML(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = XML("<xml>Test This!</xml>")
}
}
// ExampleXML example using XML()
func ExampleXML() {
fmt.Println(XML("<xml>This?</xml>"))
// Output: This?
}
// TestXSS tests the XSS sanitize method
func TestXSS(t *testing.T) {
t.Parallel()
var tests = []struct {