-
Notifications
You must be signed in to change notification settings - Fork 1
/
requests_test.go
191 lines (170 loc) · 4.38 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
package requests
import (
"context"
"fmt"
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"sync/atomic"
"testing"
"time"
)
func Test_Basic(t *testing.T) {
resp, err := Get("http://httpbin.org/get")
t.Logf("%#v, %v", resp, err)
//resp, _ = Post("http://httpbin.org/post", "application/json", strings.NewReader(`{"a": "b"}`))
//t.Log(resp.Text())
}
func Test_ProxyGet(t *testing.T) {
t.Log("Testing get request")
sess := New(
Header("a", "b"),
Cookie(http.Cookie{Name: "username", Value: "golang"}),
BasicAuth("user", "123456"),
Timeout(3*time.Second),
//Hosts(map[string][]string{"127.0.0.1:8080": {"192.168.1.1:80"}, "4.org:80": {"httpbin.org:80"}}),
//Proxy("http://127.0.0.1:8080"),
)
resp, err := sess.DoRequest(
context.Background(),
Method("GET"),
URL("http://httpbin.org"),
)
if err != nil {
t.Errorf("%s", err.Error())
return
}
t.Log(resp.StatusCode, err)
//t.Log(resp.Text())
}
func Test_PostBody(t *testing.T) {
sess := New(
BasicAuth("user", "123456"),
Logf(func(context.Context, *Stat) {
fmt.Println("111111111")
}),
)
//if err := sess.Proxy("127.0.0.1:8080"); err != nil {
// t.Error(err)
// return
//}
resp, err := sess.DoRequest(context.Background(),
Method("POST"),
URL("http://httpbin.org/post"),
Params(map[string]string{
"a": "b/c",
"c": "3",
"d": "ddd",
}),
Param("e", "ea", "es"),
Body(`{"body":"QWER"}`),
Header("hello", "world"),
//TraceLv(9),
Logf(func(ctx context.Context, stat *Stat) {
fmt.Println(22222222)
//t.Logf("%v", stat.String())
}),
)
if err != nil {
t.Logf("%v", err)
return
}
t.Log(resp.StatusCode, err, resp.Response.ContentLength, resp.Request.ContentLength)
//t.Log(resp.Text())
//t.Log(resp.Stat())
}
func Test_FormPost(t *testing.T) {
t.Log("Testing get request")
sess := New()
resp, err := sess.DoRequest(context.Background(),
Method("POST"),
URL("http://httpbin.org/post"),
Form(url.Values{"name": {"12.com"}}),
Params(map[string]string{
"a": "b/c",
"c": "cc",
"d": "dddd",
}),
Param("e", "ea", "es"),
//TraceLv(9),
)
if err != nil {
log.Fatal(err)
return
}
t.Log(resp.StatusCode, err, resp.Response.ContentLength, resp.Request.ContentLength)
}
func Test_Race(t *testing.T) {
opts := Options{}
ctx := context.Background()
t.Logf("%#v", opts)
sess := New(URL("http://httpbin.org/post")) //, Auth("user", "123456"))
for i := 0; i < 10; i++ {
go func() {
_, _ = sess.DoRequest(ctx, MethodPost, Body(`{"a":"b"}`), Params(map[string]string{"1": "2/2"})) // nolint: errcheck
}()
}
time.Sleep(3 * time.Second)
}
func Test_Retry(t *testing.T) {
var reqCount int32 = 0
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reqNo := atomic.AddInt32(&reqCount, 1)
if reqNo%3 == 0 {
w.WriteHeader(404)
} else {
w.WriteHeader(200)
}
_, _ = w.Write([]byte(fmt.Sprintf("response: %d", reqNo)))
}))
defer s.Close()
sess := New()
_, _ = sess.DoRequest(context.Background(), URL(s.URL))
}
func Test_Cannel(t *testing.T) {
sess := New()
ctx, cancel := context.WithCancel(context.Background())
cancel()
resp, err := sess.DoRequest(ctx, URL("http://127.0.0.1:9099"))
t.Logf("%s, err=%v", resp.Stat(), err)
}
func Test_Stream(t *testing.T) {
body := `{"Namespace":"v_mix_vm", "ResultColumn":["UUID", "AccountName"], "Limit": 100}`
s := New(URL("http://cache-api.polaris:80/stream"), Body(body))
resp, err := s.DoRequest(context.Background(), MethodPost,
Header("Content-Type", "application/json"),
//TraceLv(3),
Logf(func(ctx context.Context, stat *Stat) {
fmt.Println(stat)
}),
Stream(func(_ int64, b []byte) error {
fmt.Print(string(b))
return nil
}))
t.Logf("%v, err=%v", resp.Stat(), err)
}
func TestResponse_Download(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
text := "abc\ndef\nghij\n\n123"
fmt.Fprint(w, text)
}))
defer s.Close()
u := "https://go.dev/dl/go1.22.1.darwin-amd64.tar.gz" // a35015fca6f631f3501a36b3bccba9c5
sess := New(URL(u))
f, err := os.OpenFile("tmp/xx.tar.gz", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0644)
defer f.Close()
sum := 0
resp, err := sess.DoRequest(context.Background(),
Stream(func(i int64, row []byte) error {
cnt, err := f.Write(row)
sum += cnt
return err
}))
if err != nil {
t.Logf("resp=%d, err=%s", resp.Content, err)
return
}
t.Logf("resp=%d, err=%s", resp.Content, err)
}