-
Notifications
You must be signed in to change notification settings - Fork 1
/
suite.go
254 lines (230 loc) · 6.17 KB
/
suite.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
package httpbaselinetest
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/jmoiron/sqlx"
"github.com/pmezard/go-difflib/difflib"
)
type Suite struct {
t *testing.T
baselineDir string
}
func NewDefaultSuite(t *testing.T) *Suite {
return &Suite{
t: t,
baselineDir: "testdata",
}
}
type SetupFunc func(testName string, baselineTest *HTTPBaselineTest) error
type TeardownFunc func(t *testing.T, baselineTest *HTTPBaselineTest) error
type SeedFunc func(baselineTest *HTTPBaselineTest) error
type BodyValidatorFunc func(body []byte) error
type HTTPBaselineTest struct {
Setup SetupFunc
Teardown TeardownFunc
Custom interface{}
Handler http.Handler
Method string
Path string
Host string
Body interface{} // io.Reader or string
Headers map[string]string
Cookies []http.Cookie
RequestValidator BodyValidatorFunc
ResponseValidator BodyValidatorFunc
Db *sqlx.DB
Seed string
SeedFunc SeedFunc
Tables []string
}
type httpBaselineTestRunner struct {
testName string
suite *Suite
btest *HTTPBaselineTest
t *testing.T
baselineReqPath string
baselineRespPath string
baselineDbPath string
seedPath string
dbTableInfo *dbTableInfo
}
func newRunner(testName string, t *testing.T, suite *Suite,
btest *HTTPBaselineTest) httpBaselineTestRunner {
if btest.Setup != nil {
err := btest.Setup(testName, btest)
if err != nil {
t.Fatalf("Setup failed: %s", err)
}
}
if btest.Handler == nil {
t.Fatal("Handler is nil")
}
if btest.Method == "" {
t.Fatal("Method is not provided")
}
if btest.Path == "" {
t.Fatal("Path is not provided")
}
nPathPrefix := path.Join(suite.baselineDir, NormalizeTestName(testName))
var seedPath string
if btest.Seed != "" {
seedPath = path.Join(suite.baselineDir, btest.Seed)
}
return httpBaselineTestRunner{
testName: testName,
suite: suite,
btest: btest,
t: t,
baselineReqPath: nPathPrefix + ".req.txt",
baselineRespPath: nPathPrefix + ".resp.txt",
baselineDbPath: nPathPrefix + ".db.json",
seedPath: seedPath,
dbTableInfo: &dbTableInfo{},
}
}
var disallowedTestChars = regexp.MustCompile(`[^[:word:]]`)
func NormalizeTestName(name string) string {
tname := strings.ToLower(name)
tbytes := disallowedTestChars.ReplaceAll([]byte(tname), []byte("_"))
return string(tbytes)
}
func formatJSON(body []byte) (string, error) {
var v interface{}
err := json.Unmarshal(body, &v)
if err != nil {
return "", err
}
// use encoder to add trailing newline
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetIndent("", " ")
err = enc.Encode(v)
if err != nil {
return "", err
}
return buf.String(), nil
}
func formatBody(contentType string, body []byte) (string, error) {
if contentType == "application/json" {
return formatJSON(body)
}
return string(body), nil
}
func doRebaseline() bool {
return os.Getenv("REBASELINE") != ""
}
func (r *httpBaselineTestRunner) assertBaselineEquality(expectedPath string, formatted string) {
expected, err := ioutil.ReadFile(expectedPath)
if err != nil {
r.t.Fatalf("Error reading baseline %s: %s", expectedPath, err)
}
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(string(expected)),
B: difflib.SplitLines(formatted),
FromFile: expectedPath + " (expected)",
ToFile: "actual",
Eol: "\n",
}
diffstr, err := difflib.GetUnifiedDiffString(diff)
if err != nil {
r.t.Fatalf("Error generating diff: %s", err)
}
if diffstr != "" {
r.t.Error("Request Difference")
r.t.Log("\n" + diffstr)
}
}
func (r *httpBaselineTestRunner) writeFile(path string, formattedData []byte) {
dir := filepath.Dir(path)
err := os.MkdirAll(dir, 0755)
if err != nil {
r.t.Fatalf("Error creating dir %s: %s", dir, err)
}
// #nosec G306 -- The test files should be world readable
err = ioutil.WriteFile(path, []byte(formattedData), 0644)
if err != nil {
r.t.Fatalf("Error writing baseline %s: %s", path, err)
}
}
func (suite *Suite) Run(name string, btest HTTPBaselineTest) {
suite.t.Run(name, func(t *testing.T) {
// Should probably have some way of indicating if the
// test should be t.Parallel()
runner := newRunner(name, t, suite, &btest)
if btest.Db != nil {
runner.dbTestSetup()
// make sure we close the db connection after
// the test
defer btest.Db.Close()
}
req := runner.buildRequest()
formattedReq, rawReqBody, err := formatRequest(req)
if err != nil {
t.Fatalf("Error formatting request: %s", err)
}
if btest.RequestValidator != nil {
err = btest.RequestValidator(rawReqBody)
if err != nil {
t.Errorf("Error validating request: %s", err)
}
}
if doRebaseline() {
runner.writeFile(runner.baselineReqPath,
[]byte(formattedReq))
} else {
runner.assertBaselineEquality(runner.baselineReqPath,
formattedReq)
}
recorder := httptest.NewRecorder()
btest.Handler.ServeHTTP(recorder, req)
formattedResp, rawRespBody, err := formatResponse(recorder.Result())
if err != nil {
t.Fatalf("Error formatting response: %s", err)
}
if btest.ResponseValidator != nil {
err := btest.ResponseValidator(rawRespBody)
if err != nil {
t.Errorf("Error validating response: %s", err)
}
}
if doRebaseline() {
runner.writeFile(runner.baselineRespPath,
[]byte(formattedResp))
} else {
runner.assertBaselineEquality(runner.baselineRespPath,
formattedResp)
}
if btest.Db != nil {
fullDbBaseline := runner.generateDbBaseline()
if btest.Tables != nil {
formattedDb, err := formatDb(fullDbBaseline)
if err != nil {
t.Fatalf("Cannot format db baseline: %s", err)
}
if doRebaseline() {
runner.writeFile(runner.baselineDbPath, formattedDb)
} else {
runner.assertBaselineEquality(runner.baselineDbPath,
string(formattedDb))
}
} else {
runner.assertNoDbChanges(fullDbBaseline)
}
}
if btest.Teardown != nil {
err := btest.Teardown(t, &btest)
if err != nil {
t.Fatalf("Teardown failed: %s", err)
}
}
})
}