蛋蛋星球-制度模式
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

104 řádky
2.7 KiB

  1. package gopay
  2. import (
  3. "crypto/sha1"
  4. "encoding/hex"
  5. "encoding/xml"
  6. "testing"
  7. "github.com/go-pay/xlog"
  8. )
  9. func TestBodyMap_CheckParamsNull(t *testing.T) {
  10. bm := make(BodyMap)
  11. bm.Set("name", "jerry")
  12. bm.Set("age", 2)
  13. bm.Set("phone", "123")
  14. bm.Set("pi", 3.1415926)
  15. err := bm.CheckEmptyError("name", "age", "phone")
  16. if err != nil {
  17. xlog.Errorf("bm.CheckEmptyError(),err: %+v", err)
  18. return
  19. }
  20. h := sha1.New()
  21. h.Write([]byte("golang"))
  22. bs := h.Sum(nil)
  23. _signature := hex.EncodeToString(bs)
  24. xlog.Info(_signature) // 771e417b9dcae54aead2f3cbbbff340787bc462f
  25. // 771e417b9dcae54aead2f3cbbbff340787bc462f
  26. }
  27. func TestBodyMap_UnmarshalXML(t *testing.T) {
  28. xmlData := `<xml>
  29. <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
  30. <mch_id><![CDATA[10000100]]></mch_id>
  31. <nonce_str><![CDATA[TeqClE3i0mvn3DrK]]></nonce_str>
  32. <out_refund_no_0><![CDATA[1415701182]]></out_refund_no_0>
  33. <out_trade_no><![CDATA[1415757673]]></out_trade_no>
  34. <refund_count>1</refund_count>
  35. <refund_fee_0>1</refund_fee_0>
  36. <refund_id_0><![CDATA[2008450740201411110000174436]]></refund_id_0>
  37. <refund_status_0><![CDATA[PROCESSING]]></refund_status_0>
  38. <result_code><![CDATA[SUCCESS]]></result_code>
  39. <return_code><![CDATA[SUCCESS]]></return_code>
  40. <return_msg><![CDATA[OK]]></return_msg>
  41. <sign><![CDATA[1F2841558E233C33ABA71A961D27561C]]></sign>
  42. <transaction_id><![CDATA[1008450740201411110005820873]]></transaction_id>
  43. </xml>`
  44. mm := make(BodyMap)
  45. err := xml.Unmarshal([]byte(xmlData), &mm)
  46. if err != nil {
  47. xlog.Errorf("xml.Unmarshal(%s),error:%+v", xmlData, err)
  48. return
  49. }
  50. for k, v := range mm {
  51. xlog.Infof("%s:%s\n", k, v)
  52. }
  53. }
  54. type QueryRedRecordResponse struct {
  55. HbType string `xml:"hb_type,omitempty" json:"hb_type,omitempty"`
  56. Hblist *hbList `xml:"hblist,omitempty" json:"hblist,omitempty"`
  57. }
  58. type hbList struct {
  59. HbinfoList []*hbinfo `xml:"hbinfo,omitempty" json:"hbinfo,omitempty"`
  60. }
  61. type hbinfo struct {
  62. Openid string `xml:"openid,omitempty" json:"openid,omitempty"`
  63. Amount string `xml:"amount,omitempty" json:"amount,omitempty"`
  64. RcvTime string `xml:"rcv_time,omitempty" json:"rcv_time,omitempty"`
  65. }
  66. func TestBodyMap_UnmarshalXML2(t *testing.T) {
  67. xmlData := `
  68. <xml>
  69. <hb_type><![CDATA[NORMAL]]></hb_type>
  70. <hblist>
  71. <hbinfo>
  72. <openid><![CDATA[111]]></openid>
  73. <amount>222</amount>
  74. <rcv_time><![CDATA[333]]></rcv_time>
  75. </hbinfo>
  76. <hbinfo>
  77. <openid><![CDATA[444]]></openid>
  78. <amount>555</amount>
  79. <rcv_time><![CDATA[666]]></rcv_time>
  80. </hbinfo>
  81. </hblist>
  82. </xml>`
  83. mm := new(QueryRedRecordResponse)
  84. err := xml.Unmarshal([]byte(xmlData), &mm)
  85. if err != nil {
  86. xlog.Errorf("xml.Unmarshal(%s),error:%+v", xmlData, err)
  87. return
  88. }
  89. xlog.Debugf("%+v", mm)
  90. for _, v := range mm.Hblist.HbinfoList {
  91. xlog.Debugf("%+v", v)
  92. }
  93. }