-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrtest.go
372 lines (300 loc) · 8.29 KB
/
rtest.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
package main
import (
"bufio"
"fmt"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"flag"
"strconv"
"strings"
"bytes"
"github.com/rawoke083/ColorPrint"
"log"
"os"
"errors"
)
type arrayFlags []string
type varMap map[string]string
var Aliases arrayFlags
var Headers arrayFlags
var fileName string
type haveVar struct {
Key string
Val string
}
var haveVariables []haveVar
// TestCase holds the HTTP method, URL to call,
// return code, what text to check on return, and pass/fail
type TestCase struct {
HTTPMethod string
URL string
HTTPReturnCode string
ResponseTXTCheck string
Storevar string
Headers string
Pass bool
}
func getKeyType(keyname string, kn string, data interface{}) (bool, interface{}) {
switch vv := data.(type) {
case int, float64, string,bool,nil:
if kn == keyname {
return true, data
}
case json.Number:
if kn == keyname {
val, err := data.(json.Number).Int64()
if err != nil {
return true, nil
}
return true, val
}
case []interface{}:
for _, u := range vv {
return getKeyType(keyname, kn, u)
}
case map[string]interface{}:
if kn == keyname {
return true, data
}
for knn, ov := range vv {
found_sub,sub_data:= getKeyType(keyname, knn, ov)
if(found_sub) {
return true,sub_data
}
}
return false,nil
default:
return false, 0
}
return false, 0
}
func getKey(keyname string, data map[string]interface{}) (bool, interface{}) {
for k, v := range data {
ok, x := getKeyType(keyname, k, v)
if ok {
return true, x
}
}
return false, nil
}
func setKey(keyname, value string) {
found := false
for _, variable := range haveVariables {
if variable.Key == keyname {
found = true
}
}
if (!found) {
var variable haveVar
variable.Key = keyname
variable.Val = value
haveVariables = append(haveVariables, variable)
}
}
func setMultipleHeaders(req http.Request) {
for _, val := range Headers {
fields := strings.Split(val, ":")
req.Header.Set(fields[0], fields[1])
}
}
func setHeader(req http.Request, header string) {
for _, variable := range haveVariables {
if strings.Contains(header, "%"+variable.Key+"%") {
header = strings.Replace(header, "%"+variable.Key+"%", variable.Val, -1)
}
}
fields := strings.Split(header, ":")
req.Header.Set(fields[0], fields[1])
}
func (test *TestCase) runATest(mparams map[string]string) bool {
for i, k := range mparams {
test.URL = strings.Replace(test.URL, i, k, -1)
}
for _, variable := range haveVariables {
if strings.Contains(test.URL, "%"+variable.Key+"%") {
test.URL = strings.Replace(test.URL, "%"+variable.Key+"%", variable.Val, -1)
}
}
err := errors.New("")
resp := new(http.Response)
req := new(http.Request)
// Parse URL and Query
u, _ := url.Parse(test.URL)
c := &http.Client{}
switch test.HTTPMethod {
case "GET":
req, err = http.NewRequest("GET", test.URL, nil)
ColorPrint.ColWrite("\n\nTEST:"+ColorPrint.ToColor(test.HTTPMethod, ColorPrint.CL_LIGHT_CYAN)+" "+test.URL, ColorPrint.CL_WHITE)
if err != nil {
return false
}
case "POST":
urlNoQuery := fmt.Sprintf("%s://%s%s", u.Scheme, u.Host, u.Path)
req, err = http.NewRequest("POST", urlNoQuery, strings.NewReader(u.RawQuery))
ColorPrint.ColWrite("\n\nTEST:"+ColorPrint.ToColor(test.HTTPMethod, ColorPrint.CL_LIGHT_CYAN)+" "+urlNoQuery, ColorPrint.CL_WHITE)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
if err != nil {
return false
}
case "PATCH":
urlNoQuery := fmt.Sprintf("%s://%s%s", u.Scheme, u.Host, u.Path)
req, err = http.NewRequest("PATCH", urlNoQuery, strings.NewReader(u.RawQuery))
ColorPrint.ColWrite("\n\nTEST:"+ColorPrint.ToColor(test.HTTPMethod, ColorPrint.CL_LIGHT_CYAN)+" "+urlNoQuery, ColorPrint.CL_WHITE)
if err != nil {
return false
}
case "DELETE":
req, err = http.NewRequest("DELETE", test.URL, strings.NewReader(u.RawQuery))
ColorPrint.ColWrite("\n\nTEST:"+ColorPrint.ToColor(test.HTTPMethod, ColorPrint.CL_LIGHT_CYAN)+" "+test.URL, ColorPrint.CL_WHITE)
if err != nil {
return false
}
}
setMultipleHeaders(*req)
if len(test.Headers) > 1 {
h := strings.Split(test.Headers, ",")
for _, v := range h {
setHeader(*req, v)
}
}
if resp, err = c.Do(req); err != nil {
ColorPrint.ColWrite(fmt.Sprintf("\n\n=====>HTTP-ERROR:%s", test.URL), ColorPrint.CL_RED)
return false
}
test.Pass = true
http_code := strconv.Itoa(resp.StatusCode)
body, _ := ioutil.ReadAll(resp.Body)
s := string(body)
defer resp.Body.Close()
ctype := resp.Header.Get("Content-Type")
switch ctype {
case "application/javascript":
data := json.NewDecoder(bytes.NewReader(body))
data.UseNumber()
var d map[string]interface{}
err := data.Decode(&d)
if err != nil {
test.Pass = false
ColorPrint.ColWrite("\n=>FAILED - Expected response application/javascript cannot be decoded", ColorPrint.CL_RED)
break
}
if len(test.Storevar) > 1 {
checkVar := strings.Split(test.Storevar, "=")
ok, result := getKey(checkVar[0], d)
if ok {
var vvalue string
switch vv := result.(type) {
case float64:
vvalue = fmt.Sprintf("%f", vv)
case string:
vvalue = vv
case int64:
vvalue = strconv.FormatInt(vv, 10)
}
if len(checkVar) > 1 {
setKey(checkVar[1], vvalue)
} else {
setKey(checkVar[0], vvalue)
}
}
}
}
if strings.TrimSpace(test.HTTPReturnCode) != http_code {
test.Pass = false
ColorPrint.ColWrite("\n=>FAILED - HttpCode Excepted |"+test.HTTPReturnCode+"| but got |"+http_code+"|", ColorPrint.CL_RED)
return false
}
if len(test.ResponseTXTCheck) > 1 {
if !strings.Contains(s, test.ResponseTXTCheck) {
test.Pass = false
ColorPrint.ColWrite("\n=>FAILED - ResponseText ("+test.ResponseTXTCheck+") not found. "+s, ColorPrint.CL_RED)
return false
}
}
return true
}
func runTestSuite(testCases []TestCase, mparams map[string]string) int {
var testRunCount int
var testOKCount int
for _, test := range testCases {
testRunCount++
if test.runATest(mparams) {
testOKCount++
}
} //end for
s_total_test := fmt.Sprintf("\n\nTotal Test %d", testRunCount)
s_total_test_ok := fmt.Sprintf("\nTest(s) OK %d", testOKCount)
s_total_test_failed := fmt.Sprintf("\nTest(s) Failed %d\n", int(testRunCount-testOKCount))
ColorPrint.ColWrite(s_total_test, ColorPrint.CL_LIGHT_BLUE)
ColorPrint.ColWrite(s_total_test_ok, ColorPrint.CL_LIGHT_GREEN)
ColorPrint.ColWrite(s_total_test_failed, ColorPrint.CL_RED)
return (testRunCount - testOKCount)
}
// LoadTest loads test data from test specification files
func LoadTest(filename string) []TestCase {
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
//max of 64 test for now...
testCaseList := [64]TestCase{}
testCount := 0
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "#") {
continue
}
fields := strings.Split(scanner.Text(), "|")
field_count := len(fields)
if field_count < 2 {
continue
}
testCaseList[testCount].HTTPMethod = fields[0]
testCaseList[testCount].URL = fields[1]
testCaseList[testCount].HTTPReturnCode = fields[2]
if field_count > 3 {
testCaseList[testCount].ResponseTXTCheck = fields[3]
}
if field_count > 4 {
testCaseList[testCount].Storevar = fields[4]
}
if field_count > 5 {
testCaseList[testCount].Headers = fields[5]
}
testCount++
} //for scanner
return testCaseList[0:testCount]
}
func printUsage() {
ColorPrint.ColWrite("Usage:\n", ColorPrint.CL_WHITE)
ColorPrint.ColWrite("\nrtest filename=<url-list-file> [-D search=replace..]\n\n", ColorPrint.CL_WHITE)
}
func (i *arrayFlags) String() string {
return "my string representation"
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
func init() {
flag.Var(&Aliases, "D", "Replace text with some other text")
flag.Var(&Headers, "H", "Request headers")
flag.StringVar(&fileName, "filename", "", "File containing list of test rules")
flag.Parse()
}
func main() {
ColorPrint.ColWrite("\n###### REST Test ########\n", ColorPrint.CL_YELLOW)
var mparams = make(map[string]string)
for i := range Aliases {
fields := strings.Split(Aliases[i], "=")
mparams[fields[0]] = fields[1]
}
if len(fileName) < 1 {
fmt.Println("\nError:No filename")
printUsage()
os.Exit(1)
}
os.Exit(runTestSuite(LoadTest(fileName), mparams))
}