forked from asmcos/requests
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequests_test.go
173 lines (129 loc) · 3.59 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
package requests
import (
"fmt"
"net/http"
"testing"
"os"
)
func TestGet(t *testing.T) {
// example 1
println("Get example1")
req := Requests()
req.Header.Set("accept-encoding", "gzip, deflate, br")
req.Get("http://go.xiulian.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.jeapedu.com",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
}
Get("http://jeapedu.com", h, Header{"accept-encoding":"gzip, deflate, br"})
// example 3
println("Get example3")
p := Params{
"title": "The blog",
"name": "file",
"id": "12345",
}
resp := Requests().Get("http://www.cpython.org", p)
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 = req.Get("https://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?0.45874470316137206",Auth{"httpwatch","foo"})
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 = req.Get("https://httpbin.org/json")
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 = req.Get("https://httpbin.org/gzip")
fmt.Println(resp.Text())
// example 7 proxy and debug
println("Get example7")
req = Requests()
req.Debug = 1
//req.Proxy("http://192.168.1.190:8888")
resp = req.Get("https://www.sina.com.cn")
// fmt.Println(resp.Text())
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 = req.Get("https://www.httpbin.org")
coo := resp.Cookies()
// coo is [] *http.Cookies
println("********cookies*******")
for _, c:= range coo{
fmt.Println(c.Name,c.Value)
}
}
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 := req.Post("https://www.httpbin.org/post",data)
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 = req.Post("https://www.httpbin.org/post",data,Files{"a":path1,"b":path2})
fmt.Println(resp.Text())
}
func TestTimeout(t *testing.T) {
println("Timeout example1")
req := Requests()
req.Debug = 1
// 20 Second
req.SetTimeout(20)
req.Get("http://golang.org")
}