This repository has been archived by the owner on Apr 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon_test.go
61 lines (53 loc) · 2.04 KB
/
common_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
// Copyright 2009 The Ben Olive. All rights reserved.
// Use of this source code is governed by a BSD style
// license that can be found in the LICENSE file.
package xmlrpc_test
import (
. "xmlrpc"
"testing"
"strings"
"bytes"
)
type comp func(ParamValue, ParamValue) bool
func defaultCompare(a, b ParamValue) bool { return a == b }
func arrayCompare(a, b ParamValue) bool {
ap, ok := a.(Base64Value)
if !ok {
return false
}
bp, ok := b.(Base64Value)
if !ok {
return false
}
return string(ap) == string(bp)
}
func TestSimpleXMLReader(t *testing.T) {
runTest("<int>4</int>", IntValue(4), defaultCompare, t)
runTest("<string>Ben</string>", StringValue("Ben"), defaultCompare, t)
runTest("<boolean>1</boolean>", BooleanValue(true), defaultCompare, t)
runTest("<double>3.14</double>", DoubleValue(3.14), defaultCompare, t)
runTest("<base64>SGVsbG8gV29ybGQ=</base64>", Base64Value(strings.Bytes("Hello World")), arrayCompare, t)
//runTest("<?xml version=\"1.0\"?>\n<array> \n\t<data>\n\t\t<value><base64>SGVsbG8gV29ybGQ=</base64></value>\n\t</data>\n</array>", Base64Value(strings.Bytes("Hello World")), arrayCompare, t)
//runTest("<?xml version=\"1.0\"?>\n<struct>\n\t<member>\t<name>Something</name><value><array> \n\t<data>\n\t\t<value><base64>SGVsbG8gV29ybGQ=</base64></value>\n\t</data>\n</array></value></member></struct>", Base64Value(strings.Bytes("Hello World")), arrayCompare, t)
}
func TestResponse(t *testing.T) {
r := `<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value><string>South Dakota</string></value>
</param>
</params>
</methodResponse>`
resp, _ := ReadResponse(bytes.NewBufferString(r))
defaultCompare(resp.Value, StringValue("South Dakota"))
}
func runTest(xml string, value ParamValue, compare comp, t *testing.T) {
m, err := ParseMessage(strings.NewReader(xml))
if err != nil {
t.Errorf("Could not parse (%s): %v", xml, err)
}
if !compare(m, value) {
t.Errorf("Unexpected value: %v", m)
}
}