-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathurl_test.go
137 lines (102 loc) · 3.37 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
package request
import (
"errors"
"testing"
"github.com/adhocore/urlsh/common"
)
func tester(input URLInput, expect error, t *testing.T) {
if actual := input.Validate(); !errors.Is(actual, expect) {
t.Helper()
t.Errorf("wanted %v error, got %v", expect, actual)
}
}
func TestURLInput_Validate(t *testing.T) {
t.Run("validate empty", func(t *testing.T) {
input := URLInput{URL: ""}
tester(input, common.ErrInvalidURLLen, t)
})
t.Run("validate scheme", func(t *testing.T) {
input := URLInput{URL: "xyz://xyx/xyz/z"}
tester(input, common.ErrInvalidURL, t)
})
t.Run("invalid url", func(t *testing.T) {
input := URLInput{URL: "http://local\\host"}
tester(input, common.ErrInvalidURL, t)
})
t.Run("filtered url", func(t *testing.T) {
input := URLInput{URL: "http://localhost/xxx"}
tester(input, common.ErrFilteredURL, t)
})
t.Run("keywords", func(t *testing.T) {
input := URLInput{URL: "http://local-host", Keywords: make([]string, 11)}
tester(input, common.ErrKeywordsCount, t)
})
t.Run("keyword length", func(t *testing.T) {
input := URLInput{URL: "http://local-host", Keywords: []string{"x"}}
tester(input, common.ErrKeywordLength, t)
})
t.Run("invalid date", func(t *testing.T) {
input := URLInput{URL: "http://local-host", ExpiresOn: "2030x01x01x00x00x00"}
tester(input, common.ErrInvalidDate, t)
})
t.Run("validate OK", func(t *testing.T) {
input := URLInput{URL: "http://local-host", ExpiresOn: "2030-01-01 00:00:00"}
if input.Validate() != nil {
t.Errorf("valid data should not give error")
}
})
}
func TestURLInput_ValidateExpiry(t *testing.T) {
t.Run("invalid expiry", func(t *testing.T) {
input := URLInput{URL: "http://local-host", ExpiresOn: ""}
tester(input, nil, t)
})
t.Run("invalid expiry", func(t *testing.T) {
input := URLInput{URL: "http://local-host", ExpiresOn: "2020-01-01"}
tester(input, common.ErrInvalidDate, t)
})
t.Run("past expiry", func(t *testing.T) {
input := URLInput{URL: "http://local-host", ExpiresOn: "2020-01-01 00:00:00"}
tester(input, common.ErrPastExpiration, t)
})
}
func TestURLInput_GetExpiresOn(t *testing.T) {
t.Run("get expires_on empty", func(t *testing.T) {
input := URLInput{ExpiresOn: ""}
if _, err := input.GetExpiresOn(); err != nil {
t.Errorf("empty date should not give error")
}
})
t.Run("get expires_on invalid", func(t *testing.T) {
input := URLInput{ExpiresOn: "Jan 2020"}
if _, err := input.GetExpiresOn(); err == nil {
t.Errorf("invalid date should give error")
}
})
t.Run("get expires_on ok", func(t *testing.T) {
input := URLInput{ExpiresOn: "2020-01-01 00:00:00"}
if _, err := input.GetExpiresOn(); err != nil {
t.Errorf("valid date should not give error")
}
})
}
func TestURLFilter_GetOffset(t *testing.T) {
t.Run("get offset - empty", func(t *testing.T) {
if actual := (URLFilter{Page: ""}).GetOffset(2); actual != 0 {
t.Errorf("offset wanted 0, got %v", actual)
}
})
t.Run("get offset - less than 2", func(t *testing.T) {
if actual := (URLFilter{Page: "1"}).GetOffset(2); actual != 0 {
t.Errorf("offset wanted 0, got %v", actual)
}
})
t.Run("get offset", func(t *testing.T) {
if actual := (URLFilter{Page: "2"}).GetOffset(50); actual != 50 {
t.Errorf("offset wanted 100, got %v", actual)
}
if actual := (URLFilter{Page: "3"}).GetOffset(50); actual != 100 {
t.Errorf("offset wanted 100, got %v", actual)
}
})
}