// checkStatusCode check rule responses status code for problems func checkStatusCode(r *rules.Rule) []*Problem { var result []*Problem for respId, resp := range r.Responses { sectionId := "@CODE" if respId != rules.DEFAULT { sectionId += ":" + respId } if resp.Code != 0 && httputil.GetDescByCode(resp.Code) == "" { if respId == rules.DEFAULT { result = append(result, &Problem{ Type: PROBLEM_WARN, Info: "Unknown status code", Desc: fmtc.Sprintf("You define unknown status code %d in %s section. Please check list of valid status codes https://en.wikipedia.org/wiki/List_of_HTTP_status_codes", resp.Code, sectionId), }, ) } } } return result }
// parseRangeDate parse different formats of time strings // and return time struct func parseRangeDate(date string) (time.Time, error) { var ( dateStr string timeStr string layout string ) now := time.Now() var ( year = now.Year() month = now.Month() day = now.Day() ) if strings.Contains(date, " ") { dateSlice := strings.Split(date, " ") dateStr, timeStr = dateSlice[0], dateSlice[1] } else { if strings.Contains(date, "/") { dateStr = date } else { timeStr = date } } if dateStr != "" { switch strings.Count(dateStr, "/") { case 2: layout += "2006/01/02" case 1: layout += "2006/01" } } else { date = fmtc.Sprintf("%d/%02d/%d %s", year, month, day, date) layout = "2006/01/02" } if timeStr != "" { layout += " " switch strings.Count(timeStr, ":") { case 2: layout += "15:04:05" case 1: layout += "15:04" case 0: layout += "15" } } return time.Parse(layout, date) }
// checkMethod check rule request method name for problems func checkMethod(r *rules.Rule) []*Problem { var result []*Problem if !sliceutil.Contains([]string{"OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "CONNECT", "PATCH"}, r.Request.Method) { result = append(result, &Problem{ Type: PROBLEM_ERR, Info: "Unknown HTTP method", Desc: fmtc.Sprintf("You define unsupported HTTP method \"%s\". Valid methods is OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT and PATCH.", r.Request.Method), }, ) } return result }
// checkResponseDelay check rule responses delay for problems func checkResponseDelay(r *rules.Rule) []*Problem { var result []*Problem for respId, resp := range r.Responses { sectionId := "@DELAY" if respId != rules.DEFAULT { sectionId += ":" + respId } if resp.Delay > 60 { result = append(result, &Problem{ Type: PROBLEM_WARN, Info: "Response delay is too big", Desc: fmtc.Sprintf("Response delay is greater than maximum delay (60 seconds).", sectionId), }, ) } } return result }
// checkContent check rule content for problems func checkContent(r *rules.Rule) []*Problem { var result []*Problem for respId, resp := range r.Responses { sectionId := "@RESPONSE" if respId != rules.DEFAULT { sectionId += ":" + respId } if resp.File != "" && resp.Content != "" { result = append(result, &Problem{ Type: PROBLEM_ERR, Info: "Response body have two sources", Desc: fmtc.Sprintf("You define two different sources for response body (file and content in response section) in section %s. Please use only one source (file OR content in response section).", sectionId), }, ) } if resp.URL != "" && resp.Content != "" { result = append(result, &Problem{ Type: PROBLEM_ERR, Info: "Response body have two sources", Desc: fmtc.Sprintf("You define two different sources for response body (url and content in response section) in section %s. Please use only one source (url OR content in response section).", sectionId), }, ) } if resp.File == "" && resp.Content == "" && resp.URL != "" { if resp.Code == 200 || resp.Code == 0 { result = append(result, &Problem{ Type: PROBLEM_ERR, Info: "Response body is empty", Desc: fmtc.Sprintf("Section %s doesn't contains any response data.", sectionId), }, ) } } if resp.File != "" && !fsutil.IsExist(resp.File) { result = append(result, &Problem{ Type: PROBLEM_ERR, Info: "File with response is not exist", Desc: fmtc.Sprintf("File %s defined in %s section is not exist.", resp.File, sectionId), }, ) } else { if resp.File != "" && !fsutil.IsReadable(resp.File) { result = append(result, &Problem{ Type: PROBLEM_ERR, Info: "File with response is not readable", Desc: fmtc.Sprintf("File %s defined in %s section is not readable.", resp.File, sectionId), }, ) } } } return result }