forked from ComboStrikeHQ/vcr-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
httpvcr.go
185 lines (147 loc) · 3.57 KB
/
httpvcr.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
package httpvcr
import (
"context"
"fmt"
"net/http"
"sync"
)
type Mode uint32
const (
ModeStopped Mode = iota
ModeRecord
ModeReplay
)
type VCR struct {
options Options
ctx context.Context
ctxCancel context.CancelFunc
mode Mode
Cassette *cassette
FilterMap map[string]string
BeforeRequest func(Mode, *http.Request)
URLRewriter func(string) string
originalTransport http.RoundTripper
mu sync.Mutex
}
// VCR is a http.RoundTripper and can be used in http clients as such
var _ http.RoundTripper = &VCR{}
type Options struct {
HTTPDefaultOverride bool
GZipCassette bool
EpisodeMatcher EpisodeMatcher
}
var DefaultOptions = Options{
HTTPDefaultOverride: true,
GZipCassette: false,
EpisodeMatcher: &DefaultEpisodeMatcher{},
}
func New(cassetteName string, opts ...Options) *VCR {
options := DefaultOptions
if len(opts) > 0 {
options = opts[0]
}
if options.EpisodeMatcher == nil {
options.EpisodeMatcher = &DefaultEpisodeMatcher{}
}
return &VCR{
options: options,
mode: ModeStopped,
Cassette: &cassette{name: cassetteName,
Gzip: options.GZipCassette,
episodeMatcher: options.EpisodeMatcher,
},
FilterMap: make(map[string]string),
}
}
// Start starts a VCR session with the given cassette name.
// Records episodes if the cassette file does not exists.
// Otherwise plays back recorded episodes.
func (v *VCR) Start(ctx context.Context) {
v.mu.Lock()
defer v.mu.Unlock()
if v.mode != ModeStopped {
panic("httpvcr: session already started!")
}
v.ctx, v.ctxCancel = context.WithCancel(ctx)
v.originalTransport = http.DefaultTransport
if v.options.HTTPDefaultOverride {
http.DefaultTransport = v
}
if v.Cassette.Exists() {
v.mode = ModeReplay
v.Cassette.read()
} else {
v.mode = ModeRecord
}
}
// Stop stops the VCR session and writes the cassette file (when recording)
func (v *VCR) Stop() {
v.mu.Lock()
defer v.mu.Unlock()
if v.mode == ModeStopped {
return
}
if v.mode == ModeRecord {
v.Cassette.write()
}
if v.options.HTTPDefaultOverride && v.originalTransport != nil {
http.DefaultTransport = v.originalTransport
}
v.mode = ModeStopped
v.Cassette.episodeMatcher.Stop()
v.ctxCancel()
}
func (v *VCR) Mode() Mode {
v.mu.Lock()
defer v.mu.Unlock()
return v.mode
}
// FilterData allows replacement of sensitive data with a dummy-string
func (v *VCR) FilterResponseBody(original string, replacement string) {
v.mu.Lock()
defer v.mu.Unlock()
v.FilterMap[original] = replacement
}
func (v *VCR) RoundTrip(request *http.Request) (*http.Response, error) {
if v.ctx.Err() == context.Canceled {
return nil, fmt.Errorf("httpvcr: stopped")
}
if v.mode == ModeStopped {
return v.originalTransport.RoundTrip(request)
}
vcrReq := newVCRRequest(request, v.FilterMap)
var vcrRes *VCRResponse
if v.BeforeRequest != nil {
v.BeforeRequest(v.mode, request)
}
if v.mode == ModeRecord {
response, err := v.originalTransport.RoundTrip(request)
if err != nil {
return nil, err
}
vcrRes = newVCRResponse(response)
if v.URLRewriter != nil {
vcrReq.URL = v.URLRewriter(vcrReq.URL)
}
e := Episode{Request: vcrReq, Response: vcrRes}
v.Cassette.Episodes = append(v.Cassette.Episodes, e)
} else {
if v.URLRewriter != nil {
vcrReq.URL = v.URLRewriter(vcrReq.URL)
}
e, err := v.Cassette.matchEpisode(vcrReq)
if err != nil {
return nil, err
}
vcrRes = e.Response
}
if v.mode == ModeReplay {
if len(v.Cassette.Episodes) == 0 {
v.Stop()
}
}
return vcrRes.httpResponse(), nil
}
func (v *VCR) Done() <-chan struct{} {
return v.ctx.Done()
}