-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecks_test.go
363 lines (278 loc) · 8.29 KB
/
checks_test.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"testing"
)
/*
* Data models to hold test cases for various checks
*/
type TestStatusCase struct {
option func(int)
param int
send int
match bool
errStr string
}
type TestPageSizeCase struct {
option func(string)
param string
send int64
match bool
errStr string
}
type TestHeadersCase struct {
option func(string)
param string
send http.Header
match bool
errStr string
}
type TestRegexpCase struct {
option func(string)
param string
send []byte
match bool
errStr string
}
type TestJsonCase struct {
option func(string)
param string
send []byte
match bool
errStr string
}
type TestJsonValueCase struct {
jsonBlob []byte
test JsonTest
match bool
errStr string
}
/*
* Tests for primary functions
*/
func Test_checkStatus(t *testing.T) {
cases := []TestStatusCase{
{opts.FlagStatus, 200, 200, true, ""},
{opts.FlagStatus, 404, 404, true, ""},
{opts.FlagStatus, 404, 200, false,
"HTTP Status Code was '200', expected '404'"},
}
for _, c := range cases {
// Call the option with the param specified in the case.
// eg. opts.FlagStatus(200) simulates --status=200
c.option(c.param)
// Test the flag that drives the test
expect(t, Tests["status"], true)
// Run status check
match, err := checkStatus(c.send)
expect(t, c.match, match)
// If we expect didn't expect a match test the error code
if !c.match {
expect(t, c.errStr, err.Error())
}
}
}
func Test_checkPageSize(t *testing.T) {
cases := []TestPageSizeCase{
{opts.FlagPageSize, "0:124", 100, true, ""},
{opts.FlagPageSize, "1024:65535", 1024, true, ""},
{opts.FlagPageSize, "5000:10000", 10000, true, ""},
{opts.FlagPageSize, "5000:10000", 100, false,
"HTTP Response Size was '100', out side of range '5000-10000'"},
{opts.FlagPageSize, "10:11", 12, false,
"HTTP Response Size was '12', out side of range '10-11'"},
}
for _, c := range cases {
// Call the option with the param specified in the case.
// eg. opts.FlagPageSize("0:124") simulates --page-size=0:124
c.option(c.param)
// Test the flag that drives the test
expect(t, Tests["page-size"], true)
// Run page size check
match, err := checkPageSize(c.send)
expect(t, c.match, match)
// If we expect didn't expect a match test the error code
if !c.match {
expect(t, c.errStr, err.Error())
}
}
}
func Test_testHeaders(t *testing.T) {
cases := []TestHeadersCase{
{opts.FlagHeaders, "X-Pot-Type:Teapot",
http.Header{"X-Pot-Type": {"Teapot"}}, true, ""},
// Multiple headers share the same key.
{opts.FlagHeaders, "X-Pot-Type:Teapot",
http.Header{"X-Pot-Type": {"Teapot", "Flowerpot"}}, true, ""},
// Multiple headers share the same key.
{opts.FlagHeaders, "X-Pot-Type:Teapot",
http.Header{"X-Pot-Type": {"Potpan", "Teapot"}}, true, ""},
// Required headers not correct value
{opts.FlagHeaders, "X-Pot-Type:Teapot",
http.Header{"X-Pot-Type": {"Polpot"}}, false,
"Header 'X-Pot-Type' does not equal 'Teapot'"},
// Required headers not present.
{opts.FlagHeaders, "X-Pot-Type:Teapot",
http.Header{"X-No-Pots": {"Here"}}, false,
"Header 'X-Pot-Type' not in HTTP response"},
// No headers returned at all.
{opts.FlagHeaders, "X-Pot-Type:Teapot",
nil, false,
"Header 'X-Pot-Type' not in HTTP response"},
}
for _, c := range cases {
// Call the option with the param specified in the case.
// eg. opts.FlagHeaders("key:val") simulates --headers=key:val
c.option(c.param)
// Test the flag that drives the test
expect(t, Tests["headers"], true)
// Run header check
match, err := checkHeaders(c.send)
expect(t, c.match, match)
// If we expect didn't expect a match the error code
if !c.match {
expect(t, c.errStr, err.Error())
}
}
}
func Test_checkRegexp(t *testing.T) {
cases := []TestRegexpCase{
{opts.FlagRegexp, "^The.*fox.+dog$",
[]byte("The quick brown fox ... lazy dog"), true, ""},
{opts.FlagRegexp, "dwarves",
[]byte("Here be dragons"), false,
"Regexp 'dwarves' not in HTTP response"},
}
for _, c := range cases {
// Call the option with the param specified in the case.
// eg. opts.FlagRegexp(".+") simulates --regexp=.+
c.option(c.param)
// Test the flag that drives the test
expect(t, Tests["regexp"], true)
// Run regex test
match, err := checkRegexp(c.send)
expect(t, c.match, match)
// If we expect didn't expect a match the error code
if !c.match {
expect(t, c.errStr, err.Error())
}
}
}
func Test_checkJson(t *testing.T) {
cases := []TestJsonCase{
{opts.FlagKeyExists, "foo",
[]byte(`{"baz":"qux", "foo":"bar"}`), true, ""},
{opts.FlagKeyEquals, "foo:bar",
[]byte(`{"foo":"bar", "baz":"qux"}`), true, ""},
{opts.FlagKeyLte, "foo:100",
[]byte(`{"foo":50, "baz":"qux"}`), true, ""},
{opts.FlagKeyLte, "foo:10.00001",
[]byte(`{"baz":"qux", "foo":10}`), true, ""},
{opts.FlagKeyGte, "foo:100",
[]byte(`{"foo":150, "baz":"qux"}`), true, ""},
{opts.FlagKeyGte, "foo:9.99999",
[]byte(`{"baz":"qux", "foo":10}`), true, ""},
{opts.FlagKeyExists, "foo",
[]byte(`{"baz":"qux", "wibble":"wobble"}`), false,
""},
{opts.FlagKeyExists, "foo",
[]byte(`{"baz":"qux", "wibble":"wobble"}`), false,
""},
{opts.FlagKeyEquals, "foo:bar",
[]byte(`{"baz":"qux", "foo":"fub"}`), true,
"Key 'foo' does not equal 'bar'"},
{opts.FlagKeyLte, "foo:10",
[]byte(`{"foo":"bar", "baz":"qux"}`), true,
"Key 'foo' value is not an integer"},
{opts.FlagKeyLte, "foo:1",
[]byte(`{"baz":"qux", "foo":1000000000000000}`), true,
"Key 'foo' is greater than '1'"},
{opts.FlagKeyGte, "foo:10",
[]byte(`{"baz":"qux", "foo":"10"}`), true,
"Key 'foo' value is not an integer"},
{opts.FlagKeyGte, "foo:1000",
[]byte(`{"baz":"qux", "foo":1}`), true,
"Key 'foo' is less than '1000'"},
}
for _, c := range cases {
// Clear old settings
Tests["keys"] = false
JsonTests = JsonTests[:0]
// Call the option with the param specified in the case.
// eg. opts.FlagKeyLte("foo:100") simulatutes --lte=foo:100
c.option(c.param)
// Test the flag that drives the test
expect(t, Tests["keys"], true)
// Unmarshall JSON in test case
var jsonMap map[string]interface{}
err := json.Unmarshal(c.send, &jsonMap)
check(err)
// Run Json test
match, err := checkJson(jsonMap, JsonTests[0])
expect(t, c.match, match)
// If we didn't expect a match, test the error code
if c.errStr != "" {
expect(t, c.errStr, err.Error())
}
}
}
func Test_checkJsonValue(t *testing.T) {
var cases = []TestJsonValueCase{
{[]byte(`{"Foo":1,"Baz":"Qux"}`),
JsonTest{"Baz", "Qux", "equals"},
true, ""},
{[]byte(`{"Animal":{"Name":"Platypus", "Order":"Monotremata"}}`),
JsonTest{"Name", "Platypus", "equals"},
true, ""},
{[]byte(`{"Animal":{"Mammal":{"Name":"Platypus"}}}`),
JsonTest{"Name", "Platypus", "equals"},
true, ""},
{[]byte(`[{"Foo":1,"Baz":"Qux"}, {"success":true}]`),
JsonTest{"success", "true", "equals"},
true, ""},
{[]byte(`[{"Foo":1,"Baz":"Qux"}, {"success":true}, {"success":false}]`),
JsonTest{"success", "true", "equals"},
true, ""},
{[]byte(`[{"Foo":100,"Baz":"Qux"}]`),
JsonTest{"Foo", 150.00, "lte"},
true, ""},
{[]byte(`[{"Foo":100,"Baz":"Qux"}]`),
JsonTest{"Foo", 50.00, "lte"},
true, "Key 'Foo' is greater than '50'"},
{[]byte(`[{"Foo":100,"Baz":"Qux"}]`),
JsonTest{"Foo", 50.00, "gte"},
true, ""},
{[]byte(`[{"Foo":100,"Baz":"Qux"}]`),
JsonTest{"Foo", 150.00, "gte"},
true, "Key 'Foo' is less than '150'"},
{[]byte(`{"Wibble":"Wobble","Baz":"Qux"}`),
JsonTest{"Foo", "Baz", "equals"},
false, ""},
{[]byte(`{"Foo":"Ber","Baz":"Qux"}`),
JsonTest{"Foo", "Bar", "equals"},
true, "Key 'Foo' does not equal 'Bar'"},
// Null array test. Should return "Key not found"
{[]byte(`[]`),
JsonTest{"success", "true", "equals"},
false, ""},
// Null object test. Should return "Key not found"
{[]byte(`{}`),
JsonTest{"success", "true", "equals"},
false, ""},
}
for _, c := range cases {
var result interface{}
err := json.Unmarshal(c.jsonBlob, &result)
if err != nil {
fmt.Println("error unzipping json:", err)
}
match, err := checkJson(result, c.test)
expect(t, c.match, match)
// If we expect didn't expect a match test the error code
if c.errStr != "" {
expect(t, c.errStr, err.Error())
}
}
}