forked from rawoke083/RestTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtest.go
189 lines (132 loc) · 4.03 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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/rawoke083/ColorPrint"
"log"
"os"
"errors"
)
type TestCase struct {
HttpMethod string
Url string
HttpReturnCode string
ResponseTXTCheck string
Pass bool
}
func (test *TestCase) runATest(mparams map[string]string) bool {
for i, k := range mparams {
test.Url = strings.Replace(test.Url, i, k, -1)
}
ColorPrint.ColWrite("\n\nTEST:"+ColorPrint.ToColor(test.HttpMethod, ColorPrint.CL_LIGHT_CYAN)+" "+test.Url, ColorPrint.CL_WHITE)
err := errors.New("")
resp := new(http.Response)
if test.HttpMethod == "POST" {
u, _ := url.Parse(test.Url)
ColorPrint.ColWrite(" ::: ARGS:"+u.RawQuery, ColorPrint.CL_WHITE)
q, _ := url.ParseQuery(u.RawQuery)
resp, err = http.PostForm(test.Url, q)
} else {
resp, err = http.Get(test.Url)
}
if err != nil {
// handle error
ColorPrint.ColWrite(fmt.Sprintf("\n\n=====>HTTP-ERROR:%s", test.Url), ColorPrint.CL_RED)
return false
}
defer resp.Body.Close()
test.Pass = true
http_code := strconv.Itoa(resp.StatusCode)
body, _ := ioutil.ReadAll(resp.Body)
s := string(body)
if strings.TrimSpace(test.HttpReturnCode) != http_code {
test.Pass = false
//ColorPrint.ColWrite("\n=>FAILED - HttpCode Excepted |"+test.HttpReturnCode+"| but got |"+http_code+"|"+s, ColorPrint.CL_RED)
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)
}
func LoadTest(filename string) []TestCase {
// valid_http_methods := []string{"GET", "POST", "PUT", "DELETE"}
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]
}
testCount++
} //for scanner
return testCaseList[0:testCount]
}
func printUsage() {
ColorPrint.ColWrite("Usage:\n", ColorPrint.CL_WHITE)
ColorPrint.ColWrite("\nrtest filename=<url-list-file> [-DStringToReplace=NewString] [-DMoreStringToReplace=MoreNewString]\n\n", ColorPrint.CL_WHITE)
}
func main() {
ColorPrint.ColWrite("\n###### REST Test ########\n", ColorPrint.CL_YELLOW)
var mparams = make(map[string]string)
var fileName string
for i := range os.Args {
if strings.Contains(os.Args[i], "-D") {
fields := strings.Split(strings.Replace(os.Args[i], "-D", "", -1), "=")
mparams[fields[0]] = fields[1]
}
if strings.Contains(os.Args[i], "filename=") {
fields := strings.Split(os.Args[i], "=")
fileName = fields[1]
}
}
if len(fileName) < 1 {
fmt.Println("\nError:No filename\n")
printUsage()
os.Exit(1)
}
os.Exit(runTestSuite(LoadTest(fileName), mparams))
}