forked from ComboStrikeHQ/vcr-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cassette.go
168 lines (138 loc) · 3.31 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
package httpvcr
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
)
type EpisodeMatcher interface {
Init(episodes []Episode)
MatchEpisode(request *VCRRequest) (*Episode, error)
Stop()
}
type DefaultEpisodeMatcher struct {
episodes []Episode
}
func (m *DefaultEpisodeMatcher) Init(episodes []Episode) {
m.episodes = episodes
}
func (m *DefaultEpisodeMatcher) MatchEpisode(request *VCRRequest) (*Episode, error) {
if len(m.episodes) == 0 {
panic("httpvcr: no more episodes!")
}
e := m.episodes[0]
expected := e.Request
if expected.Method != request.Method {
panicEpisodeMismatch(request, "Method", expected.Method, request.Method)
}
if expected.URL != request.URL {
panicEpisodeMismatch(request, "URL", expected.URL, request.URL)
}
if !reflect.DeepEqual(expected.Body, request.Body) {
panicEpisodeMismatch(request, "Body", string(expected.Body[:]), string(request.Body[:]))
}
m.episodes = m.episodes[1:]
return &e, nil
}
func (m *DefaultEpisodeMatcher) Stop() {
// no-op
}
type cassette struct {
name string
Episodes []Episode
Gzip bool
episodeMatcher EpisodeMatcher
}
type Episode struct {
Request *VCRRequest
Response *VCRResponse
}
func (c *cassette) Name() string {
return c.name
}
func (c *cassette) Filename() string {
if c.Gzip {
return "fixtures/vcr/" + c.name + ".json.gz"
} else {
return "fixtures/vcr/" + c.name + ".json"
}
}
func (c *cassette) Exists() bool {
_, err := os.Stat(c.Filename())
return err == nil
}
func (c *cassette) read() {
var fileData, jsonData []byte
fileData, _ = ioutil.ReadFile(c.Filename())
if c.Gzip {
var data bytes.Buffer
err := gunzipWrite(&data, fileData)
if err != nil {
panic("httpvcr: gzip read failed")
}
jsonData = data.Bytes()
} else {
jsonData = fileData
}
err := json.Unmarshal(jsonData, c)
if err != nil {
panic("httpvcr: cannot parse json!")
}
c.episodeMatcher.Init(c.Episodes)
}
func (c *cassette) write() {
jsonData, _ := json.Marshal(c)
var jsonOut bytes.Buffer
json.Indent(&jsonOut, jsonData, "", " ")
os.MkdirAll("fixtures/vcr", 0755)
var fileOut bytes.Buffer
if c.Gzip {
err := gzipWrite(&fileOut, jsonOut.Bytes())
if err != nil {
panic("httpvcr: gzip write failed")
}
} else {
fileOut = jsonOut
}
err := ioutil.WriteFile(c.Filename(), fileOut.Bytes(), 0644)
if err != nil {
panic("httpvcr: cannot write cassette file!")
}
}
func (c *cassette) matchEpisode(request *VCRRequest) (*Episode, error) {
return c.episodeMatcher.MatchEpisode(request)
}
func panicEpisodeMismatch(request *VCRRequest, field string, expected string, actual string) {
panic(fmt.Sprintf(
"httpvcr: problem with episode for %s %s\n episode %s does not match:\n expected: %s\n but got: %s",
request.Method,
request.URL,
field,
expected,
actual,
))
}
// Write gzipped data to a Writer
func gzipWrite(w io.Writer, data []byte) error {
// Write gzipped data to the client
gw, err := gzip.NewWriterLevel(w, gzip.BestSpeed)
defer gw.Close()
gw.Write(data)
return err
}
// Write gunzipped data to a Writer
func gunzipWrite(w io.Writer, data []byte) error {
// Write gzipped data to the client
gr, err := gzip.NewReader(bytes.NewBuffer(data))
defer gr.Close()
data, err = ioutil.ReadAll(gr)
if err != nil {
return err
}
w.Write(data)
return nil
}