improve JSON/XML parsing feature for ghttp.Request

This commit is contained in:
John
2020-01-04 15:35:21 +08:00
parent bc53f265af
commit 2bacc77224
8 changed files with 216 additions and 44 deletions

View File

@ -80,7 +80,7 @@ func Test_XmlToJson(t *testing.T) {
}
}
func Test_Decode(t *testing.T) {
func Test_Decode1(t *testing.T) {
for _, v := range testData {
srcXml, dstXml := buildXml(v.otherEncoding, v.utf8)
if len(srcXml) == 0 && len(dstXml) == 0 {
@ -106,6 +106,32 @@ func Test_Decode(t *testing.T) {
}
}
func Test_Decode2(t *testing.T) {
gtest.Case(t, func() {
content := `
<?xml version="1.0" encoding="UTF-8"?><doc><username>johngcn</username><password1>123456</password1><password2>123456</password2></doc>
`
m, err := gxml.Decode([]byte(content))
gtest.Assert(err, nil)
gtest.Assert(m["doc"].(map[string]interface{})["username"], "johngcn")
gtest.Assert(m["doc"].(map[string]interface{})["password1"], "123456")
gtest.Assert(m["doc"].(map[string]interface{})["password2"], "123456")
})
}
func Test_DecodeWitoutRoot(t *testing.T) {
gtest.Case(t, func() {
content := `
<?xml version="1.0" encoding="UTF-8"?><doc><username>johngcn</username><password1>123456</password1><password2>123456</password2></doc>
`
m, err := gxml.DecodeWithoutRoot([]byte(content))
gtest.Assert(err, nil)
gtest.Assert(m["username"], "johngcn")
gtest.Assert(m["password1"], "123456")
gtest.Assert(m["password2"], "123456")
})
}
func Test_Encode(t *testing.T) {
m := make(map[string]interface{})
v := map[string]interface{}{