-
Notifications
You must be signed in to change notification settings - Fork 9
/
mw_test.go
73 lines (67 loc) · 1.67 KB
/
mw_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
package req
import (
"fmt"
"github.com/patrickmn/go-cache"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestWithRetry(t *testing.T) {
i := 0
Debug = true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintln(w, i)
i += 1
}))
c := NewClient()
c.Use(WithRetry(10, func(resp *Response) bool {
if i < 3 {
return false
}
return true
}))
err := Get(ts.URL).SetClient(c).Do().Error()
assert.Nil(t, err)
assert.Equal(t, 3, i)
}
func TestWithCache(t *testing.T) {
i := 1
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintln(w, i)
i += 1
}))
c := NewClient()
c.Use(WithCache(cache.New(10*time.Second, 10*time.Second)))
a, err := Get(ts.URL).SetClient(c).Do().Txt()
assert.Nil(t, err)
b, err := Get(ts.URL).SetClient(c).Do().Txt()
assert.Nil(t, err)
fmt.Println(a, b)
assert.Equal(t, a, b)
}
func TestWithRandomUA(t *testing.T) {
c := NewClient()
c.Use(WithRandomUA())
resp, err := Get("https://httpbin.org/get").SetClient(c).Do().Resp()
assert.Nil(t, err)
t.Log(resp.Text)
j, _ := resp.JSON()
assert.NotEqual(t, "Go-http-client/2.0", j.Get("headers.User-Agent").String())
}
func TestWithRefererFiller(t *testing.T) {
c := NewClient()
c.Use(WithRefererFiller())
resp, err := Get("https://httpbin.org/get").SetClient(c).Do().Resp()
assert.Nil(t, err)
t.Log(resp.Text)
j, _ := resp.JSON()
assert.True(t, j.Get("headers.Referer").Exists())
}
func TestWithDebug(t *testing.T) {
c := NewClient()
c.Use(WithDebug())
err := Get("https://httpbin.org/get").SetClient(c).Do().Error()
assert.Nil(t, err)
}