-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathretry_test.go
120 lines (104 loc) · 2.75 KB
/
retry_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
package retry
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/nbio/st"
"gopkg.in/h2non/gentleman.v2"
"gopkg.in/h2non/gentleman.v2/plugins/timeout"
)
func TestRetryRequest(t *testing.T) {
calls := 0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
if calls < 3 {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
w.Header().Set("foo", r.Header.Get("foo"))
fmt.Fprintln(w, "Hello, world")
}))
defer ts.Close()
req := gentleman.NewRequest()
req.SetHeader("foo", "bar")
req.URL(ts.URL)
req.Use(New(nil))
res, err := req.Send()
st.Expect(t, err, nil)
st.Expect(t, res.Ok, true)
st.Expect(t, res.StatusCode, 200)
st.Expect(t, res.Header.Get("foo"), "bar")
st.Expect(t, calls, 3)
}
func TestRetryRequestWithPayload(t *testing.T) {
calls := 0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
if calls < 3 {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
buf, _ := ioutil.ReadAll(r.Body)
fmt.Fprintln(w, string(buf))
}))
defer ts.Close()
req := gentleman.NewRequest()
req.URL(ts.URL)
req.Method("POST")
req.BodyString("Hello, world")
req.Use(New(nil))
res, err := req.Send()
st.Expect(t, err, nil)
st.Expect(t, res.Ok, true)
st.Expect(t, res.RawResponse.ContentLength, int64(13))
st.Expect(t, res.StatusCode, 200)
st.Expect(t, res.String(), "Hello, world\n")
st.Expect(t, calls, 3)
}
func TestRetryServerError(t *testing.T) {
calls := 0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
w.WriteHeader(http.StatusServiceUnavailable)
}))
defer ts.Close()
req := gentleman.NewRequest()
req.URL(ts.URL)
req.Use(New(nil))
res, err := req.Send()
st.Expect(t, err, nil)
st.Expect(t, res.Ok, false)
st.Expect(t, res.StatusCode, 503)
st.Expect(t, calls, 4)
}
func TestRetryNetworkError(t *testing.T) {
req := gentleman.NewRequest()
req.URL("http://127.0.0.1:9123")
req.Use(New(nil))
res, err := req.Send()
st.Reject(t, err, nil)
st.Expect(t, strings.Contains(err.Error(), "connection refused"), true)
st.Expect(t, res.Ok, false)
st.Expect(t, res.StatusCode, 0)
}
// Timeout retry is not fully supported yet
func testRetryNetworkTimeout(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(1 * time.Second)
w.WriteHeader(200)
}))
defer ts.Close()
req := gentleman.NewRequest()
req.URL(ts.URL)
req.Use(timeout.Request(100 * time.Millisecond))
req.Use(New(nil))
res, err := req.Send()
st.Reject(t, err, nil)
st.Expect(t, strings.Contains(err.Error(), "request canceled"), true)
st.Expect(t, res.Ok, false)
st.Expect(t, res.StatusCode, 0)
}