-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_test.go
172 lines (146 loc) · 3.16 KB
/
utils_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
package main
import (
"fmt"
"testing"
"time"
)
func TestSanitizeName(t *testing.T) {
if sanitizeName("VIDEO") != "video" {
t.Errorf("invalid sanitize")
}
if sanitizeName("!!!") != "" {
t.Errorf("invalid sanitize")
}
}
func TestBoolUint8(t *testing.T) {
if boolUint8(true) != 1 {
t.Errorf("invalid sanitize")
}
if boolUint8(false) != 0 {
t.Errorf("invalid sanitize")
}
}
func TestSanitizeEntityID(t *testing.T) {
if sanitizeEntityID("1") != "1" {
t.Errorf("invalid sanitize")
}
if sanitizeEntityID("!!!") != "" {
t.Errorf("invalid sanitize")
}
}
func TestSanitizeLanguage(t *testing.T) {
if sanitizeLanguage("fa-IR") != "fa" {
t.Errorf("invalid sanitize")
}
if sanitizeLanguage("per") != "fa" {
t.Errorf("invalid sanitize")
}
if sanitizeLanguage("!!!") != "" {
t.Errorf("invalid sanitize")
}
}
func TestSanitizeEntityTaxonomyID(t *testing.T) {
if sanitizeEntityTaxonomyID("100") != 100 {
t.Errorf("invalid sanitize")
}
if sanitizeEntityTaxonomyID("!!!") != 0 {
t.Errorf("invalid sanitize")
}
}
func TestHash(t *testing.T) {
s := checksum("1")
if len(s) != 24 {
t.Errorf("invalid hash")
}
}
func TestHash2(t *testing.T) {
s := checksum("")
if len(s) != 24 {
t.Errorf("invalid hash")
}
}
func TestIsValidURL(t *testing.T) {
if sanitizeURL("\x18") != "" {
t.Errorf("invalid url")
}
if isValidURL("\x18") {
t.Errorf("invalid url")
}
if isValidURL("") {
t.Errorf("invalid url")
}
if !isValidURL("http://google.com") {
t.Errorf("invalid url")
}
}
func TestParseKeywords(t *testing.T) {
k1 := parseKeywords("1,2,3,4,5,6,7,8,9,10,11")
if len(k1) != 10 {
t.Errorf("invalid keyword parse")
}
k2 := parseKeywords("")
if len(k2) != 0 {
t.Errorf("invalid keyword parse")
}
k3 := parseKeywords("1,2")
if len(k3) != 2 {
t.Errorf("invalid keyword parse")
}
}
func TestURLDomainParse(t *testing.T) {
urls := []string{
"https://www.xn--mgbtj4c7ad63e.xn--mgba3a4f16a",
"https://subdomain.فروشگاه.ایران/",
"https://sub.of.google.com/",
"http://192.168.1.1/",
"http://localhost/",
}
for _, ur := range urls {
uu := getURL(ur)
if uu == nil {
t.Errorf("invaid url")
}
d1 := getDomain(uu)
if d1 == "" {
t.Errorf("invaid domain")
}
d2 := getSecondDomainLevel(uu)
if d2 == "" {
t.Errorf("invaid second level domain")
}
}
if getURL("") != nil {
t.Errorf("empty url string")
}
}
func TestGetCursorID(t *testing.T) {
c1, _ := getCursorID()
time.Sleep(time.Duration(2) * time.Millisecond)
c2, _ := getCursorID()
fmt.Println(c1)
fmt.Println(c2)
if c1 == c2 {
t.Errorf("must not same cursor")
}
}
func TestGetURLPath(t *testing.T) {
urls := []string{
"https://www.google.com/path/تست/file.html?",
"https://www.google.com/path/%D8%AA%D8%B3%D8%AA/file.html?",
"https://www.google.com/path/to/file.html?foo=bar",
"https://www.google.com/path/to/file.html?foo=bar#fragment",
"https://www.google.com/path/to/file.html?foo=bar#/extra",
}
for _, u := range urls {
p := getURLPath(getURL(u))
s := getURLString(getURL(u))
if p == "" || s == "" {
t.Errorf("invalid url path")
}
}
}
func BenchmarkChecksum(b *testing.B) {
for n := 0; n < b.N; n++ {
checksum("a")
}
}