-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrip_test.go
206 lines (167 loc) · 4.94 KB
/
trip_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package trip_test
import (
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/philippta/trip"
)
func ExampleDefault() {
var (
attempts = 3
delay = 50 * time.Millisecond
apiToken = os.Getenv("API_TOKEN")
)
client := &http.Client{
Transport: trip.Default(
trip.BearerToken(apiToken),
trip.Retry(attempts, delay, trip.RetryableStatusCodes...),
),
}
client.Get("https://api.example.com/endpoint")
}
func TestHeader(t *testing.T) {
var (
key = "X-Foo"
value = "bar"
)
roundTrip(func(r *http.Request) (*http.Response, error) {
assertEqual(t, r.Header.Get(key), value)
return nil, nil
}, trip.Header(key, value))
}
func TestBearerToken(t *testing.T) {
var (
token = "abc123"
expected = "Bearer abc123"
)
roundTrip(func(r *http.Request) (*http.Response, error) {
assertEqual(t, r.Header.Get("Authorization"), expected)
return nil, nil
}, trip.BearerToken(token))
}
func TestBasicAuth(t *testing.T) {
var (
username = "username"
password = "password"
expected = "Basic dXNlcm5hbWU6cGFzc3dvcmQ="
)
roundTrip(func(r *http.Request) (*http.Response, error) {
assertEqual(t, r.Header.Get("Authorization"), expected)
return nil, nil
}, trip.BasicAuth(username, password))
}
func TestUserAgent(t *testing.T) {
var (
userAgent = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
)
roundTrip(func(r *http.Request) (*http.Response, error) {
assertEqual(t, r.Header.Get("User-Agent"), userAgent)
return nil, nil
}, trip.UserAgent(userAgent))
}
func TestRetryNetwork(t *testing.T) {
var (
calls []time.Time
attempts = 3
delay = 2 * time.Millisecond
)
roundTrip(func(r *http.Request) (*http.Response, error) {
calls = append(calls, time.Now())
return nil, errors.New("network error")
}, trip.Retry(attempts, delay))
assertEqual(t, len(calls), attempts)
assertTimeRange(t, calls[0], calls[1], delay, time.Millisecond)
}
func TestRetryStatusCodes(t *testing.T) {
var (
calls []time.Time
attempts = 3
delay = 2 * time.Millisecond
)
roundTrip(func(r *http.Request) (*http.Response, error) {
calls = append(calls, time.Now())
return &http.Response{StatusCode: http.StatusBadGateway, Body: io.NopCloser(strings.NewReader(""))}, nil
}, trip.Retry(attempts, delay, trip.RetryableStatusCodes...))
assertEqual(t, len(calls), attempts)
assertTimeRange(t, calls[0], calls[1], delay, time.Millisecond)
}
func TestRetryStatusCodesSkipped(t *testing.T) {
var (
calls []time.Time
attempts = 3
delay = 2 * time.Millisecond
codes = []int{http.StatusTooManyRequests}
)
roundTrip(func(r *http.Request) (*http.Response, error) {
calls = append(calls, time.Now())
return &http.Response{StatusCode: http.StatusBadGateway, Body: io.NopCloser(strings.NewReader(""))}, nil
}, trip.Retry(attempts, delay, codes...))
assertEqual(t, len(calls), 1)
}
func TestIdempotencyKey(t *testing.T) {
var (
idems []string
attempts = 3
delay = 2 * time.Millisecond
)
roundTrip(func(r *http.Request) (*http.Response, error) {
idems = append(idems, r.Header.Get("Idempotency-Key"))
return nil, errors.New("network error")
}, trip.Retry(attempts, delay), trip.IdempotencyKey())
assertEqual(t, len(idems), attempts)
assertNotEqual(t, idems[0], "")
assertEqual(t, idems[0], idems[1])
}
func TestLogger(t *testing.T) {
logf := func(format string, v ...any) {
msg := fmt.Sprintf(format, v...)
assertPrefix(t, msg, "POST http://example.com/foo?bar=yes - 200 OK -")
}
roundTrip(func(r *http.Request) (*http.Response, error) {
return &http.Response{Status: "200 OK", StatusCode: 200, Body: io.NopCloser(strings.NewReader("foo"))}, nil
}, trip.Logger(logf))
}
func TestLoggerError(t *testing.T) {
logf := func(format string, v ...any) {
msg := fmt.Sprintf(format, v...)
assertPrefix(t, msg, `POST http://example.com/foo?bar=yes - error:"network error" -`)
}
roundTrip(func(r *http.Request) (*http.Response, error) {
return nil, errors.New("network error")
}, trip.Logger(logf))
}
func roundTrip(f trip.RoundTripperFunc, trips ...trip.TripFunc) {
req := httptest.NewRequest("POST", "http://example.com/foo?bar=yes", nil)
transport := trip.New(f, trips...)
transport.RoundTrip(req)
}
func noop(r *http.Request) (*http.Response, error) {
return nil, nil
}
func assertEqual[T comparable](t *testing.T, a T, b T) {
if a != b {
t.Errorf("got: %v, expected: %v", a, b)
}
}
func assertPrefix(t *testing.T, a, b string) {
if !strings.HasPrefix(a, b) {
t.Errorf("got: %v, expected to start with: %v", a, b)
}
}
func assertNotEqual[T comparable](t *testing.T, a T, b T) {
if a == b {
t.Errorf("got: %v and %v, expected something else", a, b)
}
}
func assertTimeRange(t *testing.T, a time.Time, b time.Time, expectedDiff, timeRange time.Duration) {
diff := b.Sub(a)
if diff-expectedDiff > timeRange || diff-expectedDiff < -timeRange {
t.Errorf("took: %v, expected: %v, not in range of %v", diff, expectedDiff, timeRange)
}
}