-
Notifications
You must be signed in to change notification settings - Fork 13
/
envelope_test.go
293 lines (277 loc) · 7.9 KB
/
envelope_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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package soap
import (
"bytes"
"encoding/xml"
"reflect"
"testing"
)
var envelopeName = xml.Name{
Space: soapEnvNS,
Local: "Envelope",
}
var bodyName = xml.Name{
Space: soapEnvNS,
Local: "Body",
}
type headerExample struct {
XMLName xml.Name `xml:"HeaderExample"`
Attr1 int32 `xml:"attr1,attr"`
Value string `xml:",chardata"`
}
type envelopeExampleField struct {
XMLName xml.Name `xml:"ContentField"`
Attr1 string `xml:"attr1,attr"`
Attr2 int32 `xml:"attr2,attr"`
Value string `xml:",chardata"`
}
type envelopeContentExample struct {
XMLName xml.Name `xml:"ContentExample"`
Attr1 int32 `xml:"attr1,attr"`
Field1 envelopeExampleField `xml:"ContentField"`
}
type envelopeEncodeTest struct {
headers []headerExample
contentPtr interface{}
res string
err error
}
var envelopeEncodeTests = []envelopeEncodeTest{
{
contentPtr: &envelopeContentExample{
XMLName: xml.Name{Local: "ContentExample"},
Attr1: 10,
Field1: envelopeExampleField{
XMLName: xml.Name{Local: "ContentField"},
Attr1: "test attr",
Attr2: 11,
Value: "This is a test string",
},
},
res: `<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body xmlns="http://schemas.xmlsoap.org/soap/envelope/"><ContentExample attr1="10"><ContentField attr1="test attr" attr2="11">This is a test string</ContentField></ContentExample></Body></Envelope>`,
},
{
contentPtr: &envelopeContentExample{
XMLName: xml.Name{Local: "ContentExample"},
Attr1: 10,
Field1: envelopeExampleField{
XMLName: xml.Name{Local: "ContentField"},
Attr1: "test attr",
Attr2: 11,
Value: "This is a test string",
},
},
headers: []headerExample{
{
Attr1: 15,
Value: "test header value",
},
},
res: `<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Header xmlns="http://schemas.xmlsoap.org/soap/envelope/"><HeaderExample attr1="15">test header value</HeaderExample></Header><Body xmlns="http://schemas.xmlsoap.org/soap/envelope/"><ContentExample attr1="10"><ContentField attr1="test attr" attr2="11">This is a test string</ContentField></ContentExample></Body></Envelope>`,
},
}
func TestEnvelopeEncode(t *testing.T) {
for i, tt := range envelopeEncodeTests {
val := NewEnvelope(tt.contentPtr)
if len(tt.headers) > 0 {
val.AddHeaders(tt.headers)
}
res := new(bytes.Buffer)
enc := xml.NewEncoder(res)
if err := enc.Encode(val); !reflect.DeepEqual(err, tt.err) {
t.Errorf("#%d: %v, want %v", i, err, tt.err)
continue
} else if err != nil {
continue
}
if tt.res != res.String() {
t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v", i, res.String(), tt.res)
continue
}
}
}
type envelopeDecodeTest struct {
in string
contentPtr interface{}
faultPtr interface{}
out interface{}
err error
}
var envelopeDecodeTests = []envelopeDecodeTest{
{
in: `<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ContentExample attr1="10">
<ContentField attr1="test attr" attr2="11">This is a test content string</ContentField>
</ContentExample>
</soap:Body>
</soap:Envelope>`,
contentPtr: &envelopeContentExample{},
out: &Envelope{
XMLName: envelopeName,
Body: &Body{
XMLName: bodyName,
Content: &envelopeContentExample{
Attr1: 10,
Field1: envelopeExampleField{
Attr1: "test attr",
Attr2: 11,
Value: "This is a test content string",
},
},
},
},
},
{
in: `<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>FaultCodeValue</faultcode>
<faultstring>FaultStringValue</faultstring>
<faultactor>FaultActorValue</faultactor>
<detail>
<DetailExample attr1="10">
<DetailField attr1="test" attr2="11">This is a test string</DetailField>
</DetailExample>
</detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>`,
contentPtr: &envelopeContentExample{},
faultPtr: &faultDetailExample{},
out: &Envelope{
XMLName: envelopeName,
Body: &Body{
XMLName: bodyName,
Fault: &Fault{
XMLName: faultName,
Code: "FaultCodeValue",
String: "FaultStringValue",
Actor: "FaultActorValue",
DetailInternal: &faultDetail{
Content: &faultDetailExample{
XMLName: xml.Name{"", "DetailInternal"},
Attr1: 10,
Field1: faultDetailExampleField{
XMLName: xml.Name{"", "DetailField"},
Attr1: "test",
Attr2: 11,
Value: "This is a test string",
},
},
},
},
},
},
},
{
in: `<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>FaultCodeValue</faultcode>
<faultstring>FaultStringValue</faultstring>
<faultactor>FaultActorValue</faultactor>
</soap:Fault>
</soap:Body>
</soap:Envelope>`,
contentPtr: &envelopeContentExample{},
out: &Envelope{
XMLName: envelopeName,
Body: &Body{
XMLName: bodyName,
Fault: &Fault{
XMLName: faultName,
Code: "FaultCodeValue",
String: "FaultStringValue",
Actor: "FaultActorValue",
},
},
},
},
{
in: `<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ContentExample attr1="10">
<ContentField attr1="test attr" attr2="11">This is a test content string</ContentField>
</soap:Body>
</ContentExample>
</soap:Envelope>`,
contentPtr: &envelopeContentExample{},
out: nil,
err: &xml.SyntaxError{Msg: "element <ContentExample> closed by </Body>", Line: 6},
},
{
in: `<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ContentExample attr1="10">
<ContentField attr1="test attr", attr2="11">This is a test content string</ContentField>
</ContentExample>
</soap:Body>
</soap:Envelope>`,
contentPtr: &envelopeContentExample{},
out: nil,
err: &xml.SyntaxError{Msg: "expected attribute name in element", Line: 5},
},
{
in: `<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>FaultCodeValue</faultcode
<faultstring>FaultStringValue</faultstring>
<faultactor>FaultActorValue</faultactor>
<detail>
<DetailExample attr1="10">
<DetailField attr1="test" attr2="11">This is a test string</DetailField>
</DetailExample>
</detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>`,
contentPtr: &envelopeContentExample{},
out: nil,
err: &xml.SyntaxError{Msg: "invalid characters between </faultcode and >", Line: 6},
},
{
in: `<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ContentExample attr1="10">
<ContentField attr1="test attr", attr2="11">This is a test content string</ContentField>
</ContentExample>
</soap:Body>
</soap:Envelope>`,
contentPtr: nil,
out: nil,
err: ErrEnvelopeMisconfigured,
},
}
func TestEnvelopeDecode(t *testing.T) {
for i, tt := range envelopeDecodeTests {
var val *Envelope
if tt.faultPtr != nil {
val = NewEnvelopeWithFault(tt.contentPtr, tt.faultPtr)
} else {
val = NewEnvelope(tt.contentPtr)
}
dec := xml.NewDecoder(bytes.NewReader([]byte(tt.in)))
if err := dec.Decode(val); !reflect.DeepEqual(err, tt.err) {
t.Errorf("#%d: %v, want %v", i, err, tt.err)
continue
} else if err != nil {
continue
}
valStr, _ := xml.Marshal(val)
outStr, _ := xml.Marshal(tt.out)
if string(valStr) != string(outStr) {
t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v", i, val, tt.out)
println(string(valStr))
println(string(outStr))
continue
}
}
}