コード例 #1
0
ファイル: rtest.go プロジェクト: rawoke083/RestTest
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)
}
コード例 #2
0
ファイル: rtest.go プロジェクト: rawoke083/RestTest
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))
}
コード例 #3
0
ファイル: rtest.go プロジェクト: rawoke083/RestTest
func printUsage() {
	ColorPrint.ColWrite("Usage:\n", ColorPrint.CL_WHITE)
	ColorPrint.ColWrite("\nrtest filename=<url-list-file> [-D search=replace..]\n\n", ColorPrint.CL_WHITE)
}
コード例 #4
0
ファイル: rtest.go プロジェクト: rawoke083/RestTest
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
}