forked from royalrick/weapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdatable_message_test.go
129 lines (106 loc) · 2.82 KB
/
updatable_message_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
package weapp
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestCreateActivityID(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Fatalf("Expect 'GET' get '%s'", r.Method)
}
realPath := r.URL.EscapedPath()
expectPath := "/cgi-bin/message/wxopen/activityid/create"
if realPath != expectPath {
t.Fatalf("Expect to path '%s',get '%s'", expectPath, realPath)
}
if err := r.ParseForm(); err != nil {
t.Fatal(err)
}
if r.Form.Get("access_token") == "" {
t.Fatalf("access_token can not be empty")
}
w.WriteHeader(http.StatusOK)
raw := `{
"expiration_time": 1000,
"activity_id": "ok",
"errcode": 0,
"errmsg": "ok"
}`
if _, err := w.Write([]byte(raw)); err != nil {
t.Fatal(err)
}
}))
defer ts.Close()
res, err := createActivityID(ts.URL+apiCreateActivityID, "mock-access-token")
if err != nil {
t.Fatal(err)
}
if res.ActivityID == "" {
t.Error("Response column activity_id can not be empty")
}
if res.ExpirationTime == 0 {
t.Error("Response column expiration_time can not be zero")
}
}
func TestSetUpdatableMsg(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Fatalf("Expect 'POST' get '%s'", r.Method)
}
realPath := r.URL.EscapedPath()
expectPath := "/cgi-bin/message/wxopen/updatablemsg/send"
if realPath != expectPath {
t.Fatalf("Expect to path '%s',get '%s'", expectPath, realPath)
}
if err := r.ParseForm(); err != nil {
t.Fatal(err)
}
if r.Form.Get("access_token") == "" {
t.Fatalf("access_token can not be empty")
}
params := struct {
ActivityID string `json:"activity_id"`
TargetState uint8 `json:"target_state"`
TemplateInfo struct {
ParameterList []struct {
Name string `json:"name"`
Value string `json:"value"`
} `json:"parameter_list"`
} `json:"template_info"`
}{}
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
t.Fatal(err)
}
if params.ActivityID == "" {
t.Fatal("param activity_id can not be empty")
}
if len(params.TemplateInfo.ParameterList) == 0 {
t.Fatal("param template_info.parameter_list can not be empty")
}
w.WriteHeader(http.StatusOK)
raw := `{
"errcode": 0,
"errmsg": "ok"
}`
if _, err := w.Write([]byte(raw)); err != nil {
t.Fatal(err)
}
}))
defer ts.Close()
setter := UpdatableMsgSetter{
"mock-activity-id",
UpdatableMsgJoining,
UpdatableMsgTempInfo{
[]UpdatableMsgParameter{
{UpdatableMsgParamMemberCount, "mock-parameter-value-number"},
{UpdatableMsgParamRoomLimit, "mock-parameter-value-number"},
},
},
}
_, err := setter.set(ts.URL+apiSetUpdatableMsg, "mock-access-token")
if err != nil {
t.Fatal(err)
}
}