-
Notifications
You must be signed in to change notification settings - Fork 0
/
present_test.go
96 lines (81 loc) · 2.08 KB
/
present_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
package wxgamevp
import (
"errors"
"github.com/birjemin/wxgamevp/utils"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
)
// TestPresentQueryParams test query string
func TestPresentQueryParams(t *testing.T) {
ast := assert.New(t)
pay := Present{
OpenID: "odkx20ENSNa2w5y3g_qOkOvBNM1g",
AppID: "wx1234567",
OfferID: "12345678",
Ts: 1507530737,
ZoneID: "1",
Pf: "android",
BillNo: "BillNo_123",
AccessToken: "ACCESSTOKEN",
Secret: "zNLgAGgqsEWJOg1nFVaO5r7fAlIQxr1u",
}
query := pay.getQueryParams()
ast.Equal("1d3804993d06ae06560eb70ff9b102d7d5a932d99f9d4ef0d0fd07d9a10040d8", query["sig"])
}
// TestPresent test cancel_pay
func TestPresent(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := r.URL.EscapedPath()
if path != getPresentURI {
t.Fatalf("path is invalid: %s, %s'", getPresentURI, path)
}
if err := r.ParseForm(); err != nil {
t.Fatal(err)
}
queries := []string{"access_token"}
for _, v := range queries {
content := r.Form.Get(v)
if content == "" {
t.Fatalf("param %v can not be empty", v)
}
}
body, _ := ioutil.ReadAll(r.Body)
if string(body) == "" {
t.Fatal("body is empty")
}
w.WriteHeader(http.StatusOK)
raw := `{"errcode":0,"errmsg":"","bill_no":"1","balance":0}`
if _, err := w.Write([]byte(raw)); err != nil {
t.Fatal(err)
}
}))
defer ts.Close()
httpClient := &utils.HTTPClient{
Client: &http.Client{
Timeout: 5 * time.Second,
},
}
pay := Present{
OpenID: "odkx20ENSNa2w5y3g_qOkOvBNM1g",
AppID: "wx1234567",
OfferID: "12345678",
Ts: 1507530737,
ZoneID: "1",
BillNo: "BillNo_123",
Pf: "android",
AccessToken: "ACCESSTOKEN",
Secret: "zNLgAGgqsEWJOg1nFVaO5r7fAlIQxr1u",
HTTPRequest: httpClient,
}
if ret, err := pay.doPresent(ts.URL); err != nil {
t.Error(err)
} else {
if ret.ErrCode != 0 {
t.Error(errors.New("msg: " + ret.ErrMsg))
}
}
}