-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathclient_test.go
More file actions
80 lines (68 loc) · 1.85 KB
/
client_test.go
File metadata and controls
80 lines (68 loc) · 1.85 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
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
package wrappers
import (
"errors"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
"time"
)
type mockReadCloser struct{}
func (m *mockReadCloser) Read(p []byte) (n int, err error) {
return 0, nil
}
func (m *mockReadCloser) Close() error {
return nil
}
func TestRetryHTTPRequest_Success(t *testing.T) {
fn := func() (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: &mockReadCloser{},
}, nil
}
resp, err := retryHTTPRequest(fn, retryAttempts, retryDelay*time.Millisecond)
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
func TestRetryHTTPRequest_RetryOnBadGateway(t *testing.T) {
attempts := 0
fn := func() (*http.Response, error) {
attempts++
if attempts < retryAttempts {
return &http.Response{
StatusCode: http.StatusBadGateway,
Body: &mockReadCloser{},
}, nil
}
return &http.Response{
StatusCode: http.StatusOK,
Body: &mockReadCloser{},
}, nil
}
resp, err := retryHTTPRequest(fn, retryAttempts, retryDelay*time.Millisecond)
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, retryAttempts, attempts)
}
func TestRetryHTTPRequest_Fail(t *testing.T) {
fn := func() (*http.Response, error) {
return nil, errors.New("network error")
}
resp, err := retryHTTPRequest(fn, retryAttempts, retryDelay*time.Millisecond)
assert.Error(t, err)
assert.Nil(t, resp)
}
func TestRetryHTTPRequest_EndWithBadGateway(t *testing.T) {
fn := func() (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusBadGateway,
Body: &mockReadCloser{},
}, nil
}
resp, err := retryHTTPRequest(fn, retryAttempts, retryDelay*time.Millisecond)
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, http.StatusBadGateway, resp.StatusCode)
}