-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
96 lines (84 loc) · 2.56 KB
/
client_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
package opensrs
import (
"bytes"
"testing"
)
func testSerializeMap(t *testing.T, nsm NestedStringMap, ctrl string) {
var buf bytes.Buffer
serializeMap(nsm, &buf)
if ctrl != buf.String() {
t.Logf("Got: \n%s\n Expected: \n%s\n", buf.String(), ctrl)
t.Fail()
}
}
func TestSerialiseNSM(t *testing.T) {
nsm := NestedStringMap{
"foo": "bar",
}
ctrl := `<dt_assoc><item key="foo">bar</item></dt_assoc>`
testSerializeMap(t, nsm, ctrl)
nsm = NestedStringMap{
"blort": NestedStringMap{
"foo": "bar",
},
}
ctrl = `<dt_assoc><item key="blort"><dt_assoc><item key="foo">bar</item></dt_assoc></item></dt_assoc>`
testSerializeMap(t, nsm, ctrl)
nsm = NestedStringMap{
"blort": []NestedStringMap{
NestedStringMap{
"foo": "bar",
},
},
}
ctrl = `<dt_assoc><item key="blort"><dt_array><item key="0"><dt_assoc><item key="foo">bar</item></dt_assoc></item></dt_array></item></dt_assoc>`
testSerializeMap(t, nsm, ctrl)
nsm = NestedStringMap{
"blort": []string{
"fish",
"soup",
},
}
ctrl = `<dt_assoc><item key="blort"><dt_array><item key="0">fish</item><item key="1">soup</item></dt_array></item></dt_assoc>`
testSerializeMap(t, nsm, ctrl)
}
func TestDeserialiseNSMSimple(t *testing.T) {
c := XCPClient{}
xml := `<dt_assoc><item key="foo">bar</item><item key="baz">37</item></dt_assoc>`
nsm, err := c.xmlResponseToNSM(bytes.NewBufferString(xml))
if err != nil {
t.Error("Error decoding XML" + err.Error())
}
if v, ok := nsm.getString("foo"); !ok || v != "bar" {
t.Error("Error getting string 'foo'")
}
if _, ok := nsm.getString("fish"); ok {
t.Error("String 'fish' unexpectedly present")
}
if v, ok := nsm.getInteger("baz"); !ok || v != 37 {
t.Error("Error getting int 'baz'")
}
}
func TestDeserialiseNSMNested(t *testing.T) {
c := XCPClient{}
xml := `<dt_assoc>`
xml += `<item key="blort"><dt_assoc><item key="foo">bar</item></dt_assoc></item>`
xml += `<item key="wibble"><dt_array><item key="0">FirstItem</item><item key="1">SecondItem</item></dt_array></item>`
xml += `</dt_assoc>`
nsm, err := c.xmlResponseToNSM(bytes.NewBufferString(xml))
if err != nil {
t.Error("Error decoding XML" + err.Error())
}
if v, ok := nsm.getString("blort/foo"); !ok || v != "bar" {
t.Error("Error getting string 'blort/foo'")
}
if _, ok := nsm.getString("blort/fish"); ok {
t.Error("String 'fish' unexpectedly present")
}
if v, ok := nsm.getString("wibble/0"); !ok || v != "FirstItem" {
t.Error("Error getting string 'wibble/0'")
}
if v, ok := nsm.getString("wibble/1"); !ok || v != "SecondItem" {
t.Error("Error getting string 'wibble/1'")
}
}