This repository was archived by the owner on Jun 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_test.go
More file actions
108 lines (96 loc) · 2.89 KB
/
Copy pathhttp_test.go
File metadata and controls
108 lines (96 loc) · 2.89 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
package sources
import (
"context"
"errors"
"io"
"net/http"
"strings"
"testing"
"time"
)
type roundTripperFunc func(*http.Request) (*http.Response, error)
func (fn roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return fn(req)
}
func TestGetWithRetryRetriesTransportError(t *testing.T) {
original := upstreamRetryDelays
upstreamRetryDelays = []time.Duration{0, 0}
defer func() { upstreamRetryDelays = original }()
attempts := 0
client := &http.Client{Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
attempts++
if attempts < 3 {
return nil, errors.New("connection reset by peer")
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader("ok")),
Header: make(http.Header),
}, nil
})}
resp, err := getWithRetry(context.Background(), client, "https://example.com")
if err != nil {
t.Fatalf("getWithRetry returned error: %v", err)
}
defer resp.Body.Close()
if attempts != 3 {
t.Fatalf("expected 3 attempts, got %d", attempts)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %d", resp.StatusCode)
}
}
func TestGetWithRetryRetriesRetryableStatus(t *testing.T) {
original := upstreamRetryDelays
upstreamRetryDelays = []time.Duration{0}
defer func() { upstreamRetryDelays = original }()
attempts := 0
client := &http.Client{Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
attempts++
status := http.StatusBadGateway
if attempts == 2 {
status = http.StatusOK
}
return &http.Response{
StatusCode: status,
Body: io.NopCloser(strings.NewReader(http.StatusText(status))),
Header: make(http.Header),
}, nil
})}
resp, err := getWithRetry(context.Background(), client, "https://example.com")
if err != nil {
t.Fatalf("getWithRetry returned error: %v", err)
}
defer resp.Body.Close()
if attempts != 2 {
t.Fatalf("expected 2 attempts, got %d", attempts)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %d", resp.StatusCode)
}
}
func TestGetWithRetryDoesNotRetryNonRetryableStatus(t *testing.T) {
original := upstreamRetryDelays
upstreamRetryDelays = []time.Duration{time.Hour}
defer func() { upstreamRetryDelays = original }()
attempts := 0
client := &http.Client{Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
attempts++
return &http.Response{
StatusCode: http.StatusNotFound,
Body: io.NopCloser(strings.NewReader("missing")),
Header: make(http.Header),
}, nil
})}
resp, err := getWithRetry(context.Background(), client, "https://example.com")
if err != nil {
t.Fatalf("getWithRetry returned error: %v", err)
}
defer resp.Body.Close()
if attempts != 1 {
t.Fatalf("expected 1 attempt, got %d", attempts)
}
if resp.StatusCode != http.StatusNotFound {
t.Fatalf("unexpected status code: %d", resp.StatusCode)
}
}