-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtestutils.go
80 lines (61 loc) · 1.58 KB
/
testutils.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
package templar
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"time"
)
type recordingSender struct {
w *httptest.ResponseRecorder
}
func newRecordingSender() *recordingSender {
return &recordingSender{httptest.NewRecorder()}
}
func (r *recordingSender) Send(res *http.Response) io.Writer {
for k, v := range res.Header {
r.w.Header()[k] = v
}
r.w.WriteHeader(res.StatusCode)
return r.w
}
type slowTransport struct {
seconds time.Duration
}
func (st *slowTransport) RoundTrip(req *http.Request) (*http.Response, error) {
response := &http.Response{
Request: req,
StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader(fmt.Sprintf("now: %s", time.Now()))),
}
time.Sleep(st.seconds * time.Second)
return response, nil
}
func (st *slowTransport) CancelRequest(req *http.Request) {}
type slowTransportFallback struct {
seconds time.Duration
fallback bool
}
func (st *slowTransportFallback) RoundTrip(req *http.Request) (*http.Response, error) {
response := &http.Response{
Request: req,
StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader(fmt.Sprintf("now: %s", time.Now()))),
}
time.Sleep(st.seconds * time.Second)
return response, nil
}
func (st *slowTransportFallback) CancelRequest(req *http.Request) {}
func (st *slowTransportFallback) Fallback(req *http.Request) (*http.Response, error) {
if !st.fallback {
return nil, nil
}
response := &http.Response{
Request: req,
StatusCode: 201,
Body: ioutil.NopCloser(strings.NewReader(fmt.Sprintf("now: %s", time.Now()))),
}
return response, nil
}