forked from asmcos/requests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests_test.go
270 lines (217 loc) · 5.79 KB
/
requests_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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package requests
import (
"fmt"
"github.com/davecgh/go-spew/spew"
"net/http"
"os"
"testing"
)
func TestGet(t *testing.T) {
// example 1
println("Get example1")
req := Requests()
req.Header.Set("accept-encoding", "gzip, deflate, br")
req.Get("http://www.zhanluejia.net.cn", Header{"Referer": "http://www.jeapedu.com"}, Params{"c": "d", "e": "f"}, Params{"c": "a"})
// example 2
println("Get example2")
h := Header{
"Referer": "http://www.zhanluejia.net.cn",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
}
Get("http://www.zhanluejia.net.cn", h, Header{"accept-encoding": "gzip, deflate, br"})
// example 3
println("Get example3")
p := Params{
"title": "The blog",
"name": "file",
"id": "12345",
}
resp, err := Requests().Get("http://www.cpython.org", p)
if err == nil {
resp.Text()
fmt.Println(resp.Text())
}
// example 4
println("Get example4")
// test authentication usernae,password
//documentation https://www.httpwatch.com/httpgallery/authentication/#showExample10
req = Requests()
resp, err = req.Get("https://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?0.45874470316137206", Auth{"httpwatch", "foo"})
if err == nil {
fmt.Println(resp.R)
}
// this save file test PASS
// resp.SaveFile("auth.jpeg")
//example 5 test Json
println("Get example5")
req = Requests()
req.Header.Set("Content-Type", "application/json")
resp, err = req.Get("https://httpbin.org/json")
if err == nil {
var json map[string]interface{}
resp.Json(&json)
for k, v := range json {
fmt.Println(k, v)
}
}
// example 6 test gzip
println("Get example6")
req = Requests()
req.Debug = 1
resp, err = req.Get("https://httpbin.org/gzip")
if err == nil {
fmt.Println(resp.Text())
}
// example 7 proxy and debug
println("Get example7")
req = Requests()
req.Debug = 1
// You need open the line
//req.Proxy("http://192.168.1.190:8888")
req.Get("https://www.sina.com.cn")
//example 8 test auto Cookies
println("Get example8")
req = Requests()
req.Debug = 1
// req.Proxy("http://192.168.1.190:8888")
req.Get("https://www.httpbin.org/cookies/set?freeform=1234")
req.Get("https://www.httpbin.org")
req.Get("https://www.httpbin.org/cookies/set?a=33d")
req.Get("https://www.httpbin.org")
// example 9 test AddCookie
println("Get example9")
req = Requests()
req.Debug = 1
cookie := &http.Cookie{}
cookie.Name = "anewcookie"
cookie.Value = "20180825"
cookie.Path = "/"
req.SetCookie(cookie)
fmt.Println(req.Cookies)
// req.Proxy("http://127.0.0.1:8888")
req.Get("https://www.httpbin.org/cookies/set?freeform=1234")
req.Get("https://www.httpbin.org")
req.Get("https://www.httpbin.org/cookies/set?a=33d")
resp, err = req.Get("https://www.httpbin.org")
if err == nil {
coo := resp.Cookies()
// coo is [] *http.Cookies
println("********cookies*******")
for _, c := range coo {
fmt.Println(c.Name, c.Value)
}
}
}
func TestClose(t *testing.T) {
req := Requests()
fmt.Println("Start 1000 times get test...")
for i:=0;i<1000;i++{
_,err := req.Post("http://localhost:1337/requests",Datas{"SrcIp":"4312"})
fmt.Printf("\r%d %v",i,err)
req.Close()
}
fmt.Println("1000 times get test end.")
}
func TestPost(t *testing.T) {
// example 1
// set post formdata
println("Post example1")
req := Requests()
req.Debug = 1
data := Datas{
"comments": "ew",
"custemail": "[email protected]",
"custname": "1",
"custtel": "2",
"delivery": "12:45",
"size": "small",
"topping": "bacon",
}
resp, err := req.Post("https://www.httpbin.org/post", data)
if err == nil {
fmt.Println(resp.Text())
}
//example 2 upload files
println("Post example2")
req = Requests()
req.Debug = 1
path, _ := os.Getwd()
path1 := path + "/README.md"
path2 := path + "/docs/index.md"
resp, err = req.Post("https://www.httpbin.org/post", data, Files{"a": path1, "b": path2})
if err == nil {
fmt.Println(resp.Text())
}
req = Requests()
cookie := &http.Cookie{}
cookie.Name = "postcookie"
cookie.Value = "20200725"
cookie.Path = "/"
req.SetCookie(cookie)
//test post cookies
resp, err = req.Post("https://www.httpbin.org/post", data)
if err == nil {
coo := resp.Cookies()
// coo is [] *http.Cookies
println("********Post cookies*******")
for _, c := range coo {
fmt.Println(c.Name, c.Value)
}
}
}
func TestTimeout(t *testing.T) {
println("Timeout example1")
req := Requests()
req.Debug = 1
// 20 Second
req.SetTimeout(20)
req.Get("http://golang.org")
}
func TestPostGet(t *testing.T) {
println("Test Post and Get")
client := Requests()
client.Debug = 1
resp, err := client.Post("https://www.httpbin.org/post", Datas{"abc": "123", "ddd": "789"})
spew.Dump(client)
resp, err = client.Get("https://www.httpbin.org/get")
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Text())
}
func TestPostJson(t *testing.T) {
type StructReq struct {
ContainerId string `json:"id"`
Worker string `json:"worker"`
Force bool `json:"force"`
}
dataStruct := StructReq{
ContainerId: "123456",
Worker: "worker1",
Force: true,
}
dataMap := map[string]interface{}{
"id": "123456",
"worker": "worker1",
"force": true,
}
dataJsonStr := "{\"id\":\"123456\",\"worker\":\"worker1\",\"force\": true}"
println("Test PostJson")
client := Requests()
client.Debug = 1
resp, err := client.PostJson("https://www.httpbin.org/post", dataStruct)
if err != nil {
t.Fatalf("post struct json error: %v", err)
}
fmt.Println(resp.Text())
resp, err = client.PostJson("https://www.httpbin.org/post", dataMap)
if err != nil {
t.Fatalf("post struct json error: %v", err)
}
fmt.Println(resp.Text())
resp, err = client.PostJson("https://www.httpbin.org/post", dataJsonStr)
if err != nil {
t.Fatalf("post struct json error: %v", err)
}
fmt.Println(resp.Text())
}