-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurlparam_test.go
More file actions
102 lines (97 loc) · 2.23 KB
/
urlparam_test.go
File metadata and controls
102 lines (97 loc) · 2.23 KB
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
package urlparam
import (
"net/url"
"reflect"
"testing"
)
// UserReportParams 作为示例
type UserReportParams struct {
BusiType int32 `json:"busi_type,omitempty"` // 同一个标签里面有option参数,例如PB生成的结构
UID string `json:"uid"` // 一个字段一个标签 string类型
AdposType int32 `json:"adpos_type"`
ActionType int32 // 无标签
FeedsIndex uint32 `json:"feeds_index"` // uint32 类型
AdposID string `json:"-"` // 屏蔽标签
}
func TestEncode(t *testing.T) {
type args struct {
s interface{}
}
want := url.Values{}
want.Set("busi_type", "1")
want.Set("uid", "2222")
want.Set("adpos_type", "555")
want.Set("ActionType", "-666")
want.Set("feeds_index", "7777")
tests := []struct {
name string
args args
want url.Values
wantErr bool
}{
{
name: "Encode",
args: args{&UserReportParams{
BusiType: 1,
UID: "2222",
AdposType: 555,
ActionType: -666,
FeedsIndex: 7777,
AdposID: "aaaaa",
}},
want: want,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Encode(tt.args.s)
if (err != nil) != tt.wantErr {
t.Errorf("Encode() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Encode() = %v, want %v", got, tt.want)
}
})
}
}
func TestDecode(t *testing.T) {
urlStr := "ActionType=-666&adpos_type=555&busi_type=1&feeds_index=7777&uid=2222"
values, _ := url.ParseQuery(urlStr)
got := &UserReportParams{}
want := &UserReportParams{
BusiType: 1,
UID: "2222",
AdposType: 555,
ActionType: -666,
FeedsIndex: 7777,
}
type args struct {
urlValues url.Values
s interface{}
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Decode",
args: args{
urlValues: values,
s: got,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := Decode(tt.args.urlValues, tt.args.s); (err != nil) != tt.wantErr {
t.Errorf("Decode() error = %v, wantErr %v", err, tt.wantErr)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Encode() = %v, want %v", got, want)
}
})
}
}