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