-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
210 lines (173 loc) · 4.36 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package easy_http
import (
"fmt"
"net/http"
"testing"
"time"
)
func TestClient(t *testing.T) {
//new 一个构造器
builder := NewClientBuilder()
//是否跳过服务器证书校验
builder.SkipVerify(false)
//设置超时时间
builder.TimeOut(time.Second * 5)
//设置代理
builder.ProxyUrl("http://127.0.0.1:10809")
//设置根证书
var certPool [1]string
certPool[0] = "D:\\server.pem"
builder.Cert(certPool[:])
//设置双向校验证书
var tlsPath [1]*TlsPath
tlsPath[0] = &TlsPath{
CertFile: "D:\\client.pem",
KeyFile: "D:\\client.key",
}
builder.Tls(tlsPath[:])
//设置http请求header
header := make(map[string]string)
header["Accept-Language"] = "Accept-Language: en,zh"
builder.Header(header)
//设置http请求cookie
cookie := make(map[string]string)
cookie["name"] = "value"
builder.Cookie(EasyCookie(cookie))
//设置 Response 处理函数
builder.BuildResponse(EasyBuildResponse)
//构造client
client, err := builder.Build()
if err != nil {
fmt.Println(err)
return
}
response := client.Get("https://baidu.com")
fmt.Println(response.Error())
fmt.Println(response.StatusCode())
fmt.Println(string(response.Content()))
}
func TestClientGetAsyn(t *testing.T) {
builder := NewClientBuilder()
client, _ := builder.Build()
err := client.GetAsyn("http://baidu.com", call)
if err != nil {
panic(err)
}
time.Sleep(5 * time.Second)
}
func TestClientGetAsynWithCallback(t *testing.T) {
builder := NewClientBuilder()
client, err := builder.Build()
if err != nil {
panic(err.Error())
}
err = client.GetAsynWithCallback("http://baidu.com", &Get{})
if err != nil {
panic(err)
}
time.Sleep(5 * time.Second)
}
func call(response IResponse) {
fmt.Println(response.Error())
fmt.Println(string(response.Content()))
}
type Get struct {
}
func (g Get) EasyResponseCallback(response IResponse) {
fmt.Println(response.Error())
fmt.Println(string(response.Content()))
}
func TestClientProxy(t *testing.T) {
builder := NewClientBuilder()
builder.ProxyUrl("http://127.0.0.1:10809")
client, err := builder.Build()
if err != nil {
fmt.Println(err)
return
}
response := client.Get("https://google.com")
fmt.Println(response.Error())
fmt.Println(response.StatusCode())
fmt.Println(string(response.Content()))
}
func TestClientStl(t *testing.T) {
builder := NewClientBuilder()
//builder.Tls("D:\\client.pem", "D:\\client.key")
client, err := builder.Build()
if err != nil {
fmt.Println(err)
return
}
response := client.Get("https://lqx.com")
fmt.Println(response.Error())
fmt.Println(response.StatusCode())
fmt.Println(string(response.Content()))
}
func TestClientHeader(t *testing.T) {
builder := NewClientBuilder()
header := make(map[string]string)
header["token"] = "ttttt"
client, err := builder.Header(header).Build()
if err != nil {
panic(err)
}
header2 := make(map[string]string)
header2["User-Agent"] = "dsdsdsdsdsd"
client.Header(header2).Get("http://127.0.0.1:8088")
client.Get("http://127.0.0.1:8088")
}
func TestClientPostForm(t *testing.T) {
builder := NewClientBuilder()
client, err := builder.Build()
if err != nil {
panic(err)
}
data := make(map[string]string)
data["name"] = "vvvvv1"
data["name2"] = "2222222"
client.PostForm("http://127.0.0.1:8088", EasyPost(data))
}
func TestClientPostMultipart(t *testing.T) {
builder := NewClientBuilder()
client, err := builder.Build()
if err != nil {
panic(err)
}
data := make(map[string]string)
data["name"] = "vvvvv1"
data["name2"] = "2222222"
multipartBuilder := NewMultipartBuilder()
multipart, err := multipartBuilder.FromDate(data).AddFile("file", "D:\\a.txt").
AddFile("file2", "D:\\b.log").Builder()
if err != nil {
panic(err)
}
client.PostMultipart("http://127.0.0.1:8088", multipart)
}
func TestClientSendWithMethod(t *testing.T) {
builder := NewClientBuilder()
client, err := builder.Build()
if err != nil {
panic(err)
}
response := client.SendWithMethod("http://127.0.0.1:8088", http.MethodGet, nil, func(request *http.Request) {
})
fmt.Println(response)
}
func TestClientHttpRequest(t *testing.T) {
builder := NewClientBuilder()
client, err := builder.Build()
if err != nil {
panic(err)
}
request, err := http.NewRequest(http.MethodGet, "http://baidu.com", nil)
if err != nil {
panic(err)
}
response, err := client.DoRequest(request)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
}
}