-
Notifications
You must be signed in to change notification settings - Fork 8
/
resource_config_test.go
183 lines (167 loc) · 6.52 KB
/
resource_config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2015-present, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
package caddyesi_test
import (
"bytes"
"testing"
"github.com/corestoreio/caddy-esi"
"github.com/corestoreio/errors"
"github.com/stretchr/testify/assert"
)
func TestConfigUnmarshal(t *testing.T) {
t.Parallel()
t.Run("File extension not supported", func(t *testing.T) {
items, err := caddyesi.UnmarshalResourceItems("./testdata/config_01.txt")
assert.True(t, errors.NotSupported.Match(err))
assert.Nil(t, items, "Items should be nil")
})
t.Run("File not found", func(t *testing.T) {
data, err := caddyesi.UnmarshalResourceItems("./testdata/config_99.xml")
assert.True(t, errors.Fatal.Match(err), "%+v", err)
assert.Nil(t, data, "Data should be nil")
})
t.Run("XML unmarshalling failed", func(t *testing.T) {
data, err := caddyesi.UnmarshalResourceItems("./testdata/config_00.xml")
assert.True(t, errors.Fatal.Match(err), "%+v", err)
assert.Nil(t, data, "Data should be nil")
})
t.Run("JSON unmarshalling failed", func(t *testing.T) {
data, err := caddyesi.UnmarshalResourceItems("./testdata/config_00.json")
assert.True(t, errors.Fatal.Match(err), "%+v", err)
assert.Nil(t, data, "Data should be nil")
})
t.Run("Unknown content type", func(t *testing.T) {
data, err := caddyesi.UnmarshalResourceItems("this is a text file")
assert.True(t, errors.NotSupported.Match(err), "%+v", err)
assert.Nil(t, data, "Data should be nil")
})
t.Run("Load XML and JSON which must be equal", func(t *testing.T) {
var want = caddyesi.ResourceItems{
caddyesi.NewResourceItem(
"redis://127.0.0.1:6379/?db=0&max_active=10&max_idle=4",
"redis01",
),
caddyesi.NewResourceItem(
"grpc://127.0.0.1:53044/?pem=../path/to/root.pem",
"grpc01",
),
&caddyesi.ResourceItem{
Alias: "mysql01",
URL: "user:password@tcp(localhost:5555)/dbname?charset=utf8mb4,utf8&tls=skip-verify",
Query: "SELECT `value` FROM tableX WHERE key='?'",
},
caddyesi.NewResourceItem(
// the alias mysql01 got resolved to the correct URL data
"user:password@tcp(localhost:5555)/dbname?charset=utf8mb4,utf8&tls=skip-verify",
"mysql02",
"SELECT `value` FROM tableY WHERE another_key=?",
),
}
xmlI, err := caddyesi.UnmarshalResourceItems("./testdata/config_01.xml")
if err != nil {
t.Fatalf("%+v", err)
}
jsonI, err := caddyesi.UnmarshalResourceItems("./testdata/config_01.json")
if err != nil {
t.Fatalf("%+v", err)
}
assert.Exactly(t, xmlI, jsonI, "Both unmarshalled slices are not equal")
assert.Exactly(t, want, jsonI, "JSON unmarshalling failed")
assert.Exactly(t, want, xmlI)
})
t.Run("Load XML and JSON from string", func(t *testing.T) {
var want = caddyesi.ResourceItems{
&caddyesi.ResourceItem{
Alias: "redis01",
URL: "redis://127.0.0.1:6379/?db=0&max_active=10&max_idle=4",
Query: "",
},
&caddyesi.ResourceItem{
Alias: "mysql01",
URL: "user:password@tcp(localhost:5555)/dbname?charset=utf8mb4,utf8&tls=skip-verify",
Query: "SELECT value FROM tableX WHERE key='?'",
},
}
xmlI, err := caddyesi.UnmarshalResourceItems(`<?xml version="1.0"?>
<items>
<item>
<alias>redis01</alias>
<url><![CDATA[redis://127.0.0.1:6379/?db=0&max_active=10&max_idle=4]]></url>
<!--<query>Unused and hence optional</query>-->
</item>
<item>
<alias>mysql01</alias>
<url><![CDATA[user:password@tcp(localhost:5555)/dbname?charset=utf8mb4,utf8&tls=skip-verify]]></url>
<query><![CDATA[SELECT value FROM tableX WHERE key='?']]></query>
</item>
</items>
`)
if err != nil {
t.Fatalf("%+v", err)
}
jsonI, err := caddyesi.UnmarshalResourceItems(`[
{
"alias": "redis01",
"url": "redis://127.0.0.1:6379/?db=0&max_active=10&max_idle=4"
},
{
"alias": "mysql01",
"url": "user:password@tcp(localhost:5555)/dbname?charset=utf8mb4,utf8&tls=skip-verify",
"query": "SELECT value FROM tableX WHERE key='?'"
}
]
`)
if err != nil {
t.Fatalf("%+v", err)
}
assert.Exactly(t, xmlI, jsonI, "Both unmarshalled slices are not equal")
assert.Exactly(t, want, jsonI, "JSON unmarshalling failed")
assert.Exactly(t, want, xmlI)
})
t.Run("ResourceItems to XML String", func(t *testing.T) {
items := caddyesi.ResourceItems{
&caddyesi.ResourceItem{
Alias: "redis01",
URL: "redis://127.0.0.1:6379/?db=0&max_active=10&max_idle=4",
},
&caddyesi.ResourceItem{
Alias: "grpc01",
URL: "grpc://127.0.0.1:53044/?pem=../path/to/root.pem",
},
&caddyesi.ResourceItem{
Alias: "mysql01",
URL: "user:password@tcp(localhost:5555)/dbname?charset=utf8mb4,utf8&tls=skip-verify",
Query: "SELECT `value` FROM tableX WHERE key='?'",
},
&caddyesi.ResourceItem{
Alias: "mysql02",
// the alias mysql-1 got resolved to the correct URL data
URL: "user:password@tcp(localhost:5555)/dbname?charset=utf8mb4,utf8&tls=skip-verify",
Query: "SELECT `value` FROM tableY WHERE another_key<>?",
},
}
const wantXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><items><item><alias>redis01</alias><url>redis://127.0.0.1:6379/?db=0&max_active=10&max_idle=4</url></item><item><alias>grpc01</alias><url>grpc://127.0.0.1:53044/?pem=../path/to/root.pem</url></item><item><alias>mysql01</alias><url>user:password@tcp(localhost:5555)/dbname?charset=utf8mb4,utf8&tls=skip-verify</url><query>SELECT `value` FROM tableX WHERE key='?'</query></item><item><alias>mysql02</alias><url>user:password@tcp(localhost:5555)/dbname?charset=utf8mb4,utf8&tls=skip-verify</url><query>SELECT `value` FROM tableY WHERE another_key<>?</query></item></items>"
haveXML := items.MustToXML()
assert.Exactly(t, wantXML, haveXML)
var buf bytes.Buffer
written, err := items.WriteTo(&buf)
assert.Exactly(t, int64(0), written)
assert.NoError(t, err, "%+v", err)
items2, err := caddyesi.UnmarshalResourceItems(haveXML)
if err != nil {
t.Fatalf("%+v", err)
}
assert.Exactly(t, items, items2, "Encoded items should be equal to decoded items2")
})
}