蛋蛋星球-制度模式
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
4.1 KiB

  1. package gopay
  2. import (
  3. "container/list"
  4. "encoding/json"
  5. "encoding/xml"
  6. "testing"
  7. "github.com/go-pay/util"
  8. "github.com/go-pay/xlog"
  9. )
  10. func TestBodyMapSetBodyMap(t *testing.T) {
  11. xlog.SetLevel(xlog.DebugLevel)
  12. bm := make(BodyMap)
  13. // 1、配合map使用
  14. sceneInfo := make(map[string]map[string]string)
  15. h5Info := make(map[string]string)
  16. h5Info["type"] = "Wap"
  17. h5Info["wap_url"] = "https://www.fmm.ink"
  18. h5Info["wap_name"] = "H5测试支付"
  19. sceneInfo["h5_info"] = h5Info
  20. bm.Set("scene_info", sceneInfo)
  21. xlog.Debug("配合map使用:", bm) // map[scene_info:map[h5_info:map[type:Wap wap_name:H5测试支付 wap_url:https://www.fmm.ink]]]
  22. bm.Reset()
  23. xlog.Debug(bm) // []
  24. // 2、基础用法
  25. bm.Set("1key", "1value")
  26. bm.Set("2key", "2value")
  27. bm.Set("3key", "3value")
  28. xlog.Debug("基础用法:", bm)
  29. bm.Reset()
  30. xlog.Debug(bm) // []
  31. // 3、链式用法
  32. bm.Set("4key", "4value").
  33. Set("5key", "5value").
  34. Set("6key", "6value")
  35. xlog.Debug("链式用法:", bm)
  36. bm.Reset()
  37. xlog.Debug(bm) // []
  38. // 4、高级用法
  39. bm.SetBodyMap("scene_info", func(bm BodyMap) {
  40. bm.SetBodyMap("h5_info", func(bm BodyMap) {
  41. bm.Set("type", "Wap").
  42. Set("wap_url", "https://www.fmm.ink").
  43. Set("wap_name", "H5测试支付")
  44. })
  45. }).Set("7key", "7value").
  46. Set("8key", "8value")
  47. xlog.Debug("高级用法:", bm) // map[scene_info:map[h5_info:map[type:Wap wap_name:H5测试支付 wap_url:https://www.fmm.ink]]]
  48. xlog.Debug("高级用法 JsonBody:", bm.JsonBody())
  49. }
  50. func TestBodyMapMarshal(t *testing.T) {
  51. xlog.SetLevel(xlog.DebugLevel)
  52. bm := make(BodyMap)
  53. bm.Set("4key", "4value").
  54. Set("6key", "6value").
  55. Set("5key", "5value")
  56. jb := bm.JsonBody()
  57. xlog.Debug("jb:", jb)
  58. bm.Reset()
  59. bm.SetBodyMap("scene_info", func(bm BodyMap) {
  60. bm.SetBodyMap("h5_info", func(bm BodyMap) {
  61. bm.Set("type", "Wap").
  62. Set("wap_url", "https://www.fmm.ink").
  63. Set("wap_name", "H5测试支付")
  64. })
  65. }).Set("7key", "7value").
  66. Set("8key", "8value")
  67. jb2 := bm.JsonBody()
  68. xlog.Debug("jb2:", jb2)
  69. bm.Reset()
  70. bm.SetBodyMap("partner", func(bm BodyMap) {
  71. bm.Set("type", "APPID").
  72. Set("appid", "wx123456").
  73. Set("merchant_id", "88888")
  74. }).SetBodyMap("authorized_data", func(bm BodyMap) {
  75. bm.Set("business_type", "BUSIFAVOR_STOCK").
  76. Set("stock_id", "66666")
  77. }).Set("limit", 5).
  78. Set("offset", 10)
  79. urlParams := bm.EncodeURLParams()
  80. xlog.Debug("urlParams:", urlParams)
  81. }
  82. func TestBodyMapMarshalSlice(t *testing.T) {
  83. xlog.SetLevel(xlog.DebugLevel)
  84. type Receiver struct {
  85. Type string `json:"type"`
  86. Account string `json:"account"`
  87. Amount int `json:"amount"`
  88. Description string `json:"description"`
  89. }
  90. var rs []*Receiver
  91. item := &Receiver{
  92. Type: "MERCHANT_ID",
  93. Account: "190001001",
  94. Amount: 100,
  95. Description: "分到商户",
  96. }
  97. rs = append(rs, item)
  98. item2 := &Receiver{
  99. Type: "PERSONAL_OPENID",
  100. Account: "86693952",
  101. Amount: 888,
  102. Description: "分到个人",
  103. }
  104. rs = append(rs, item2)
  105. bs, _ := json.Marshal(rs)
  106. bm := make(BodyMap)
  107. bm.Set("nonce_str", util.RandomString(32)).
  108. Set("transaction_id", "4208450740201411110007820472").
  109. Set("out_order_no", "P20150806125346")
  110. bm.Set("receivers", string(bs))
  111. xlog.Debug("JsonBody:", bm.JsonBody())
  112. //receiver := make(BodyMap)
  113. //receiver.Set("receiver", string(bs))
  114. //
  115. //body := receiver.JsonBody()
  116. bss, _ := xml.Marshal(bm)
  117. xlog.Debug("body:", string(bss))
  118. }
  119. func TestSliceTest(t *testing.T) {
  120. xlog.SetLevel(xlog.DebugLevel)
  121. var rs []string
  122. rs = append(rs, "SOFTWARE")
  123. rs = append(rs, "SECURITY")
  124. rs = append(rs, "LOVE_MARRIAGE")
  125. bm := make(BodyMap)
  126. bm.Set("sub_mchid", "2021060717").
  127. Set("advertising_industry_filters", rs)
  128. xlog.Debugf("%s", bm.JsonBody())
  129. }
  130. func TestOutSlice(t *testing.T) {
  131. xlog.SetLevel(xlog.DebugLevel)
  132. type Jerry struct {
  133. Name string `json:"name"`
  134. Age int `json:"age"`
  135. }
  136. var js = []*Jerry{
  137. {
  138. Name: "Jerry",
  139. Age: 18,
  140. },
  141. {
  142. Name: "Tom",
  143. Age: 20,
  144. },
  145. }
  146. bm := make(BodyMap)
  147. bm.Set("", js)
  148. xlog.Debugf("%v", bm.GetAny(""))
  149. xlog.Debugf("%s", bm.GetString(""))
  150. xlog.Debugf("%s", bm.JsonBody())
  151. }
  152. func TestLruCache(t *testing.T) {
  153. list.New()
  154. }