-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
client_test.go
73 lines (65 loc) · 2.07 KB
/
client_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
// Copyright 2020 FastWeGo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package miniprogram
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestClient_getAccessToken(t *testing.T) {
var MockMiniprogram = New(Config{
Appid: "TestClient_getAccessToken",
Secret: "SECRET",
})
// Mock Server
var MockSvrHandler = http.NewServeMux()
var MockSvr = httptest.NewServer(MockSvrHandler)
WXServerUrl = MockSvr.URL // 拦截发往微信服务器的请求
mockResp := map[string][]byte{
"case1": []byte(`{"access_token":"ACCESS_TOKEN","expires_in":3}`),
"case2": []byte(`{"errcode":40013,"errmsg":"invalid appid"}`),
}
var resp []byte
// Mock access token
MockSvrHandler.HandleFunc("/cgi-bin/token", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(resp)
})
client := &Client{
Ctx: MockMiniprogram,
}
tests := []struct {
name string
wantAccessToken string
wantErr bool
}{
{name: "case1", wantAccessToken: "ACCESS_TOKEN", wantErr: false},
{name: "case2", wantAccessToken: "", wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client.Ctx.AccessToken.Cache.Delete(client.Ctx.Config.Appid)
resp = mockResp[tt.name]
gotAccessToken, err := GetAccessToken(client.Ctx)
fmt.Println(gotAccessToken, err)
if (err != nil) != tt.wantErr {
t.Errorf("getAccessToken() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotAccessToken != tt.wantAccessToken {
t.Errorf("getAccessToken() gotAccessToken = %v, want %v", gotAccessToken, tt.wantAccessToken)
}
})
}
}