Просмотр исходного кода

Add tests for hostport type

tags/v2.0.0-rc1
9seconds 5 лет назад
Родитель
Сommit
dc81740bda
2 измененных файлов: 128 добавлений и 2 удалений
  1. 13
    2
      config/type_hostport.go
  2. 115
    0
      config/type_hostport_test.go

+ 13
- 2
config/type_hostport.go Просмотреть файл

@@ -16,11 +16,17 @@ func (c *TypeHostPort) UnmarshalText(data []byte) error {
16 16
 		return nil
17 17
 	}
18 18
 
19
-	host, port, err := net.SplitHostPort(string(data))
19
+	text := string(data)
20
+
21
+	host, port, err := net.SplitHostPort(text)
20 22
 	if err != nil {
21 23
 		return fmt.Errorf("incorrect host:port syntax: %w", err)
22 24
 	}
23 25
 
26
+	if port == "" {
27
+		return fmt.Errorf("port in %s host:port pair cannot be empty", text)
28
+	}
29
+
24 30
 	if err := c.port.UnmarshalJSON([]byte(port)); err != nil {
25 31
 		return fmt.Errorf("incorrect port in host:port: %w", err)
26 32
 	}
@@ -52,5 +58,10 @@ func (c TypeHostPort) Value(defaultHostValue net.IP, defaultPortValue uint) stri
52 58
 	host := c.HostValue(defaultHostValue)
53 59
 	port := c.PortValue(defaultPortValue)
54 60
 
55
-	return net.JoinHostPort(host.String(), strconv.Itoa(int(port)))
61
+	hostStr := ""
62
+	if len(host) > 0 {
63
+		hostStr = host.String()
64
+	}
65
+
66
+	return net.JoinHostPort(hostStr, strconv.Itoa(int(port)))
56 67
 }

+ 115
- 0
config/type_hostport_test.go Просмотреть файл

@@ -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
+}

Загрузка…
Отмена
Сохранить