forked from seborama/govcr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cassette.go
645 lines (540 loc) · 16.3 KB
/
cassette.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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
package govcr
import (
"bytes"
"compress/gzip"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"sync/atomic"
)
// request is a recorded HTTP request.
type request struct {
Method string
URL *url.URL
Header http.Header
Body []byte
}
// Request transforms internal "request" to a filter "Request".
func (r request) Request() Request {
res := Request{
Header: r.Header,
Body: r.Body,
Method: r.Method,
}
if r.URL != nil {
// res.URL = *r.URL
res.URL = *copyURL(r.URL)
}
return res
}
// response is a recorded HTTP response.
type response struct {
Status string
StatusCode int
Proto string
ProtoMajor int
ProtoMinor int
Header http.Header
Body []byte
ContentLength int64
TransferEncoding []string
Trailer http.Header
TLS *tls.ConnectionState
}
// Response returns the internal "response" to a filter "Response".
func (r response) Response(req Request) Response {
return Response{
req: req,
Body: r.Body,
Header: r.Header,
StatusCode: r.StatusCode,
}
}
// track is a recording (HTTP request + response) in a cassette.
type track struct {
Request request
Response response
ErrType string
ErrMsg string
// replayed indicates whether the track has already been processed in the cassette playback.
replayed bool
}
func (t *track) response(req *http.Request) (*http.Response, error) {
var (
err error
resp = &http.Response{}
)
// create a ReadCloser to supply to resp
bodyReadCloser := toReadCloser(t.Response.Body)
// create error object
switch t.ErrType {
case "*net.OpError":
err = &net.OpError{
Op: "govcr",
Net: "govcr",
Source: nil,
Addr: nil,
Err: errors.New(t.ErrType + ": " + t.ErrMsg),
}
case "":
err = nil
default:
err = errors.New(t.ErrType + ": " + t.ErrMsg)
}
if err != nil {
// No need to parse the response.
// By convention, when an HTTP error occurred, the response should be empty
// (or Go's http package will show a warning message at runtime).
return resp, err
}
// re-create the response object from track record
respTLS := t.Response.TLS
resp.Status = t.Response.Status
resp.StatusCode = t.Response.StatusCode
resp.Proto = t.Response.Proto
resp.ProtoMajor = t.Response.ProtoMajor
resp.ProtoMinor = t.Response.ProtoMinor
resp.Header = t.Response.Header
resp.Body = bodyReadCloser
resp.ContentLength = t.Response.ContentLength
resp.TransferEncoding = t.Response.TransferEncoding
resp.Trailer = t.Response.Trailer
// See notes on http.Response.Request - Body is nil because it has already been consumed
resp.Request = copyRequestWithoutBody(req)
resp.TLS = respTLS
return resp, nil
}
// newTrack creates a new track from an HTTP request and response.
func newTrack(req *http.Request, resp *http.Response, reqErr error) (*track, error) {
var (
k7Request request
k7Response response
)
// build request object
if req != nil {
bodyData, err := readRequestBody(req)
if err != nil {
return nil, err
}
k7Request = request{
Method: req.Method,
URL: req.URL,
Header: req.Header,
Body: bodyData,
}
}
// build response object
if resp != nil {
bodyData, err := readResponseBody(resp)
if err != nil {
return nil, err
}
k7Response = response{
Status: resp.Status,
StatusCode: resp.StatusCode,
Proto: resp.Proto,
ProtoMajor: resp.ProtoMajor,
ProtoMinor: resp.ProtoMinor,
Header: resp.Header,
Body: bodyData,
ContentLength: resp.ContentLength,
TransferEncoding: resp.TransferEncoding,
Trailer: resp.Trailer,
TLS: resp.TLS,
}
}
// build track object
var reqErrType, reqErrMsg string
if reqErr != nil {
reqErrType = fmt.Sprintf("%T", reqErr)
reqErrMsg = reqErr.Error()
}
track := &track{
Request: k7Request,
Response: k7Response,
ErrType: reqErrType,
ErrMsg: reqErrMsg,
}
return track, nil
}
// Stats holds information about the cassette and
// VCR runtime.
type Stats struct {
// TracksLoaded is the number of tracks that were loaded from the cassette.
TracksLoaded int32
// TracksRecorded is the number of new tracks recorded by VCR.
TracksRecorded int32
// TracksPlayed is the number of tracks played back straight from the cassette.
// I.e. tracks that were already present on the cassette and were played back.
TracksPlayed int32
}
// cassette contains a set of tracks.
type cassette struct {
Name string
Path string `json:"-"`
Tracks []track
trackSliceMutex *sync.RWMutex
// the following variables are not exported as they are not serialised
tracksLoaded int32
removeTLS bool
}
// newCassette creates a ready to use new cassette.
func newCassette(name string, path string) *cassette {
return &cassette{Name: name, Path: path, trackSliceMutex: &sync.RWMutex{}}
}
// isLongPlay returns true if the cassette content is compressed.
func (k7 *cassette) isLongPlay() bool {
return strings.HasSuffix(k7.Name, ".gz")
}
// TODO - this feels wrong - the cassette should just replay, not replace the track resp.req with the live req
// if it must be done, then it should be done somewhere else, either vcrTransport (or PCB, to be confirmed)
func (k7 *cassette) replayResponse(trackNumber int32, req *http.Request) (*http.Response, error) {
if trackNumber >= k7.NumberOfTracks() {
return nil, nil
}
k7.trackSliceMutex.Lock()
defer k7.trackSliceMutex.Unlock()
track := &k7.Tracks[trackNumber]
// mark the track as replayed so it doesn't get re-used
track.replayed = true
return track.response(req)
}
// saveCassette writes a cassette to file.
func (k7 *cassette) save() error {
k7.trackSliceMutex.Lock()
defer k7.trackSliceMutex.Unlock()
data, err := json.Marshal(k7)
if err != nil {
return err
}
tData, err := transformInterfacesInJSON(data)
if err != nil {
return err
}
var iData bytes.Buffer
if err := json.Indent(&iData, tData, "", " "); err != nil {
return err
}
filename := cassetteNameToFilename(k7.Name, k7.Path)
path := filepath.Dir(filename)
if err := os.MkdirAll(path, 0750); err != nil {
return err
}
gData, err := k7.gzipFilter(iData)
if err != nil {
return err
}
return ioutil.WriteFile(filename, gData, 0640)
}
// gzipFilter compresses the cassette data in gzip format if the cassette
// name ends with '.gz', otherwise data is left as is (i.e. de-compressed)
func (k7 *cassette) gzipFilter(data bytes.Buffer) ([]byte, error) {
if k7.isLongPlay() {
return compress(data.Bytes())
}
return data.Bytes(), nil
}
// gunzipFilter de-compresses the cassette data in gzip format if the cassette
// name ends with '.gz', otherwise data is left as is (i.e. de-compressed)
func (k7 *cassette) gunzipFilter(data []byte) ([]byte, error) {
if k7.isLongPlay() {
return decompress(data)
}
return data, nil
}
// addTrack adds a track to a cassette.
func (k7 *cassette) addTrack(track *track) {
if k7.removeTLS { // TODO: refactor this to be handled by the PCB?
track.Response.TLS = nil
}
k7.trackSliceMutex.Lock()
defer k7.trackSliceMutex.Unlock()
k7.Tracks = append(k7.Tracks, *track)
}
// Track retrieves the requested track number.
// '0' is the first track.
func (k7 *cassette) Track(trackNumber int32) track {
k7.trackSliceMutex.RLock()
defer k7.trackSliceMutex.RUnlock()
return k7.Tracks[trackNumber]
}
// Stats returns the cassette's Stats.
func (k7 *cassette) Stats() Stats {
stats := Stats{}
stats.TracksLoaded = atomic.LoadInt32(&k7.tracksLoaded)
stats.TracksRecorded = k7.NumberOfTracks() - stats.TracksLoaded
stats.TracksPlayed = k7.tracksPlayed() - stats.TracksRecorded
return stats
}
func (k7 *cassette) tracksPlayed() int32 {
replayed := int32(0)
k7.trackSliceMutex.RLock()
defer k7.trackSliceMutex.RUnlock()
for _, t := range k7.Tracks {
if t.replayed {
replayed++
}
}
return replayed
}
func (k7 *cassette) NumberOfTracks() int32 {
k7.trackSliceMutex.RLock()
defer k7.trackSliceMutex.RUnlock()
return int32(len(k7.Tracks))
}
// DeleteCassette removes the cassette file from disk.
func DeleteCassette(cassetteName, cassettePath string) error {
filename := cassetteNameToFilename(cassetteName, cassettePath)
err := os.Remove(filename)
if os.IsNotExist(err) {
// the file does not exist so this is not an error since we wanted it gone!
err = nil
}
return err
}
// CassetteExistsAndValid verifies a cassette file exists and is seemingly valid.
func CassetteExistsAndValid(cassetteName, cassettePath string) bool {
_, err := readCassetteFromFile(cassetteName, cassettePath)
return err == nil
}
// cassetteNameToFilename returns the filename associated to the cassette.
func cassetteNameToFilename(cassetteName, cassettePath string) string {
if cassetteName == "" || cassetteName == ".gz" {
return ""
}
if cassettePath == "" {
cassettePath = defaultCassettePath
}
fPath, err := filepath.Abs(filepath.Join(cassettePath, adjustCassetteName(cassetteName)))
if err != nil {
log.Fatal(err)
}
return fPath
}
// adjustCassetteName moves the '.gz' suffix to the end of the cassette name
// instead of the middle
func adjustCassetteName(cassetteName string) string {
if strings.HasSuffix(cassetteName, ".gz") {
return strings.TrimSuffix(cassetteName, ".gz") + ".cassette.gz"
}
return cassetteName + ".cassette"
}
// transformInterfacesInJSON looks for known properties in the JSON that are defined as interface{}
// in their original Go structure and don't Unmarshal correctly.
//
// Example x509.Certificate.PublicKey:
// When the type is rsa.PublicKey, Unmarshal attempts to map property "N" to a float64 because it is a number.
// However, it really is a big.Int which does not fit float64 and makes Unmarshal fail.
//
// This is not an ideal solution but it works. In the future, we could consider adding a property that
// records the original type and re-creates it post Unmarshal.
func transformInterfacesInJSON(jsonString []byte) ([]byte, error) {
// TODO: precompile this regexp perhaps via a receiver
regex, err := regexp.Compile(`("PublicKey":{"N":)([0-9]+),`)
if err != nil {
return []byte{}, err
}
return []byte(regex.ReplaceAllString(string(jsonString), `$1"$2",`)), nil
}
func loadCassette(cassetteName, cassettePath string) (*cassette, error) {
k7, err := readCassetteFromFile(cassetteName, cassettePath)
if err != nil {
return nil, err
}
// provide an empty cassette as a minimum
if k7 == nil {
k7 = newCassette(cassetteName, cassettePath)
}
// initial stats
k7.tracksLoaded = k7.NumberOfTracks()
return k7, nil
}
// readCassetteFromFile reads the cassette file, if present.
func readCassetteFromFile(cassetteName, cassettePath string) (*cassette, error) {
filename := cassetteNameToFilename(cassetteName, cassettePath)
k7 := newCassette(cassetteName, cassettePath)
data, err := ioutil.ReadFile(filename)
if os.IsNotExist(err) {
return k7, nil
} else if err != nil {
return nil, err
}
cData, err := k7.gunzipFilter(data)
if err != nil {
return nil, err
}
// NOTE: Properties which are of type 'interface{}' are not handled very well
if err := json.Unmarshal(cData, k7); err != nil {
return nil, err
}
return k7, nil
}
// recordNewTrackToCassette saves a new track to a cassette.
func recordNewTrackToCassette(cassette *cassette, req *http.Request, resp *http.Response, httpErr error) error {
// create track
track, err := newTrack(req, resp, httpErr)
if err != nil {
return err
}
// mark track as replayed since it's coming from a live request!
track.replayed = true
// add track to cassette
cassette.addTrack(track)
// save cassette
return cassette.save()
}
// compress data and return the result
func compress(data []byte) ([]byte, error) {
var out bytes.Buffer
w := gzip.NewWriter(&out)
if _, err := io.Copy(w, bytes.NewBuffer(data)); err != nil {
return nil, err
}
if err := w.Close(); err != nil {
return nil, err
}
return out.Bytes(), nil
}
// decompress data and return the result
func decompress(data []byte) ([]byte, error) {
r, err := gzip.NewReader(bytes.NewBuffer(data))
if err != nil {
return nil, err
}
data, err = ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return data, nil
}
// Track Filters
// TrackFilter allows modifying information that is being stored in a track
//
// The filters receive a deep copy of the request and response, that can be modified.
type TrackFilter func(*http.Request, *http.Response, error) (*http.Request, *http.Response)
// TrackFilters is a slice of TrackFilter
type TrackFilters []TrackFilter
// TrackRequestDeleteHeaderKeys will delete one or more header keys on the request
// before the request is matched against the cassette.
func TrackRequestDeleteHeaderKeys(keys ...string) TrackFilter {
return func(req *http.Request, resp *http.Response, err error) (*http.Request, *http.Response) {
for _, key := range keys {
req.Header.Del(key)
}
return req, resp
}
}
// TrackResponseDeleteHeaderKeys will delete one or more header keys on the request
// before the request is matched against the cassette.
func TrackResponseDeleteHeaderKeys(keys ...string) TrackFilter {
return func(req *http.Request, resp *http.Response, err error) (*http.Request, *http.Response) {
if resp == nil || resp.Header == nil {
return req, resp
}
for _, key := range keys {
resp.Header.Del(key)
}
return req, resp
}
}
// TrackRequestChangeBody will allows to change the body.
// Supply a function that does input to output transformation.
func TrackRequestChangeBody(fn func(b []byte) []byte) TrackFilter {
return func(req *http.Request, resp *http.Response, err error) (*http.Request, *http.Response) {
body, err2 := readRequestBody(req) // XXX does too much, don't recreate body
if err2 != nil {
return req, resp
}
body = fn(body)
req.Body = toReadCloser(body)
req.ContentLength = int64(len(body))
return req, resp
}
}
// TrackResponseChangeBody will allows to change the body.
// Supply a function that does input to output transformation.
func TrackResponseChangeBody(fn func(b []byte) []byte) TrackFilter {
return func(req *http.Request, resp *http.Response, err error) (*http.Request, *http.Response) {
if resp == nil || resp.Header == nil {
return req, resp
}
body, err2 := readResponseBody(resp) // XXX does too much, don't recreate body
if err2 != nil {
return req, resp
}
body = fn(body)
resp.Body = toReadCloser(body)
resp.ContentLength = int64(len(body))
return req, resp
}
}
// OnMethod will return a new filter that will only apply 'r'
// if the method of the request matches.
// Original filter is unmodified.
func (r TrackFilter) OnMethod(method string) TrackFilter {
return func(req *http.Request, resp *http.Response, err error) (*http.Request, *http.Response) {
if req.Method != method {
return req, resp
}
return r(req, resp, err)
}
}
// OnPath will return a track filter that will only apply 'r'
// if the url string of the request matches the supplied regex.
// Original filter is unmodified.
func (r TrackFilter) OnPath(pathRegEx string) TrackFilter {
if pathRegEx == "" {
pathRegEx = ".*"
}
re := regexp.MustCompile(pathRegEx)
return func(req *http.Request, resp *http.Response, err error) (*http.Request, *http.Response) {
if !re.MatchString(req.URL.String()) {
return req, resp
}
return r(req, resp, err)
}
}
// OnStatus will return a Track filter that will only apply 'r' if the response status matches.
// Original filter is unmodified.
func (r TrackFilter) OnStatus(status int) TrackFilter {
return func(req *http.Request, resp *http.Response, err error) (*http.Request, *http.Response) {
if resp == nil || resp.StatusCode != status {
return req, resp
}
return r(req, resp, err)
}
}
// Add one or more filters at the end of the filter chain.
func (r *TrackFilters) Add(filters ...TrackFilter) {
v := *r
v = append(v, filters...)
*r = v
}
// Prepend one or more filters before the current ones.
func (r *TrackFilters) Prepend(filters ...TrackFilter) {
src := *r
dst := make(TrackFilters, 0, len(filters)+len(src))
dst = append(dst, filters...)
*r = append(dst, src...)
}
// combined returns the filters as a single filter.
func (r TrackFilters) combined() TrackFilter {
return func(req *http.Request, resp *http.Response, err error) (*http.Request, *http.Response) {
for _, filter := range r {
req, resp = filter(req, resp, err)
}
return req, resp
}
}