-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwechat_test.go
166 lines (130 loc) · 4.02 KB
/
wechat_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
package socialite
import (
"github.com/birjemin/socialite/utils"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
"time"
)
var (
wxHTTPClient = &utils.HTTPClient{
Client: &http.Client{
Timeout: 5 * time.Second,
},
}
wxObj = &Wechat{
AppID: "APPID",
AppSecret: "SECRET",
RedirectURL: "REDIRECT_URI",
HTTPRequest: wxHTTPClient,
}
)
// TestWxGetAuthorizeURL test GetAuthorizeURL
func TestWxGetAuthorizeURL(t *testing.T) {
url1 := "https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_login&state=SCOPE"
url2 := "https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_login&state=SCOPE"
ast := assert.New(t)
ast.Equal(url1, wxObj.GetAuthorizeURL("SCOPE"))
ast.Equal(url2, wxObj.GetAuthorizeURL("SCOPE", "STATE"))
}
// TestWxToken
func TestWxToken(t *testing.T) {
ast := assert.New(t)
var ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ret := `{"access_token":"YOUR_ACCESS_TOKEN","expires_in":7200,"refresh_token":"YOUR_REFRESH_TOKEN","openid":"OPENID","scope":"SCOPE","unionid":"o6_bmasdasdsad6_2sgVt7hMZOPfL"}`
for _, v := range []string{"appid", "secret", "code", "grant_type"} {
val := r.FormValue(v)
if val == "" {
ret = `{"errcode":40029,"errmsg":"invalid code"}`
break
}
}
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(ret)); err != nil {
t.Fatal(err)
}
}))
defer ts.Close()
// success
ret, err := wxObj.doToken(ts.URL, "code")
if err != nil {
ast.Error(err)
}
ast.Equal(0, ret.ErrCode)
ast.Equal("YOUR_ACCESS_TOKEN", ret.AccessToken)
ast.Equal(7200, ret.ExpiresIn)
ast.Equal("YOUR_REFRESH_TOKEN", ret.RefreshToken)
// fail
ret, err = wxObj.doToken(ts.URL, "")
if err != nil {
ast.Fail(err.Error())
return
}
ast.Equal(40029, ret.ErrCode)
}
// TestWxRefreshToken
func TestWxRefreshToken(t *testing.T) {
ast := assert.New(t)
var ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ret := `{"access_token":"YOUR_ACCESS_TOKEN","expires_in":7200,"refresh_token":"YOUR_REFRESH_TOKEN","openid":"OPENID","scope":"SCOPE"}`
for _, v := range []string{"appid", "grant_type", "refresh_token"} {
val := r.FormValue(v)
if val == "" {
ret = `{"errcode":40030,"errmsg":"invalid refresh_token"}`
break
}
}
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(ret)); err != nil {
t.Fatal(err)
}
}))
defer ts.Close()
// success
ret, err := wxObj.doRefreshToken(ts.URL, "YOUR_REFRESH_TOKEN")
if err != nil {
ast.Error(err)
}
ast.Equal(0, ret.ErrCode)
ast.Equal("YOUR_ACCESS_TOKEN", ret.AccessToken)
ast.Equal(7200, ret.ExpiresIn)
ast.Equal("YOUR_REFRESH_TOKEN", ret.RefreshToken)
// fail
ret, err = wxObj.doRefreshToken(ts.URL, "")
if err != nil {
ast.Fail(err.Error())
return
}
ast.Equal(40030, ret.ErrCode)
}
// TestWxUserInfo
func TestWxUserInfo(t *testing.T) {
ast := assert.New(t)
var ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ret := `{"openid":"YOUR_OPENID","nickname":"NICKNAME","sex":1,"province":"PROVINCE","city":"CITY","country":"COUNTRY","headimgurl":"https://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0","privilege":["PRIVILEGE1","PRIVILEGE2"],"unionid":" o6_bmasdasdsad6_2sgVt7hMZOPfL"}`
val := r.FormValue("access_token")
if val == "" {
ret = `{"errcode":40003,"errmsg":"invalid openid"}`
}
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(ret)); err != nil {
t.Fatal(err)
}
}))
defer ts.Close()
// success
ret, err := wxObj.doGetUserInfo(ts.URL, "YOUR_ACCESS_TOKEN", "YOUR_OPENID")
if err != nil {
ast.Error(err)
}
ast.Equal(0, ret.ErrCode)
ast.Equal("YOUR_OPENID", ret.OpenID)
// fail
ret, err = wxObj.doGetUserInfo(ts.URL, "", "")
if err != nil {
ast.Fail(err.Error())
return
}
ast.Equal(40003, ret.ErrCode)
}