-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp_test.go
More file actions
324 lines (269 loc) · 7.34 KB
/
http_test.go
File metadata and controls
324 lines (269 loc) · 7.34 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package retry
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"sync"
"testing"
"time"
)
type testTransport struct {
status []int
}
func (t *testTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if req.Body == nil {
return nil, errors.New("req.Body == nil")
}
defer req.Body.Close()
if a := Attempt(req.Context()); a != 0 {
if got, want := req.Header.Get("Retry-Attempt"), strconv.Itoa(a); got != want {
return nil, fmt.Errorf("req.Header.Get(\"Retry-Attempt\") = %q, want %q", got, want)
}
}
payload, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}
if got, want := string(payload), "request payload"; got != want {
return nil, fmt.Errorf("request payload: got %q, want %q", got, want)
}
if len(t.status) == 0 {
return nil, errors.New("no more status codes")
}
res := &http.Response{}
res.StatusCode, t.status = t.status[0], t.status[1:]
return res, nil
}
func TestTransport(t *testing.T) {
cases := []struct {
transport *testTransport
opts []Option
wantStatus int
wantErr bool
}{
{
transport: &testTransport{status: []int{500, 200}},
wantStatus: 200,
},
{
transport: &testTransport{status: []int{599, 403}},
wantStatus: 403,
},
{
transport: &testTransport{status: []int{503, 502, 200}},
opts: []Option{Attempts(2)},
wantErr: true,
},
}
for _, c := range cases {
client := &http.Client{
Transport: NewTransport(c.transport, c.opts...),
}
res, err := client.Post("http://example.com/", "text/plain", strings.NewReader("request payload"))
if err != nil {
if !c.wantErr {
t.Errorf("Get() = %v, want success", err)
}
continue
}
if err == nil && c.wantErr {
t.Errorf("Get() = %v, want failure", err)
continue
}
if res.StatusCode != c.wantStatus {
t.Errorf("Get().StatusCode = %d, want %d", res.StatusCode, c.wantStatus)
continue
}
}
}
func ExampleTransport() {
c := &http.Client{
Transport: &Transport{},
}
// Caveat: there is no specific context associated with this request.
// The net/http package uses the background context in that case.
// That means that this request will be retried indefinitely until it succeeds.
res, err := c.Get("http://example.com/")
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode >= 500 && res.StatusCode < 600 {
panic("this does not happen. HTTP 5xx errors are reported as errors.")
}
// use "res"
}
func ExampleTransport_withTimeout() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
c := &http.Client{
Transport: &Transport{},
}
// The context needs to be added to the request via http.Request.WithContext().
// The net/http package defaults to using the background context, which is never cancelled.
// That's why NewRequest()/Do() is used here instead of the more
// convenient Get(), Head() and Post() short-hands.
req, err := http.NewRequest(http.MethodPost, "https://example.com/",
strings.NewReader(`{"example":true}`))
if err != nil {
log.Fatalf("NewRequest() = %v", err)
}
res, err := c.Do(req.WithContext(ctx))
if err != nil {
log.Printf("Do() = %v", err)
return
}
defer res.Body.Close()
// use "res"
}
func ExampleTransport_withOptions() {
c := &http.Client{
Transport: NewTransport(http.DefaultTransport, Attempts(3)),
}
// Caveat: there is no specific context associated with this request.
// The net/http package uses the background context, i.e. cancellation does not work.
res, err := c.Get("http://example.com/")
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
// use "res"
}
// testResponseWriter writes a response to a bytes.Buffer, i.e. to memory.
// It implements the http.ResponseWriter interface.
type testResponseWriter struct {
header http.Header
buffer bytes.Buffer
status int
}
func (w *testResponseWriter) Header() http.Header {
return w.header
}
func (w *testResponseWriter) Write(data []byte) (int, error) {
if w.status == 0 {
w.WriteHeader(http.StatusOK)
}
return w.buffer.Write(data)
}
func (w *testResponseWriter) WriteHeader(s int) {
if w.status != 0 {
panic(fmt.Sprintf("w.status = %d, want 0", w.status))
}
w.status = s
}
// testBudgetTransport is an http.RoundTripper that simply calls an http.Handler.
type testBudgetTransport struct {
http.Handler
}
func (t *testBudgetTransport) RoundTrip(req *http.Request) (*http.Response, error) {
w := &testResponseWriter{
header: make(http.Header),
}
t.Handler.ServeHTTP(w, req)
return &http.Response{
Status: http.StatusText(w.status),
StatusCode: w.status,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: w.header,
Body: io.NopCloser(&w.buffer),
ContentLength: int64(w.buffer.Len()),
Request: req,
}, nil
}
// testHandler is a handler returning success for every other request and 503
// for the remaining requests.
type testHandler struct {
sync.Mutex
totalCount int
}
func (h *testHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
h.Lock()
defer h.Unlock()
h.totalCount++
// 50% of requests are "failures". Do blocks of two, because the
// requests alternate between initial requests and retries and this way
// we get all retry/failure combinations.
if h.totalCount%2 == 1 {
http.Error(w, "Service Unavailable", http.StatusServiceUnavailable)
return
}
fmt.Fprintln(w, "Ok")
}
func TestBudgetHandler(t *testing.T) {
if os.Getenv("RETRY_ENDTOEND") == "" {
t.Skip("set the \"RETRY_ENDTOEND\" environment variable to enable this test")
}
// testBudgetHandler always produces 25% retries.
// max 24% retries -> in overload -> status 429
testBudgetHandler(t, 0.24, 429)
// max 26% retries -> not overloaded -> status 503
testBudgetHandler(t, 0.26, 503)
}
func testBudgetHandler(t *testing.T, ratio float64, wantStatus int) {
t.Helper()
ticker := time.NewTicker(time.Second / 100)
wg := &sync.WaitGroup{}
hndl := &testHandler{}
client := &http.Client{
Transport: &testBudgetTransport{
Handler: &BudgetHandler{
Handler: hndl,
Budget: Budget{
Ratio: ratio,
},
},
},
}
responsesByStatus := map[int]int{}
responsesByStatusLock := &sync.Mutex{}
for i := 0; i < 200; i++ {
<-ticker.C
wg.Add(1)
go func(n int) {
defer wg.Done()
req, err := http.NewRequest(http.MethodGet, "http://example.com/", nil)
if err != nil {
t.Errorf("http.NewRequest() = %v", err)
return
}
if n%4 == 0 {
req.Header.Set("Retry-Attempt", "1")
}
res, err := client.Do(req)
if err != nil {
t.Errorf("client.Get() = %v", err)
return
}
// avoid start-up effects by only accounting the second
// half of requests.
if n < 100 {
return
}
responsesByStatusLock.Lock()
defer responsesByStatusLock.Unlock()
responsesByStatus[res.StatusCode]++
}(i)
}
wg.Wait()
for status, count := range responsesByStatus {
t.Logf("HTTP %d: %d responses", status, count)
}
if got, want := responsesByStatus[200], 50; got != want {
t.Errorf("responsesByStatus[200] = %d, want %d", got, want)
}
if got, want := responsesByStatus[429]+responsesByStatus[503], 50; got != want {
t.Errorf("responsesByStatus[429] + responsesByStatus[503] = %d, want %d", got, want)
}
if got, want := responsesByStatus[wantStatus], 50; got != want {
t.Errorf("responsesByStatus[%d] = %d, want %d", wantStatus, got, want)
}
}