-
Notifications
You must be signed in to change notification settings - Fork 3
/
url_test.go
169 lines (156 loc) · 3.43 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
package validator
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUrl_ValidateValue_ValidURL_Successfully(t *testing.T) {
tests := []struct {
Name string
ValidSchemes []string
URL string
}{
{
Name: "valid example url with scheme https",
URL: "https://example.com",
},
{
Name: "valid example url with scheme http",
URL: "http://example.com",
},
{
Name: "valid example url",
URL: "http://example.com/hkjhiojhio/knhoijhio?a=1&s=2",
},
{
Name: "valid example url with custom scheme test.app",
URL: "test://example.com",
ValidSchemes: []string{"test"},
},
{
Name: "any protocol",
URL: "test://example.com",
ValidSchemes: []string{AllowAnyURLSchema},
},
}
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
ctx := context.Background()
r := NewURL()
if len(tt.ValidSchemes) > 0 {
r = r.WithValidScheme(tt.ValidSchemes...)
}
err := r.ValidateValue(ctx, tt.URL)
assert.NoError(t, err)
})
}
}
func TestUrl_ValidateValue_InvalidURL_Failure(t *testing.T) {
tests := []struct {
Name string
ValidSchemes []string
URL string
}{
{
Name: "invalid protocol",
URL: "httpz://example.com",
},
{
Name: "invalid domain",
URL: "http://examplecom",
},
{
Name: "domain is empty",
URL: "http://",
},
}
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
ctx := context.Background()
r := NewURL()
if len(tt.ValidSchemes) > 0 {
r.WithValidScheme(tt.ValidSchemes...)
}
err := r.ValidateValue(ctx, tt.URL)
assert.Error(t, err)
})
}
}
func TestUrl_ValidateValue_IDN_Successfully(t *testing.T) {
ctx := context.Background()
err := NewURL().WithEnableIDN().ValidateValue(ctx, "https://президент.рф")
assert.NoError(t, err)
}
func TestUrlValidateValue_EmptyValue_ReturnsError(t *testing.T) {
ctx := context.Background()
err := NewURL().ValidateValue(ctx, "")
assert.Error(t, err)
}
func TestUrlValidateValue_InvalidValue_ReturnsExpectedErrorMessage(t *testing.T) {
ctx := context.Background()
err := NewURL().WithMessage("test error").ValidateValue(ctx, "http://")
assert.Error(t, err)
assert.Equal(t, "test error.", err.Error())
}
func TestDeepLinkURL_ValidateValue_ValidURL_Successfully(t *testing.T) {
tests := []struct {
Name string
URL string
}{
{
Name: "tg protocol #1",
URL: "tg:resolve?domain={domain}",
},
{
Name: "tg protocol #2",
URL: "tg://resolve?domain={domain}",
},
{
Name: "tg protocol #1 without query",
URL: "tg:resolve",
},
{
Name: "tg protocol #2 without query",
URL: "tg://resolve",
},
}
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
ctx := context.Background()
r := NewDeepLinkURL()
err := r.ValidateValue(ctx, tt.URL)
assert.NoError(t, err)
})
}
}
func TestDeepLinkURL_ValidateValue_InvalidURL_Failure(t *testing.T) {
tests := []struct {
Name string
URL string
}{
{
Name: "http scheme",
URL: "http://example.com",
},
{
Name: "https scheme",
URL: "https://example.com",
},
{
Name: "domain only",
URL: "example.com",
},
{
Name: "invalid scheme",
URL: "://example.com",
},
}
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
ctx := context.Background()
r := NewDeepLinkURL()
err := r.ValidateValue(ctx, tt.URL)
assert.Error(t, err)
})
}
}