-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuntil_test.go
More file actions
38 lines (30 loc) · 844 Bytes
/
until_test.go
File metadata and controls
38 lines (30 loc) · 844 Bytes
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
package interceptor_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/tiny-go/interceptor"
)
func Test_Retry_UntilStatusCodeIs(t *testing.T) {
handler := make(serveChain, 3)
handler <- serveWithCode(http.StatusNotFound)
handler <- serveWithCode(http.StatusBadRequest)
handler <- serveWithCode(http.StatusOK)
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.UntilStatusCodeIs(200),
).Then(http.DefaultClient).Do(req)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
defer req.Body.Close()
if res.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %d", res.StatusCode)
}
}