-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
390 lines (368 loc) · 10.5 KB
/
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
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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"github.com/xeipuuv/gojsonschema"
)
var headerJar = make(map[string]string)
type Tests []*Test
type responseHeaderTestCase struct {
Key string `json:"key"`
Value string `json:"value"`
Validate bool `json:"validate"`
PutInJar bool `json:"putInJar"`
}
type Test struct {
Label string `json:"label"`
request *http.Request // contains the actual request
Request *struct {
Method string `json:"method"`
URL *url.URL `json:"url"`
URLUserInfo *struct {
User string `json:"user"`
Password string `json:"password"`
} `json:"urlUserInfo"`
TLSInsecureSkipVerify bool `json:"tlsInsecureSkipverify"`
NoDefaultHeaders bool `json:"noDefaultHeaders"`
Headers []*struct {
Key string `json:"key"`
Value string `json:"value"`
UseFromJar bool `json:"useFromJar"`
} `json:"headers"`
BodyString string `json:"bodyString"`
BodyJson interface{} `json:"bodyJson"`
} `json:"request"`
response *http.Response // contains the actual response
Response *struct {
Status string `json:"status,omitempty`
StatusCode int `json:"statusCode"`
NoDefaultHeaders bool `json:"noDefaultHeaders"`
Headers []*responseHeaderTestCase `json:"headers"`
contentType string
BodyCheck bool `json:"bodyCheck"`
BodyString string `json:"bodyString"`
BodyJsonSchema map[string]interface{} `json:"bodyJsonSchema"`
body []byte
} `json:"response"`
UseCookieJar bool `json:"useCookieJar"`
NoCookieJar bool `json:"NoCookieJar"`
cookieJar *cookiejar.Jar
PrintDebugOnFail bool `json:"printDebugOnFail`
PrintJsonIndented bool `json:"printJsonIndented"`
failed bool
}
func (t *Test) Run() bool {
defer t.printDebugOnfail()
if t.failed {
return false
}
var err error
t.response, err = Call(t.request, t.Request.TLSInsecureSkipVerify)
if err != nil {
t.fail(err)
return false
}
t.evaluate()
return !t.failed
}
func (t *Test) Prepare(defaultTest *Test) {
if t.Request == nil {
t.fail(errors.New("cannot execute test because 'request' is missing"))
return
}
if defaultTest != nil {
if defaultTest.PrintDebugOnFail {
t.PrintDebugOnFail = true
if defaultTest.PrintJsonIndented {
t.PrintJsonIndented = true
}
}
}
if t.Request.Method == "" && defaultTest != nil {
if defaultTest.Request.Method == "" {
t.fail(errors.New("request method missing"))
} else {
t.Request.Method = defaultTest.Request.Method
}
}
t.prepareURL(defaultTest)
var body io.Reader
var err error
if t.Request.BodyString != "" {
body = bytes.NewBufferString(t.Request.BodyString)
} else if t.Request.BodyJson != nil {
var b []byte
b, err = json.Marshal(t.Request.BodyJson)
if err != nil {
t.fail(err)
return
}
body = bytes.NewBuffer(b)
}
t.request, err = http.NewRequest(t.Request.Method, t.Request.URL.String(), body)
if err != nil {
t.fail(err)
}
t.prepareHeaders(defaultTest)
t.prepareCookies(defaultTest)
}
func (t *Test) prepareURL(defaultTest *Test) {
if t.Request.URL == nil {
return
}
if t.Request.URLUserInfo != nil && t.Request.URLUserInfo.User != "" {
t.Request.URL.User = url.UserPassword(t.Request.URLUserInfo.User, t.Request.URLUserInfo.Password)
}
if defaultTest == nil || defaultTest.Request == nil || defaultTest.Request.URL == nil {
return
}
if t.Request.URL.Scheme == "" {
t.Request.URL.Scheme = defaultTest.Request.URL.Scheme
}
if t.Request.URL.Opaque == "" {
t.Request.URL.Opaque = defaultTest.Request.URL.Opaque
}
if t.Request.URL.Host == "" {
if t.Request.URL.User == nil {
t.Request.URL.User = defaultTest.Request.URL.User
}
t.Request.URL.Host = defaultTest.Request.URL.Host
if defaultTest.Request.TLSInsecureSkipVerify {
t.Request.TLSInsecureSkipVerify = true
}
}
if t.Request.URL.Path == "" {
t.Request.URL.Path = defaultTest.Request.URL.Path
}
if t.Request.URL.RawQuery == "" {
t.Request.URL.RawQuery = defaultTest.Request.URL.RawQuery
}
if t.Request.URL.Fragment == "" {
t.Request.URL.Fragment = defaultTest.Request.URL.Fragment
}
url := t.Request.URL.String()
if url == "" {
t.fail(errors.New("request url missing"))
}
}
func (t *Test) prepareHeaders(defaultTest *Test) {
// set request headers
if t.Request.NoDefaultHeaders == false && defaultTest != nil && defaultTest.Request != nil && defaultTest.Request.Headers != nil {
//TODO test if the default headers get overwritten by the ones in described in the test
t.Request.Headers = append(defaultTest.Request.Headers, t.Request.Headers...)
}
if len(t.Request.Headers) > 0 {
for _, h := range t.Request.Headers {
if h.UseFromJar {
if v, ok := headerJar[h.Key]; ok {
h.Value = v
}
}
t.request.Header.Add(h.Key, h.Value)
}
}
// set response test case headers
if t.Response != nil && t.Response.NoDefaultHeaders == false && defaultTest != nil && defaultTest.Response != nil && defaultTest.Response.Headers != nil {
testCasesToAdd := make([]*responseHeaderTestCase, 0)
for _, defaultTestCase := range defaultTest.Response.Headers {
found := false
for _, testCase := range t.Response.Headers {
if defaultTestCase.Key == testCase.Key {
found = true
}
}
if !found {
testCasesToAdd = append(testCasesToAdd, defaultTestCase)
}
}
t.Response.Headers = append(t.Response.Headers, testCasesToAdd...)
}
}
func (t *Test) prepareCookies(defaultTest *Test) {
if defaultTest == nil {
// t is the default test create new cookie jar in it nothing more
var err error
if t.cookieJar, err = cookiejar.New(nil); err != nil {
t.fail(err)
}
return
}
// no cookie jar precede above use cookie jar
if t.NoCookieJar {
return
}
if defaultTest.UseCookieJar {
t.UseCookieJar = true
}
if !t.UseCookieJar {
return
}
t.cookieJar = defaultTest.cookieJar
for _, c := range t.cookieJar.Cookies(t.Request.URL) {
t.request.AddCookie(c)
}
}
func (t *Test) evaluate() {
t.readResponse()
if t.Response == nil {
return
}
t.evaluateHeaders()
t.evaluateStatusCode()
t.evaluateStatus()
t.evaluateBody()
}
func (t *Test) readResponse() {
if t.response == nil {
return
}
// content type
if t.response.Header != nil {
if v, ok := t.response.Header["Content-Type"]; ok {
t.Response.contentType = v[0]
}
}
// cookies
if cookies := t.response.Cookies(); cookies != nil && !t.NoCookieJar && t.UseCookieJar {
t.cookieJar.SetCookies(t.Request.URL, cookies)
}
// body
if t.Response.body != nil {
return
}
var err error
t.Response.body, err = ioutil.ReadAll(t.response.Body)
defer t.response.Body.Close()
if err != nil {
t.fail(fmt.Errorf("response body read error ", err))
}
}
func (t *Test) evaluateHeaders() {
if t.Response.Headers != nil {
for _, testCase := range t.Response.Headers {
value, ok := t.response.Header[testCase.Key]
if ok {
if testCase.PutInJar && value[0] == testCase.Value {
headerJar[testCase.Key] = value[0]
} else if testCase.Validate && value[0] != testCase.Value {
t.fail(fmt.Errorf("expected header %s to equal %s, given %s", testCase.Key, testCase.Value, value[0]))
}
} else if testCase.Validate {
t.fail(fmt.Errorf("expected header %s to be present", testCase.Key))
}
}
}
}
func (t *Test) evaluateStatusCode() {
if t.Response.StatusCode != 0 && t.Response.StatusCode != t.response.StatusCode {
t.fail(fmt.Errorf("expect status code to equal %d, given %d", t.Response.StatusCode, t.response.StatusCode))
}
}
func (t *Test) evaluateStatus() {
if t.Response.Status != "" && t.Response.Status != t.response.Status {
t.fail(fmt.Errorf("expect status to equal %q, given %q", t.Response.Status, t.response.Status))
}
}
func (t *Test) evaluateBody() {
if !t.Response.BodyCheck {
return
}
if t.Response.BodyString != "" {
b := []byte(t.Response.BodyString)
if bytes.Equal(t.Response.body, b) {
return
}
t.fail(fmt.Errorf("expect response body to equal %q, given %q", t.Response.BodyString, t.Response.body))
} else if t.Response.BodyJsonSchema != nil {
var v interface{}
if err := json.Unmarshal(t.Response.body, &v); err != nil {
t.fail(fmt.Errorf("response json body error %s", err))
return
}
result, err := gojsonschema.Validate(gojsonschema.NewGoLoader(t.Response.BodyJsonSchema), gojsonschema.NewGoLoader(v))
if err != nil {
t.fail(fmt.Errorf("validation error %s", err))
return
}
if !result.Valid() {
for _, desc := range result.Errors() {
t.fail(fmt.Errorf("JSON schema expect %s", desc))
}
}
}
}
func (t *Test) fail(err error) {
if t.failed == false {
t.failed = true
fmt.Println("\n\033[1;31mFAILED\033[0m", t.Label)
}
fmt.Printf(" \033[0;31m%s\033[0m\n", err)
}
func (t *Test) printDebugOnfail() {
if t.failed && t.PrintDebugOnFail {
fmt.Println("\033[1;36mDEBUG REQUEST\033[0m")
// request
fmt.Printf(" \033[1;33mURL\033[0m: %s\n", t.Request.URL.String())
fmt.Printf(" \033[1;33mHeaders\033[0m: %+v\n", t.request.Header)
fmt.Printf(" \033[1;33mBody\033[0m: ")
if t.Request.BodyString != "" {
fmt.Println(t.Request.BodyString)
} else if t.Request.BodyJson != nil {
b, err := json.Marshal(t.Request.BodyJson)
if err != nil {
t.fail(err)
return
}
if t.PrintJsonIndented {
var out bytes.Buffer
if err = json.Indent(&out, b, "\t", " "); err != nil {
fmt.Println(err)
} else {
fmt.Println(out.String())
}
} else {
fmt.Printf("%s\n", b)
}
} else {
fmt.Println("")
}
//response
if t.Response != nil {
fmt.Println("\033[1;36mDEBUG RESPONSE\033[0m")
if t.response != nil {
fmt.Printf(" \033[1;33mHeaders\033[0m: %+v\n", t.response.Header)
fmt.Printf(" \033[1;33mStatus code\033[0m: %+v\n", t.response.StatusCode)
fmt.Printf(" \033[1;33mStatus\033[0m: %+v\n", t.response.Status)
if t.Response.body != nil {
defaultBodyPrint := false
fmt.Printf(" \033[1;33mBody\033[0m: ")
if strings.Contains(strings.ToLower(t.Response.contentType), "application/json") {
if t.PrintJsonIndented {
var out bytes.Buffer
if err := json.Indent(&out, t.Response.body, "\t", " "); err != nil {
fmt.Println(err)
} else {
fmt.Println(out.String())
}
} else {
defaultBodyPrint = true
}
}
if defaultBodyPrint {
fmt.Printf("%s\n", t.Response.body)
}
}
} else {
fmt.Println(" \033[0;31mno response\033[0m")
}
}
}
}