-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_test.go
107 lines (79 loc) · 2.93 KB
/
user_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
package user_test
import (
"bytes"
"encoding/json"
"fmt"
"io"
//"github.com/KerryJava/goserver/user"
"net/http"
// "net/url"
"testing"
)
func Benchmark_Request(b *testing.B) {
b.StopTimer()
b.StartTimer()
for i := 0; i < b.N; i++ {
requestControl()
}
}
func Benchmark_LoginRequest(b *testing.B) {
b.StopTimer()
b.StartTimer()
for i := 0; i < b.N; i++ {
login()
}
}
func Test_HttpConnect(t *testing.T) {
response, buf, _ := requestControl()
t.Logf("%s", buf)
if response.StatusCode == http.StatusOK {
t.Log("success")
} else {
t.Error("response no ok")
}
}
func requestControl() (*http.Response, string, error) {
reqParam := make(map[string]interface{})
reqParam["id"] = "123456"
reqParam["jsonrpc"] = "2.0"
reqParam["method"] = "User.ContactList"
reqParam["params"] = make(map[string]interface{})
bytesData, err := json.Marshal(reqParam)
if err != nil {
fmt.Println(err.Error())
return nil, "", err
}
reader := bytes.NewReader(bytesData)
r, err := http.NewRequest("POST", "http://localhost:8085/control", reader)
token := "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MDY3ODEzNTcsImlhdCI6MTUwNjc3Nzc1NywidXNlcmlkIjoxMTAwMDAwMDAwfQ.38i1lsbrrNNU309tpbfRKBPVU2Wj07Xj3pNiq7i9BWhmR3GhhBRgQToAhKXVae5NEP6MC4-rliHnzI4xaLHLkXGmFmB2PDEIlU4wP8_Js6RnBiDNr7lLpzyG_r4vwM9KCDVB9cIyMSaGGU0H2ACDKGhg8wpKqZNYF1yhxuaB1VwHOhTyw-PlfIrlHWagT-hEyRFfCHX6yi1F6cfHs9XQ1WxGmi37VDt1eYeorWx9BTj4T7gqT1XmzOASBk-c1rHC9ow7YMVhV4TcsXGWet7nqY-NZ8xpeFUYeNH2iW1i2SslBQigdl0bHKAMJy2Uyaq3hEUeJn2OlT7JhCqpndb4iQ"
r.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
response, err := http.DefaultClient.Do(r)
buf := new(bytes.Buffer)
io.Copy(buf, response.Body)
response.Body.Close()
return response, buf.String(), nil
}
func login() (*http.Response, string, error) {
reqParam := make(map[string]interface{})
reqParam["id"] = "123456"
reqParam["jsonrpc"] = "2.0"
reqParam["method"] = "Base.Login"
param := make(map[string]interface{})
param["phone"] = "13826595953"
param["passwd"] = "123456"
reqParam["params"] = param
bytesData, err := json.Marshal(reqParam)
if err != nil {
fmt.Println(err.Error())
return nil, "", err
}
reader := bytes.NewReader(bytesData)
r, err := http.NewRequest("POST", "http://localhost:8085/control", reader)
token := "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MDY3ODEzNTcsImlhdCI6MTUwNjc3Nzc1NywidXNlcmlkIjoxMTAwMDAwMDAwfQ.38i1lsbrrNNU309tpbfRKBPVU2Wj07Xj3pNiq7i9BWhmR3GhhBRgQToAhKXVae5NEP6MC4-rliHnzI4xaLHLkXGmFmB2PDEIlU4wP8_Js6RnBiDNr7lLpzyG_r4vwM9KCDVB9cIyMSaGGU0H2ACDKGhg8wpKqZNYF1yhxuaB1VwHOhTyw-PlfIrlHWagT-hEyRFfCHX6yi1F6cfHs9XQ1WxGmi37VDt1eYeorWx9BTj4T7gqT1XmzOASBk-c1rHC9ow7YMVhV4TcsXGWet7nqY-NZ8xpeFUYeNH2iW1i2SslBQigdl0bHKAMJy2Uyaq3hEUeJn2OlT7JhCqpndb4iQ"
r.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
response, err := http.DefaultClient.Do(r)
buf := new(bytes.Buffer)
io.Copy(buf, response.Body)
response.Body.Close()
return response, buf.String(), nil
}