1 package service
2
3 import (
4 "encoding/json"
5 "testing"
6
7 "github.com/alibabacloud-go/tea/tea"
8 "github.com/alibabacloud-go/tea/utils"
9 )
10
11 type validatorTest struct {
12 Num *int `json:"num" require:"true"`
13 Str *string `json:"str" pattern:"^[a-d]*$" maxLength:"4"`
14 Test *errLength `json:"test"`
15 List []*string `json:"list" pattern:"^[a-d]*$" maxLength:"4"`
16 }
17
18 type GetBucketLocationResponse struct {
19 RequestId *string `json:"x-oss-request-id" xml:"x-oss-request-id" require:"true"`
20 LocationConstraint *string `json:"LocationConstraint" xml:"LocationConstraint" require:"true"`
21 }
22
23 type errLength struct {
24 Num *int `json:"num" maxLength:"a"`
25 }
26
27 func Test_ParseXml(t *testing.T) {
28 str := `<?xml version="1.0" encoding="utf-8" standalone="no"?>
29 <num>10</num>`
30 result := ParseXml(tea.String(str), new(validatorTest))
31 utils.AssertEqual(t, 1, len(result))
32 str = `<?xml version="1.0" encoding="utf-8" standalone="no"?>
33 <num></num>`
34 result = ParseXml(tea.String(str), new(validatorTest))
35 utils.AssertEqual(t, 1, len(result))
36 xmlVal := `<?xml version="1.0" encoding="utf-8" standalone="no"?>
37 <students>
38 <student number="1001">
39 <name>zhangSan</name>
40 <age>23</age>
41 <sex>male</sex>
42 </student>
43 </students>`
44 res := ParseXml(tea.String(xmlVal), nil)
45 utils.AssertEqual(t, 1, len(res))
46 }
47
48 func Test_ToXML(t *testing.T) {
49 val := map[string]interface{}{
50 "oss": map[string]interface{}{
51 "key": "value",
52 },
53 }
54 str := ToXML(val)
55 utils.AssertEqual(t, "<oss><key>value</key></oss>", tea.StringValue(str))
56 }
57
58 func Test_getStartElement(t *testing.T) {
59 xmlVal := `<?xml version="1.0" encoding="utf-8" standalone="no"?>
60 <students>
61 <student number="1001">
62 <name>zhangSan</name>
63 <age>23</age>
64 <sex>male</sex>
65 </student>
66 </students>`
67 str := getStartElement([]byte(xmlVal))
68 utils.AssertEqual(t, "students", str)
69
70 xmlVal = `<?xml version="1.0" encoding="utf-8" standalone="no"?>
71 <students/\>
72 <student number="1001">
73 <name>zhangSan</name>
74 <age>23</age>
75 <sex>male</sex>
76 </student>
77 </students>`
78 str = getStartElement([]byte(xmlVal))
79 utils.AssertEqual(t, "", str)
80 }
81
82 func Test_mapToXML(t *testing.T) {
83 obj := map[string]interface{}{
84 "struct": map[string]interface{}{
85 "param1": "value1",
86 "list": []string{"value2", "value3"},
87 "listMap": []map[string]interface{}{
88 map[string]interface{}{
89 "param2": "value2",
90 },
91 map[string]interface{}{
92 "param3": "value3",
93 },
94 },
95 "listMapString": []map[string]string{
96 map[string]string{
97 "param4": "value4",
98 },
99 map[string]string{
100 "param5": "value5",
101 },
102 },
103 "mapString": map[string]string{
104 "param6": "value6",
105 },
106 "listInterface": []interface{}{"10", 20},
107 },
108 }
109 byt, _ := json.Marshal(obj)
110 obj1 := make(map[string]interface{})
111 json.Unmarshal(byt, &obj1)
112 xml := mapToXML(obj1)
113 utils.AssertContains(t, xml, `<listInterface>10</listInterface>`)
114 }
115
116 func Test_XmlUnmarshal(t *testing.T) {
117 result := new(GetBucketLocationResponse)
118 xmlVal := `<?xml version="1.0" encoding="UTF-8"?>
119 <LocationConstraint>oss-cn-hangzhou</LocationConstraint >`
120 out, err := xmlUnmarshal([]byte(xmlVal), result)
121 utils.AssertNil(t, err)
122
123 byt, _ := json.Marshal(out)
124 utils.AssertEqual(t, `"oss-cn-hangzhou"`, string(byt))
125 }
126
View as plain text