forked from basgys/goxml2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter_test.go
202 lines (184 loc) · 5.69 KB
/
converter_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package xml2json_test
import (
"strings"
"testing"
"github.com/stretchr/testify/suite"
xml2json "github.com/txix-open/goxml2json"
)
func TestConverter_Suite(t *testing.T) {
t.Parallel()
suite.Run(t, &TestConverter{})
}
type TestConverter struct {
suite.Suite
converter xml2json.Converter
}
func (t *TestConverter) SetupSuite() {
t.converter = xml2json.NewConverter(
xml2json.WithAttrPrefix("-"),
xml2json.WithContentPrefix("#"),
xml2json.AllAttrToArray(),
)
}
// TestConvert ensures that the whole process works correctly
// It takes an XML document and outputs a JSON document
func (t *TestConverter) TestConvert() {
s := `<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="CGImap 0.0.2">
<bounds minlat="54.0889580" minlon="12.2487570" maxlat="54.0913900" maxlon="12.2524800"/>
<node id="298884269" lat="54.0901746" lon="12.2482632" user="SvenHRO" uid="46882" visible="true" version="1" changeset="676636" timestamp="2008-09-21T21:37:45Z"/>
<node id="261728686" lat="54.0906309" lon="12.2441924" user="PikoWinter" uid="36744" visible="true" version="1" changeset="323878" timestamp="2008-05-03T13:39:23Z"/>
<node id="1831881213" version="1" changeset="12370172" lat="54.0900666" lon="12.2539381" user="lafkor" uid="75625" visible="true" timestamp="2012-07-20T09:43:19Z">
<tag k="name" v="Neu Broderstorf"/>
<tag k="traffic_sign" v="city_limit"/>
</node>
<foo>bar</foo>
<mixed attr="attribute">
content
</mixed>
</osm>`
expected := []byte(`{
"osm": [{
"-version": ["0.6"],
"-generator": ["CGImap 0.0.2"],
"bounds": [{
"-minlat": ["54.0889580"],
"-minlon": ["12.2487570"],
"-maxlat": ["54.0913900"],
"-maxlon": ["12.2524800"]
}],
"node": [
{
"-id": ["298884269"],
"-lat": ["54.0901746"],
"-lon": ["12.2482632"],
"-user": ["SvenHRO"],
"-uid": ["46882"],
"-visible": ["true"],
"-version": ["1"],
"-changeset": ["676636"],
"-timestamp": ["2008-09-21T21:37:45Z"]
},
{
"-id": ["261728686"],
"-lat": ["54.0906309"],
"-lon": ["12.2441924"],
"-user": ["PikoWinter"],
"-uid": ["36744"],
"-visible": ["true"],
"-version": ["1"],
"-changeset": ["323878"],
"-timestamp": ["2008-05-03T13:39:23Z"]
},
{
"-id": ["1831881213"],
"-version": ["1"],
"-changeset": ["12370172"],
"-lat": ["54.0900666"],
"-lon": ["12.2539381"],
"-user": ["lafkor"],
"-uid": ["75625"],
"-visible": ["true"],
"-timestamp": ["2012-07-20T09:43:19Z"],
"tag": [
{
"-k": ["name"],
"-v": ["Neu Broderstorf"]
},
{
"-k": ["traffic_sign"],
"-v": ["city_limit"]
}
]
}
],
"foo": ["bar"],
"mixed": [{
"-attr": ["attribute"],
"#content": ["content"]
}]
}]
}`)
actual, err := t.converter.Convert(strings.NewReader(s))
t.NoError(err)
t.JSONEq(string(expected), actual.String())
}
func (t *TestConverter) TestConvertWithNewLines() {
s := `<?xml version="1.0" encoding="UTF-8"?>
<osm>
<foo>
foo
bar
</foo>
</osm>`
expected := []byte(`{
"osm": [{
"foo": ["foo\n\n\t\tbar"]
}]
}`)
actual, err := t.converter.Convert(strings.NewReader(s))
t.NoError(err)
t.JSONEq(string(expected), actual.String())
}
func (t *TestConverter) TestConvertWithMixedTags() {
s := `<?xml version="1.0" encoding="UTF-8"?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header>
<wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext">
<wsse:BinarySecurityToken valueType="String" EncodingType="wsse:Base64Binary">
Shared/IDL:IceSess\/SessMgr:1\.0.IDL/Common/!ICESMS\/ACPCRTC!ICESMSLB\/CRT.LB!-3379045898978075261!1563026!0
</wsse:BinarySecurityToken>
</wsse:Security>
</soap-env:Header>
</soap-env:Envelope> `
expected := []byte(`
{
"Envelope": [
{
"Header": [
{
"Security": [
{
"-wsse": [
"http://schemas.xmlsoap.org/ws/2002/12/secext"
],
"BinarySecurityToken": [
{
"#content": [
"Shared/IDL:IceSess\\/SessMgr:1\\.0.IDL/Common/!ICESMS\\/ACPCRTC!ICESMSLB\\/CRT.LB!-3379045898978075261!1563026!0"
],
"-EncodingType": [
"wsse:Base64Binary"
],
"-valueType": [
"String"
]
}
]
}
]
}
],
"-soap-env": [
"http://schemas.xmlsoap.org/soap/envelope/"
]
}
]
}
`)
actual, err := t.converter.Convert(strings.NewReader(s))
t.NoError(err)
t.JSONEq(string(expected), actual.String())
}
// TestConvertISO ensures that other charsets can be converted
func (t *TestConverter) TestConvertISO() {
s := []byte{0x3C, 0x3F, 0x78, 0x6D, 0x6C, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3D, 0x22, 0x31, 0x2E, 0x30, 0x22, 0x20, 0x65, 0x6E, 0x63, 0x6F, 0x64, 0x69, 0x6E, 0x67, 0x3D, 0x22, 0x49, 0x53, 0x4F, 0x2D, 0x38, 0x38, 0x35, 0x39, 0x2D, 0x31, 0x22, 0x3F, 0x3E, 0x3C, 0x63, 0x68, 0x61, 0x72, 0x73, 0x65, 0x74, 0x3E, 0xFC, 0x62, 0x65, 0x72, 0x20, 0x63, 0x6F, 0x6D, 0x70, 0x6C, 0x65, 0x78, 0x3C, 0x2F, 0x63, 0x68, 0x61, 0x72, 0x73, 0x65, 0x74, 0x3E}
expected := []byte(`
{
"charset": ["über complex"]
}
`)
actual, err := t.converter.Convert(strings.NewReader(string(s)))
t.NoError(err)
t.JSONEq(string(expected), actual.String())
}