forked from lattecake/request
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_test.go
68 lines (55 loc) · 1.28 KB
/
request_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
/**
* @Time : 2019-07-03 15:37
* @Author : [email protected]
* @File : request_test
* @Software: GoLand
*/
package request
import (
"crypto/tls"
"net"
"net/http"
"net/url"
"testing"
"time"
)
func TestRequest_Do(t *testing.T) {
var body = `{"hello": "world"}`
var res []byte
err := NewRequest("https://www.iphpt.com/", "POST").
Body([]byte(body)).Do().Into(&res)
if err != nil {
t.Error("err", err.Error())
}
t.Log("success", res)
}
func TestRequest_HttpClient(t *testing.T) {
var proxy func(r *http.Request) (*url.URL, error)
proxy = func(_ *http.Request) (*url.URL, error) {
return url.Parse("http://127.0.0.1:1087")
}
dialer := &net.Dialer{
Timeout: time.Duration(5 * int64(time.Second)),
KeepAlive: time.Duration(5 * int64(time.Second)),
}
var body []byte
err := NewRequest("www.baidu.com", "GET").
HttpClient(&http.Client{
Transport: &http.Transport{
Proxy: proxy, DialContext: dialer.DialContext,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: false,
},
},
}).Do().Into(&body)
if err != nil {
t.Error("err", err.Error())
}
t.Log("body", body)
var b []byte
if b, err = NewRequest("https://www.iphpt.com/", "GET").
Body([]byte(body)).Do().Raw(); err != nil {
t.Error("raw err", err.Error())
}
t.Log("b", string(b))
}