|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+package config_test
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "encoding/json"
|
|
|
5
|
+ "testing"
|
|
|
6
|
+
|
|
|
7
|
+ "github.com/9seconds/mtg/v2/config"
|
|
|
8
|
+ "github.com/stretchr/testify/assert"
|
|
|
9
|
+ "github.com/stretchr/testify/suite"
|
|
|
10
|
+)
|
|
|
11
|
+
|
|
|
12
|
+type typeHTTPPathTestStruct struct {
|
|
|
13
|
+ Value config.TypeHTTPPath `json:"value"`
|
|
|
14
|
+}
|
|
|
15
|
+
|
|
|
16
|
+type TypeHTTPPathTestSuite struct {
|
|
|
17
|
+ suite.Suite
|
|
|
18
|
+}
|
|
|
19
|
+
|
|
|
20
|
+func (suite *TypeHTTPPathTestSuite) TestUnmarshal() {
|
|
|
21
|
+ testData := []string{
|
|
|
22
|
+ "/hello",
|
|
|
23
|
+ "hello",
|
|
|
24
|
+ "hello/",
|
|
|
25
|
+ "/hello/",
|
|
|
26
|
+ }
|
|
|
27
|
+
|
|
|
28
|
+ for _, v := range testData {
|
|
|
29
|
+ data, err := json.Marshal(map[string]string{
|
|
|
30
|
+ "value": v,
|
|
|
31
|
+ })
|
|
|
32
|
+ suite.NoError(err)
|
|
|
33
|
+
|
|
|
34
|
+ suite.T().Run(v, func(t *testing.T) {
|
|
|
35
|
+ testStruct := &typeHTTPPathTestStruct{}
|
|
|
36
|
+
|
|
|
37
|
+ assert.NoError(t, json.Unmarshal(data, testStruct))
|
|
|
38
|
+ assert.Equal(t, "/hello", testStruct.Value.Value(""))
|
|
|
39
|
+ })
|
|
|
40
|
+ }
|
|
|
41
|
+}
|
|
|
42
|
+
|
|
|
43
|
+func (suite *TypeHTTPPathTestSuite) TestMarshalOk() {
|
|
|
44
|
+ testData := map[string]string{
|
|
|
45
|
+ "": "/",
|
|
|
46
|
+ "/hello": "/hello",
|
|
|
47
|
+ "/hello/": "/hello",
|
|
|
48
|
+ "hello/": "/hello",
|
|
|
49
|
+ "hello": "/hello",
|
|
|
50
|
+ }
|
|
|
51
|
+
|
|
|
52
|
+ for k, v := range testData {
|
|
|
53
|
+ toPass := k
|
|
|
54
|
+ compareWith := v
|
|
|
55
|
+
|
|
|
56
|
+ data, err := json.Marshal(map[string]string{
|
|
|
57
|
+ "value": toPass,
|
|
|
58
|
+ })
|
|
|
59
|
+ suite.NoError(err)
|
|
|
60
|
+
|
|
|
61
|
+ suite.T().Run(toPass, func(t *testing.T) {
|
|
|
62
|
+ testStruct := &typeHTTPPathTestStruct{}
|
|
|
63
|
+
|
|
|
64
|
+ assert.NoError(t, json.Unmarshal(data, testStruct))
|
|
|
65
|
+ assert.Equal(t, compareWith, testStruct.Value.String())
|
|
|
66
|
+
|
|
|
67
|
+ marshalled, err := testStruct.Value.MarshalText()
|
|
|
68
|
+ assert.NoError(t, err)
|
|
|
69
|
+ assert.Equal(t, compareWith, string(marshalled))
|
|
|
70
|
+ })
|
|
|
71
|
+ }
|
|
|
72
|
+}
|
|
|
73
|
+
|
|
|
74
|
+func (suite *TypeHTTPPathTestSuite) TestValue() {
|
|
|
75
|
+ testStruct := &typeHTTPPathTestStruct{}
|
|
|
76
|
+
|
|
|
77
|
+ suite.Equal("/hello", testStruct.Value.Value("/hello"))
|
|
|
78
|
+
|
|
|
79
|
+ data, err := json.Marshal(map[string]string{
|
|
|
80
|
+ "value": "/map",
|
|
|
81
|
+ })
|
|
|
82
|
+ suite.NoError(err)
|
|
|
83
|
+ suite.NoError(json.Unmarshal(data, testStruct))
|
|
|
84
|
+
|
|
|
85
|
+ suite.Equal("/map", testStruct.Value.Value("/hello"))
|
|
|
86
|
+}
|
|
|
87
|
+
|
|
|
88
|
+func TestTypeHTTPPath(t *testing.T) {
|
|
|
89
|
+ t.Parallel()
|
|
|
90
|
+ suite.Run(t, &TypeHTTPPathTestSuite{})
|
|
|
91
|
+}
|