|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+package config_test
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "encoding/json"
|
|
|
5
|
+ "net"
|
|
|
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 typeHostPortTestStruct struct {
|
|
|
14
|
+ Value config.TypeHostPort `json:"value"`
|
|
|
15
|
+}
|
|
|
16
|
+
|
|
|
17
|
+type TypeHostPortTestSuite struct {
|
|
|
18
|
+ suite.Suite
|
|
|
19
|
+}
|
|
|
20
|
+
|
|
|
21
|
+func (suite *TypeHostPortTestSuite) TestUnmarshalFail() {
|
|
|
22
|
+ testData := []string{
|
|
|
23
|
+ "10.0.0.10:aaa",
|
|
|
24
|
+ "10.0.0.10:",
|
|
|
25
|
+ ":",
|
|
|
26
|
+ "xxx",
|
|
|
27
|
+ "xxx:80",
|
|
|
28
|
+ }
|
|
|
29
|
+
|
|
|
30
|
+ for _, v := range testData {
|
|
|
31
|
+ data, err := json.Marshal(map[string]string{
|
|
|
32
|
+ "value": v,
|
|
|
33
|
+ })
|
|
|
34
|
+ suite.NoError(err)
|
|
|
35
|
+
|
|
|
36
|
+ suite.T().Run(v, func(t *testing.T) {
|
|
|
37
|
+ assert.Error(t, json.Unmarshal(data, &typeHostPortTestStruct{}))
|
|
|
38
|
+ })
|
|
|
39
|
+ }
|
|
|
40
|
+}
|
|
|
41
|
+
|
|
|
42
|
+func (suite *TypeHostPortTestSuite) TestUnmarshalOk() {
|
|
|
43
|
+ testData := []string{
|
|
|
44
|
+ "10.0.0.10:80",
|
|
|
45
|
+ "0.0.0.0:80",
|
|
|
46
|
+ ":8000",
|
|
|
47
|
+ }
|
|
|
48
|
+
|
|
|
49
|
+ for _, v := range testData {
|
|
|
50
|
+ value := v
|
|
|
51
|
+
|
|
|
52
|
+ data, err := json.Marshal(map[string]string{
|
|
|
53
|
+ "value": v,
|
|
|
54
|
+ })
|
|
|
55
|
+ suite.NoError(err)
|
|
|
56
|
+
|
|
|
57
|
+ suite.T().Run(v, func(t *testing.T) {
|
|
|
58
|
+ testStruct := &typeHostPortTestStruct{}
|
|
|
59
|
+
|
|
|
60
|
+ assert.NoError(t, json.Unmarshal(data, testStruct))
|
|
|
61
|
+ assert.EqualValues(t, value, testStruct.Value.Value(nil, 0))
|
|
|
62
|
+ })
|
|
|
63
|
+ }
|
|
|
64
|
+}
|
|
|
65
|
+
|
|
|
66
|
+func (suite *TypeHostPortTestSuite) TestMarshalOk() {
|
|
|
67
|
+ testData := []string{
|
|
|
68
|
+ "10.0.0.10:80",
|
|
|
69
|
+ "0.0.0.0:80",
|
|
|
70
|
+ ":8000",
|
|
|
71
|
+ }
|
|
|
72
|
+
|
|
|
73
|
+ for _, v := range testData {
|
|
|
74
|
+ value := v
|
|
|
75
|
+
|
|
|
76
|
+ data, err := json.Marshal(map[string]string{
|
|
|
77
|
+ "value": v,
|
|
|
78
|
+ })
|
|
|
79
|
+ suite.NoError(err)
|
|
|
80
|
+
|
|
|
81
|
+ suite.T().Run(v, func(t *testing.T) {
|
|
|
82
|
+ testStruct := &typeHostPortTestStruct{}
|
|
|
83
|
+
|
|
|
84
|
+ assert.NoError(t, json.Unmarshal(data, testStruct))
|
|
|
85
|
+ assert.Equal(t, value, testStruct.Value.String())
|
|
|
86
|
+
|
|
|
87
|
+ marshalled, err := testStruct.Value.MarshalText()
|
|
|
88
|
+ assert.NoError(t, err)
|
|
|
89
|
+ assert.Equal(t, value, string(marshalled))
|
|
|
90
|
+ })
|
|
|
91
|
+ }
|
|
|
92
|
+}
|
|
|
93
|
+
|
|
|
94
|
+func (suite *TypeHostPortTestSuite) TestValue() {
|
|
|
95
|
+ testStruct := &typeHostPortTestStruct{}
|
|
|
96
|
+
|
|
|
97
|
+ suite.EqualValues("127.0.0.1:80",
|
|
|
98
|
+ testStruct.Value.Value(net.ParseIP("127.0.0.1"), 80))
|
|
|
99
|
+ suite.EqualValues("127.1.0.1:80",
|
|
|
100
|
+ testStruct.Value.Value(net.ParseIP("127.1.0.1"), 80))
|
|
|
101
|
+
|
|
|
102
|
+ data, err := json.Marshal(map[string]string{
|
|
|
103
|
+ "value": "127.0.0.1:80",
|
|
|
104
|
+ })
|
|
|
105
|
+ suite.NoError(err)
|
|
|
106
|
+ suite.NoError(json.Unmarshal(data, testStruct))
|
|
|
107
|
+
|
|
|
108
|
+ suite.EqualValues("127.0.0.1:80", testStruct.Value.Value(nil, 0))
|
|
|
109
|
+ suite.EqualValues("127.0.0.1:80", testStruct.Value.Value(net.ParseIP("10.0.0.10"), 3000))
|
|
|
110
|
+}
|
|
|
111
|
+
|
|
|
112
|
+func TestTypeHostPort(t *testing.T) {
|
|
|
113
|
+ t.Parallel()
|
|
|
114
|
+ suite.Run(t, &TypeHostPortTestSuite{})
|
|
|
115
|
+}
|