-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhile_test.go
More file actions
47 lines (38 loc) · 1.09 KB
/
while_test.go
File metadata and controls
47 lines (38 loc) · 1.09 KB
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
package interceptor_test
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/tiny-go/interceptor"
)
func Test_Retry_WhileFails(t *testing.T) {
// 1 try + 3 retries = 4 calls in total
handler := make(serveChain, 4)
handler <- serveWithCode(http.StatusServiceUnavailable)
handler <- serveWithCode(http.StatusInternalServerError)
handler <- serveWithCode(http.StatusInternalServerError)
handler <- serveWithCode(http.StatusNotFound)
ts := httptest.NewServer(handler)
defer ts.Close()
req, err := http.NewRequest(http.MethodPost, ts.URL, http.NoBody)
if err != nil {
t.Fatalf("cannot instantiate a client: %s", err)
}
res, err := interceptor.New(
interceptor.Retry(3),
interceptor.WhileFails(func(r *http.Response) error {
if r.StatusCode >= 500 {
return fmt.Errorf("unexpected status code: %d", r.StatusCode)
}
return nil
}),
).Then(http.DefaultClient).Do(req)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
defer req.Body.Close()
if res.StatusCode != http.StatusNotFound {
t.Fatalf("unexpected status code: %d", res.StatusCode)
}
}