forked from imroc/req
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setting_test.go
62 lines (54 loc) · 1.26 KB
/
setting_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
package req
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
func newDefaultTestServer() *httptest.Server {
handler := func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hi"))
}
return httptest.NewServer(http.HandlerFunc(handler))
}
func TestSetClient(t *testing.T) {
ts := newDefaultTestServer()
client := &http.Client{}
SetClient(client)
_, err := Get(ts.URL)
if err != nil {
t.Errorf("error after set client: %v", err)
}
SetClient(nil)
_, err = Get(ts.URL)
if err != nil {
t.Errorf("error after set client to nil: %v", err)
}
client = Client()
if trans, ok := client.Transport.(*http.Transport); ok {
trans.MaxIdleConns = 1
trans.DisableKeepAlives = true
_, err = Get(ts.URL)
if err != nil {
t.Errorf("error after change client's transport: %v", err)
}
} else {
t.Errorf("transport is not http.Transport: %+#v", client.Transport)
}
}
func TestSetting(t *testing.T) {
defer func() {
if rc := recover(); rc != nil {
t.Errorf("panic happened while change setting: %v", rc)
}
}()
SetTimeout(2 * time.Second)
EnableCookie(false)
EnableCookie(true)
EnableInsecureTLS(true)
SetJSONIndent("", " ")
SetJSONEscapeHTML(false)
SetXMLIndent("", "\t")
SetProxyUrl("http://localhost:8080")
SetProxy(nil)
}