|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+package config_test
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "encoding/json"
|
|
|
5
|
+ "net/url"
|
|
|
6
|
+ "testing"
|
|
|
7
|
+
|
|
|
8
|
+ "github.com/9seconds/mtg/v2/config"
|
|
|
9
|
+ "github.com/stretchr/testify/assert"
|
|
|
10
|
+ "github.com/stretchr/testify/suite"
|
|
|
11
|
+)
|
|
|
12
|
+
|
|
|
13
|
+type typeURLTestStruct struct {
|
|
|
14
|
+ Value config.TypeURL `json:"value"`
|
|
|
15
|
+}
|
|
|
16
|
+
|
|
|
17
|
+type TypeURLTestSuite struct {
|
|
|
18
|
+ suite.Suite
|
|
|
19
|
+}
|
|
|
20
|
+
|
|
|
21
|
+func (suite *TypeURLTestSuite) TestUnmarshalFail() {
|
|
|
22
|
+ testData := []string{
|
|
|
23
|
+ "http:/aaa.com",
|
|
|
24
|
+ "ipv4",
|
|
|
25
|
+ "111",
|
|
|
26
|
+ "://111",
|
|
|
27
|
+ "http://aaa.com:xxx",
|
|
|
28
|
+ "gopher://aaa.com:888",
|
|
|
29
|
+ }
|
|
|
30
|
+
|
|
|
31
|
+ for _, v := range testData {
|
|
|
32
|
+ data, err := json.Marshal(map[string]string{
|
|
|
33
|
+ "value": v,
|
|
|
34
|
+ })
|
|
|
35
|
+ suite.NoError(err)
|
|
|
36
|
+
|
|
|
37
|
+ suite.T().Run(v, func(t *testing.T) {
|
|
|
38
|
+ assert.Error(t, json.Unmarshal(data, &typeURLTestStruct{}))
|
|
|
39
|
+ })
|
|
|
40
|
+ }
|
|
|
41
|
+}
|
|
|
42
|
+
|
|
|
43
|
+func (suite *TypeURLTestSuite) TestUnmarshalOk() {
|
|
|
44
|
+ testData := map[string]string{
|
|
|
45
|
+ "https://10.0.0.10:80": "https://10.0.0.10:80",
|
|
|
46
|
+ "https://10.0.0.10:443": "https://10.0.0.10",
|
|
|
47
|
+ "http://10.0.0.10:8": "http://10.0.0.10:8",
|
|
|
48
|
+ "http://10.0.0.10:80": "http://10.0.0.10",
|
|
|
49
|
+ "socks5://10.0.0.10:1080": "socks5://10.0.0.10",
|
|
|
50
|
+ "socks5://10.0.0.10:888": "socks5://10.0.0.10:888",
|
|
|
51
|
+ }
|
|
|
52
|
+
|
|
|
53
|
+ for k, v := range testData {
|
|
|
54
|
+ expected := k
|
|
|
55
|
+ actual := v
|
|
|
56
|
+
|
|
|
57
|
+ data, err := json.Marshal(map[string]string{
|
|
|
58
|
+ "value": actual,
|
|
|
59
|
+ })
|
|
|
60
|
+ suite.NoError(err)
|
|
|
61
|
+
|
|
|
62
|
+ suite.T().Run(actual, func(t *testing.T) {
|
|
|
63
|
+ testStruct := &typeURLTestStruct{}
|
|
|
64
|
+
|
|
|
65
|
+ assert.NoError(t, json.Unmarshal(data, testStruct))
|
|
|
66
|
+ assert.Equal(t, expected, testStruct.Value.Value(nil).String())
|
|
|
67
|
+
|
|
|
68
|
+ marshalled, err := testStruct.Value.MarshalText()
|
|
|
69
|
+ assert.NoError(t, err)
|
|
|
70
|
+ assert.Equal(t, expected, string(marshalled))
|
|
|
71
|
+ })
|
|
|
72
|
+ }
|
|
|
73
|
+}
|
|
|
74
|
+
|
|
|
75
|
+func (suite *TypeURLTestSuite) TestValue() {
|
|
|
76
|
+ testStruct := &typeURLTestStruct{}
|
|
|
77
|
+
|
|
|
78
|
+ u1, _ := url.Parse("https://10.0.0.10:80")
|
|
|
79
|
+ u2, _ := url.Parse("https://10.1.0.10:80")
|
|
|
80
|
+
|
|
|
81
|
+ suite.Equal("https://10.0.0.10:80", testStruct.Value.Value(u1).String())
|
|
|
82
|
+ suite.Equal("https://10.1.0.10:80", testStruct.Value.Value(u2).String())
|
|
|
83
|
+
|
|
|
84
|
+ data, err := json.Marshal(map[string]string{
|
|
|
85
|
+ "value": "http://127.0.0.1:80",
|
|
|
86
|
+ })
|
|
|
87
|
+ suite.NoError(err)
|
|
|
88
|
+ suite.NoError(json.Unmarshal(data, testStruct))
|
|
|
89
|
+
|
|
|
90
|
+ suite.Equal("http://127.0.0.1:80", testStruct.Value.Value(u1).String())
|
|
|
91
|
+ suite.Equal("http://127.0.0.1:80", testStruct.Value.Value(u2).String())
|
|
|
92
|
+}
|
|
|
93
|
+
|
|
|
94
|
+func TestTypeURL(t *testing.T) {
|
|
|
95
|
+ t.Parallel()
|
|
|
96
|
+ suite.Run(t, &TypeURLTestSuite{})
|
|
|
97
|
+}
|