|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+package config_test
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "crypto/rand"
|
|
|
5
|
+ "encoding/base64"
|
|
|
6
|
+ "encoding/json"
|
|
|
7
|
+ "os"
|
|
|
8
|
+ "path/filepath"
|
|
|
9
|
+ "strconv"
|
|
|
10
|
+ "testing"
|
|
|
11
|
+
|
|
|
12
|
+ "github.com/9seconds/mtg/v2/config"
|
|
|
13
|
+ "github.com/stretchr/testify/assert"
|
|
|
14
|
+ "github.com/stretchr/testify/suite"
|
|
|
15
|
+)
|
|
|
16
|
+
|
|
|
17
|
+type typeBlocklistURITestStruct struct {
|
|
|
18
|
+ Value config.TypeBlocklistURI `json:"value"`
|
|
|
19
|
+}
|
|
|
20
|
+
|
|
|
21
|
+type TypeBlocklistURITestSuite struct {
|
|
|
22
|
+ suite.Suite
|
|
|
23
|
+}
|
|
|
24
|
+
|
|
|
25
|
+func (suite *TypeBlocklistURITestSuite) TestUnmarshalFail() {
|
|
|
26
|
+ rnd := make([]byte, 48)
|
|
|
27
|
+
|
|
|
28
|
+ rand.Read(rnd) // nolint: errcheck
|
|
|
29
|
+
|
|
|
30
|
+ unknownPath := base64.StdEncoding.EncodeToString(rnd)
|
|
|
31
|
+
|
|
|
32
|
+ testData := []string{
|
|
|
33
|
+ "1",
|
|
|
34
|
+ unknownPath,
|
|
|
35
|
+ "/" + unknownPath,
|
|
|
36
|
+ "http:/",
|
|
|
37
|
+ "gopher://lalalal",
|
|
|
38
|
+ }
|
|
|
39
|
+
|
|
|
40
|
+ for _, v := range testData {
|
|
|
41
|
+ data, err := json.Marshal(map[string]string{
|
|
|
42
|
+ "value": v,
|
|
|
43
|
+ })
|
|
|
44
|
+ suite.NoError(err)
|
|
|
45
|
+
|
|
|
46
|
+ suite.T().Run(v, func(t *testing.T) {
|
|
|
47
|
+ assert.Error(t, json.Unmarshal(data, &typeBlocklistURITestStruct{}))
|
|
|
48
|
+ })
|
|
|
49
|
+ }
|
|
|
50
|
+}
|
|
|
51
|
+
|
|
|
52
|
+func (suite *TypeBlocklistURITestSuite) TestUnmarshalOk() {
|
|
|
53
|
+ dir, _ := os.Getwd()
|
|
|
54
|
+ dir, _ = filepath.Abs(dir)
|
|
|
55
|
+
|
|
|
56
|
+ testData := []string{
|
|
|
57
|
+ "http://lalala",
|
|
|
58
|
+ filepath.Join(dir, "config.go"),
|
|
|
59
|
+ "https://lalala",
|
|
|
60
|
+ }
|
|
|
61
|
+
|
|
|
62
|
+ for _, v := range testData {
|
|
|
63
|
+ value := v
|
|
|
64
|
+
|
|
|
65
|
+ data, err := json.Marshal(map[string]string{
|
|
|
66
|
+ "value": v,
|
|
|
67
|
+ })
|
|
|
68
|
+ suite.NoError(err)
|
|
|
69
|
+
|
|
|
70
|
+ suite.T().Run(v, func(t *testing.T) {
|
|
|
71
|
+ testStruct := &typeBlocklistURITestStruct{}
|
|
|
72
|
+
|
|
|
73
|
+ assert.NoError(t, json.Unmarshal(data, testStruct))
|
|
|
74
|
+ assert.EqualValues(t, value, testStruct.Value.Value(""))
|
|
|
75
|
+ })
|
|
|
76
|
+ }
|
|
|
77
|
+}
|
|
|
78
|
+
|
|
|
79
|
+func (suite *TypeBlocklistURITestSuite) TestMarshalOk() {
|
|
|
80
|
+ dir, _ := os.Getwd()
|
|
|
81
|
+ dir, _ = filepath.Abs(dir)
|
|
|
82
|
+
|
|
|
83
|
+ testData := []string{
|
|
|
84
|
+ "http://lalalal",
|
|
|
85
|
+ filepath.Join(dir, "config.go"),
|
|
|
86
|
+ }
|
|
|
87
|
+
|
|
|
88
|
+ for _, v := range testData {
|
|
|
89
|
+ name := v
|
|
|
90
|
+
|
|
|
91
|
+ data, err := json.Marshal(map[string]string{
|
|
|
92
|
+ "value": name,
|
|
|
93
|
+ })
|
|
|
94
|
+ suite.NoError(err)
|
|
|
95
|
+
|
|
|
96
|
+ suite.T().Run(name, func(t *testing.T) {
|
|
|
97
|
+ testStruct := &typeBlocklistURITestStruct{}
|
|
|
98
|
+
|
|
|
99
|
+ assert.NoError(t, json.Unmarshal(data, testStruct))
|
|
|
100
|
+ assert.Equal(t, name, testStruct.Value.String())
|
|
|
101
|
+
|
|
|
102
|
+ marshalled, err := testStruct.Value.MarshalText()
|
|
|
103
|
+ assert.NoError(t, err)
|
|
|
104
|
+ assert.Equal(t, name, string(marshalled))
|
|
|
105
|
+ })
|
|
|
106
|
+ }
|
|
|
107
|
+}
|
|
|
108
|
+
|
|
|
109
|
+func (suite *TypeBlocklistURITestSuite) TestValue() {
|
|
|
110
|
+ testStruct := &typeBlocklistURITestStruct{}
|
|
|
111
|
+
|
|
|
112
|
+ suite.Equal("http://lalala", testStruct.Value.Value("http://lalala"))
|
|
|
113
|
+
|
|
|
114
|
+ data, err := json.Marshal(map[string]string{
|
|
|
115
|
+ "value": "http://blablabla",
|
|
|
116
|
+ })
|
|
|
117
|
+ suite.NoError(err)
|
|
|
118
|
+ suite.NoError(json.Unmarshal(data, testStruct))
|
|
|
119
|
+
|
|
|
120
|
+ suite.Equal("http://blablabla", testStruct.Value.Value(""))
|
|
|
121
|
+}
|
|
|
122
|
+
|
|
|
123
|
+func (suite *TypeBlocklistURITestSuite) TestIsRemote() {
|
|
|
124
|
+ dir, _ := os.Getwd()
|
|
|
125
|
+ dir, _ = filepath.Abs(dir)
|
|
|
126
|
+
|
|
|
127
|
+ testData := map[bool]string{
|
|
|
128
|
+ true: "http://lalalal",
|
|
|
129
|
+ false: filepath.Join(dir, "config.go"),
|
|
|
130
|
+ }
|
|
|
131
|
+
|
|
|
132
|
+ for k, v := range testData {
|
|
|
133
|
+ ok := k
|
|
|
134
|
+
|
|
|
135
|
+ data, err := json.Marshal(map[string]string{
|
|
|
136
|
+ "value": v,
|
|
|
137
|
+ })
|
|
|
138
|
+ suite.NoError(err)
|
|
|
139
|
+
|
|
|
140
|
+ suite.T().Run(strconv.FormatBool(ok), func(t *testing.T) {
|
|
|
141
|
+ testStruct := &typeBlocklistURITestStruct{}
|
|
|
142
|
+ assert.NoError(t, json.Unmarshal(data, testStruct))
|
|
|
143
|
+
|
|
|
144
|
+ if ok {
|
|
|
145
|
+ assert.True(t, testStruct.Value.IsRemote())
|
|
|
146
|
+ } else {
|
|
|
147
|
+ assert.False(t, testStruct.Value.IsRemote())
|
|
|
148
|
+ }
|
|
|
149
|
+ })
|
|
|
150
|
+ }
|
|
|
151
|
+}
|
|
|
152
|
+
|
|
|
153
|
+func TestTypeBlocklistURI(t *testing.T) {
|
|
|
154
|
+ t.Parallel()
|
|
|
155
|
+ suite.Run(t, &TypeBlocklistURITestSuite{})
|
|
|
156
|
+}
|