github.com/gogf/gf/v2@v2.7.4/encoding/gxml/gxml_z_unit_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gxml_test
     8  
     9  import (
    10  	"bytes"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/gogf/gf/v2/encoding/gcharset"
    15  	"github.com/gogf/gf/v2/encoding/gjson"
    16  	"github.com/gogf/gf/v2/encoding/gxml"
    17  	"github.com/gogf/gf/v2/test/gtest"
    18  )
    19  
    20  var testData = []struct {
    21  	utf8, other, otherEncoding string
    22  }{
    23  	{"Hello 常用國字標準字體表", "Hello \xb1`\xa5\u03b0\xea\xa6r\xbc\u0437\u01e6r\xc5\xe9\xaa\xed", "big5"},
    24  	{"Hello 常用國字標準字體表", "Hello \xb3\xa3\xd3\xc3\x87\xf8\xd7\xd6\x98\xcb\x9c\xca\xd7\xd6\xf3\x77\xb1\xed", "gbk"},
    25  	{"Hello 常用國字標準字體表", "Hello \xb3\xa3\xd3\xc3\x87\xf8\xd7\xd6\x98\xcb\x9c\xca\xd7\xd6\xf3\x77\xb1\xed", "gb18030"},
    26  }
    27  
    28  var testErrData = []struct {
    29  	utf8, other, otherEncoding string
    30  }{
    31  	{"Hello 常用國字標準字體表", "Hello \xb3\xa3\xd3\xc3\x87\xf8\xd7\xd6\x98\xcb\x9c\xca\xd7\xd6\xf3\x77\xb1\xed", "gbk"},
    32  }
    33  
    34  func buildXml(charset string, str string) (string, string) {
    35  	head := `<?xml version="1.0" encoding="UTF-8"?>`
    36  	srcXml := strings.Replace(head, "UTF-8", charset, -1)
    37  
    38  	srcParser := gjson.New(nil)
    39  	srcParser.Set("name", str)
    40  	srcParser.Set("age", "12")
    41  
    42  	s, err := srcParser.ToXml()
    43  	if err != nil {
    44  		return "", ""
    45  	}
    46  
    47  	srcXml = srcXml + string(s)
    48  	srcXml, err = gcharset.UTF8To(charset, srcXml)
    49  	if err != nil {
    50  		return "", ""
    51  	}
    52  
    53  	dstXml := head + string(s)
    54  
    55  	return srcXml, dstXml
    56  }
    57  
    58  // 测试XML中字符集的转换
    59  func Test_XmlToJson(t *testing.T) {
    60  	for _, v := range testData {
    61  		srcXml, dstXml := buildXml(v.otherEncoding, v.utf8)
    62  		if len(srcXml) == 0 && len(dstXml) == 0 {
    63  			t.Errorf("build xml string error. srcEncoding:%s, src:%s, utf8:%s", v.otherEncoding, v.other, v.utf8)
    64  		}
    65  
    66  		srcJson, err := gxml.ToJson([]byte(srcXml))
    67  		if err != nil {
    68  			t.Errorf("gxml.ToJson error. %s", srcXml)
    69  		}
    70  
    71  		dstJson, err := gxml.ToJson([]byte(dstXml))
    72  		if err != nil {
    73  			t.Errorf("dstXml to json error. %s", dstXml)
    74  		}
    75  
    76  		if !bytes.Equal(srcJson, dstJson) {
    77  			t.Errorf("convert to json error. srcJson:%s, dstJson:%s", string(srcJson), string(dstJson))
    78  		}
    79  
    80  	}
    81  }
    82  
    83  func Test_Decode1(t *testing.T) {
    84  	for _, v := range testData {
    85  		srcXml, dstXml := buildXml(v.otherEncoding, v.utf8)
    86  		if len(srcXml) == 0 && len(dstXml) == 0 {
    87  			t.Errorf("build xml string error. srcEncoding:%s, src:%s, utf8:%s", v.otherEncoding, v.other, v.utf8)
    88  		}
    89  
    90  		srcMap, err := gxml.Decode([]byte(srcXml))
    91  		if err != nil {
    92  			t.Errorf("gxml.Decode error. %s", srcXml)
    93  		}
    94  
    95  		dstMap, err := gxml.Decode([]byte(dstXml))
    96  		if err != nil {
    97  			t.Errorf("gxml decode error. %s", dstXml)
    98  		}
    99  		s := srcMap["doc"].(map[string]interface{})
   100  		d := dstMap["doc"].(map[string]interface{})
   101  		for kk, vv := range s {
   102  			if vv.(string) != d[kk].(string) {
   103  				t.Errorf("convert to map error. src:%v, dst:%v", vv, d[kk])
   104  			}
   105  		}
   106  	}
   107  }
   108  
   109  func Test_Decode2(t *testing.T) {
   110  	gtest.C(t, func(t *gtest.T) {
   111  		content := `
   112  <?xml version="1.0" encoding="UTF-8"?><doc><username>johngcn</username><password1>123456</password1><password2>123456</password2></doc>
   113  `
   114  		m, err := gxml.Decode([]byte(content))
   115  		t.AssertNil(err)
   116  		t.Assert(m["doc"].(map[string]interface{})["username"], "johngcn")
   117  		t.Assert(m["doc"].(map[string]interface{})["password1"], "123456")
   118  		t.Assert(m["doc"].(map[string]interface{})["password2"], "123456")
   119  	})
   120  }
   121  
   122  func Test_DecodeWitoutRoot(t *testing.T) {
   123  	gtest.C(t, func(t *gtest.T) {
   124  		content := `
   125  <?xml version="1.0" encoding="UTF-8"?><doc><username>johngcn</username><password1>123456</password1><password2>123456</password2></doc>
   126  `
   127  		m, err := gxml.DecodeWithoutRoot([]byte(content))
   128  		t.AssertNil(err)
   129  		t.Assert(m["username"], "johngcn")
   130  		t.Assert(m["password1"], "123456")
   131  		t.Assert(m["password2"], "123456")
   132  	})
   133  }
   134  
   135  func Test_Encode(t *testing.T) {
   136  	m := make(map[string]interface{})
   137  	v := map[string]interface{}{
   138  		"string": "hello world",
   139  		"int":    123,
   140  		"float":  100.92,
   141  		"bool":   true,
   142  	}
   143  	m["root"] = interface{}(v)
   144  
   145  	xmlStr, err := gxml.Encode(m)
   146  	if err != nil {
   147  		t.Errorf("encode error.")
   148  	}
   149  	// t.Logf("%s\n", string(xmlStr))
   150  
   151  	res := `<root><bool>true</bool><float>100.92</float><int>123</int><string>hello world</string></root>`
   152  	if string(xmlStr) != res {
   153  		t.Errorf("encode error. result: [%s], expect:[%s]", string(xmlStr), res)
   154  	}
   155  }
   156  
   157  func Test_EncodeIndent(t *testing.T) {
   158  	m := make(map[string]interface{})
   159  	v := map[string]interface{}{
   160  		"string": "hello world",
   161  		"int":    123,
   162  		"float":  100.92,
   163  		"bool":   true,
   164  	}
   165  	m["root"] = interface{}(v)
   166  
   167  	_, err := gxml.EncodeWithIndent(m, "xml")
   168  	if err != nil {
   169  		t.Errorf("encodeWithIndent error.")
   170  	}
   171  
   172  	// t.Logf("%s\n", string(xmlStr))
   173  
   174  }
   175  
   176  func TestErrXml(t *testing.T) {
   177  	for _, v := range testErrData {
   178  		srcXml, dstXml := buildXml(v.otherEncoding, v.utf8)
   179  		if len(srcXml) == 0 && len(dstXml) == 0 {
   180  			t.Errorf("build xml string error. srcEncoding:%s, src:%s, utf8:%s", v.otherEncoding, v.other, v.utf8)
   181  		}
   182  
   183  		srcXml = strings.Replace(srcXml, "gbk", "XXX", -1)
   184  		_, err := gxml.ToJson([]byte(srcXml))
   185  		if err == nil {
   186  			t.Errorf("srcXml to json should be failed. %s", srcXml)
   187  		}
   188  
   189  	}
   190  }
   191  
   192  func TestErrCase(t *testing.T) {
   193  	gtest.C(t, func(t *gtest.T) {
   194  		errXml := `<root><bool>true</bool><float>100.92</float><int>123</int><string>hello world</string>`
   195  		_, err := gxml.ToJson([]byte(errXml))
   196  		if err == nil {
   197  			t.Errorf("unexpected value: nil")
   198  		}
   199  	})
   200  
   201  	gtest.C(t, func(t *gtest.T) {
   202  		errXml := `<root><bool>true</bool><float>100.92</float><int>123</int><string>hello world</string>`
   203  		_, err := gxml.Decode([]byte(errXml))
   204  		if err == nil {
   205  			t.Errorf("unexpected value: nil")
   206  		}
   207  	})
   208  }
   209  
   210  func Test_Issue3716(t *testing.T) {
   211  	gtest.C(t, func(t *gtest.T) {
   212  		var (
   213  			xml = `<Person><Bio>I am a software developer &amp; I love coding.</Bio><Email>john.doe@example.com</Email><Name>&lt;&gt;&amp;&apos;&quot;AAA</Name></Person>`
   214  			m   = map[string]interface{}{
   215  				"Person": map[string]interface{}{
   216  					"Name":  "<>&'\"AAA",
   217  					"Email": "john.doe@example.com",
   218  					"Bio":   "I am a software developer & I love coding.",
   219  				},
   220  			}
   221  		)
   222  		gxml.XMLEscapeChars(true)
   223  		defer gxml.XMLEscapeChars(false)
   224  
   225  		xb, err := gxml.Encode(m)
   226  		t.AssertNil(err)
   227  		t.Assert(string(xb), xml)
   228  
   229  		dm, err := gxml.Decode(xb)
   230  		t.AssertNil(err)
   231  		t.Assert(dm, m)
   232  	})
   233  }