|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+package config_test
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "encoding/json"
|
|
|
5
|
+ "net/url"
|
|
|
6
|
+ "testing"
|
|
|
7
|
+
|
|
|
8
|
+ "github.com/9seconds/mtg/v2/internal/config"
|
|
|
9
|
+ "github.com/stretchr/testify/assert"
|
|
|
10
|
+ "github.com/stretchr/testify/suite"
|
|
|
11
|
+)
|
|
|
12
|
+
|
|
|
13
|
+type typeHttpsURLTestStruct struct {
|
|
|
14
|
+ Value config.TypeHttpsURL `json:"value"`
|
|
|
15
|
+}
|
|
|
16
|
+
|
|
|
17
|
+type HttpsURLTestSuite struct {
|
|
|
18
|
+ suite.Suite
|
|
|
19
|
+}
|
|
|
20
|
+
|
|
|
21
|
+func (suite *HttpsURLTestSuite) TestUnmarshalFail() {
|
|
|
22
|
+ testData := []string{
|
|
|
23
|
+ "",
|
|
|
24
|
+ "https://",
|
|
|
25
|
+ "://lala",
|
|
|
26
|
+ "/path",
|
|
|
27
|
+ "http://example.com",
|
|
|
28
|
+ "socks5://example.com",
|
|
|
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, &typeHttpsURLTestStruct{}))
|
|
|
39
|
+ })
|
|
|
40
|
+ }
|
|
|
41
|
+}
|
|
|
42
|
+
|
|
|
43
|
+func (suite *HttpsURLTestSuite) TestUnmarshalOk() {
|
|
|
44
|
+ testData := map[string]string{
|
|
|
45
|
+ "https://example.com": "https://example.com",
|
|
|
46
|
+ "https://example.com:8443": "https://example.com:8443",
|
|
|
47
|
+ "https://example.com/path?q=1": "https://example.com/path?q=1",
|
|
|
48
|
+ "https://user:pass@example.com": "https://user:pass@example.com",
|
|
|
49
|
+ }
|
|
|
50
|
+
|
|
|
51
|
+ for k, v := range testData {
|
|
|
52
|
+ value := v
|
|
|
53
|
+
|
|
|
54
|
+ data, err := json.Marshal(map[string]string{
|
|
|
55
|
+ "value": k,
|
|
|
56
|
+ })
|
|
|
57
|
+ suite.NoError(err)
|
|
|
58
|
+
|
|
|
59
|
+ suite.T().Run(k, func(t *testing.T) {
|
|
|
60
|
+ testStruct := &typeHttpsURLTestStruct{}
|
|
|
61
|
+ assert.NoError(t, json.Unmarshal(data, testStruct))
|
|
|
62
|
+
|
|
|
63
|
+ parsed, _ := url.Parse(value)
|
|
|
64
|
+
|
|
|
65
|
+ assert.Equal(t, parsed.Scheme, testStruct.Value.Get(nil).Scheme)
|
|
|
66
|
+ assert.Equal(t, parsed.Host, testStruct.Value.Get(nil).Host)
|
|
|
67
|
+ assert.Equal(t, parsed.RawQuery, testStruct.Value.Get(nil).RawQuery)
|
|
|
68
|
+ assert.Equal(t, parsed.Path, testStruct.Value.Get(nil).Path)
|
|
|
69
|
+ })
|
|
|
70
|
+ }
|
|
|
71
|
+}
|
|
|
72
|
+
|
|
|
73
|
+func (suite *HttpsURLTestSuite) TestMarshalOk() {
|
|
|
74
|
+ parsed, _ := url.Parse("https://example.com/path?q=1")
|
|
|
75
|
+ testStruct := &typeHttpsURLTestStruct{
|
|
|
76
|
+ Value: config.TypeHttpsURL{
|
|
|
77
|
+ Value: parsed,
|
|
|
78
|
+ },
|
|
|
79
|
+ }
|
|
|
80
|
+
|
|
|
81
|
+ encodedJSON, err := json.Marshal(testStruct)
|
|
|
82
|
+ suite.NoError(err)
|
|
|
83
|
+ suite.JSONEq(`{"value": "https://example.com/path?q=1"}`,
|
|
|
84
|
+ string(encodedJSON))
|
|
|
85
|
+}
|
|
|
86
|
+
|
|
|
87
|
+func (suite *HttpsURLTestSuite) TestGet() {
|
|
|
88
|
+ emptyURL := &url.URL{}
|
|
|
89
|
+
|
|
|
90
|
+ value := config.TypeHttpsURL{}
|
|
|
91
|
+ suite.Equal(emptyURL, value.Get(emptyURL))
|
|
|
92
|
+
|
|
|
93
|
+ value.Value = &url.URL{}
|
|
|
94
|
+ suite.Equal(value.Value, value.Get(emptyURL))
|
|
|
95
|
+}
|
|
|
96
|
+
|
|
|
97
|
+func TestTypeHttpsURL(t *testing.T) {
|
|
|
98
|
+ t.Parallel()
|
|
|
99
|
+ suite.Run(t, &HttpsURLTestSuite{})
|
|
|
100
|
+}
|