-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_user_storage_test.go
77 lines (62 loc) · 1.53 KB
/
set_user_storage_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
package wxgameod
import (
"errors"
"github.com/birjemin/wxgameod/utils"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
)
// TestSetUserStorage test SetUserStorage
func TestSetUserStorage(t *testing.T) {
var sessionKey = "tiihtNczf5v6AKRyjwEUhQ=="
var httpClient = &utils.HTTPClient{
Client: &http.Client{
Timeout: 5 * time.Second,
},
}
var ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := r.URL.EscapedPath()
if path != setUserStorageURI {
t.Fatalf("path is invalid: %s, %s'", setUserStorageURI, path)
}
if err := r.ParseForm(); err != nil {
t.Fatal(err)
}
for _, v := range []string{"access_token", "signature"} {
content := r.Form.Get(v)
if content == "" {
t.Fatalf("param %v can not be empty", v)
}
}
body, _ := ioutil.ReadAll(r.Body)
kvList := string(body)
if kvList == "" {
t.Fatal("body is empty")
}
if Encrypt(sessionKey, kvList) != r.Form.Get("signature") {
t.Fatal("signature is invalid")
}
w.WriteHeader(http.StatusOK)
raw := `{"errcode":0,"errmsg":""}`
if _, err := w.Write([]byte(raw)); err != nil {
t.Fatal(err)
}
}))
defer ts.Close()
var m = SetUserStorage{
AccessToken: "ACCESSTOKEN",
SessionKey: sessionKey,
OpenID: "OPENID",
KvList: "{\"kv_list\":[{\"key\":\"1\",\"value\":\"0\"}]}",
HTTPRequest: httpClient,
}
if ret, err := m.doSetUserStorage(ts.URL); err != nil {
t.Error(err)
} else {
if ret.ErrCode != 0 {
t.Error(errors.New("msg: " + ret.ErrMsg))
}
}
}