2020-04-04 00:16:27 +05:30
|
|
|
package matchers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"regexp"
|
2020-04-26 23:32:58 +02:00
|
|
|
|
|
|
|
|
"github.com/Knetic/govaluate"
|
2020-04-04 00:16:27 +05:30
|
|
|
)
|
|
|
|
|
|
2020-12-21 15:51:43 +05:30
|
|
|
// Matcher is used to match a part in the output from a protocol.
|
2020-04-04 00:16:27 +05:30
|
|
|
type Matcher struct {
|
|
|
|
|
// Type is the type of the matcher
|
|
|
|
|
Type string `yaml:"type"`
|
2020-12-21 15:51:43 +05:30
|
|
|
// Condition is the optional condition between two matcher variables
|
|
|
|
|
//
|
|
|
|
|
// By default, the condition is assumed to be OR.
|
|
|
|
|
Condition string `yaml:"condition,omitempty"`
|
|
|
|
|
|
|
|
|
|
// Part is the part of the data to match
|
|
|
|
|
Part string `yaml:"part,omitempty"`
|
|
|
|
|
|
|
|
|
|
// Negative specifies if the match should be reversed
|
|
|
|
|
// It will only match if the condition is not true.
|
|
|
|
|
Negative bool `yaml:"negative,omitempty"`
|
2020-04-04 00:16:27 +05:30
|
|
|
|
2020-07-10 09:04:38 +02:00
|
|
|
// Name is matcher Name
|
2020-04-24 06:54:46 +05:30
|
|
|
Name string `yaml:"name,omitempty"`
|
2020-04-04 00:16:27 +05:30
|
|
|
// Status are the acceptable status codes for the response
|
|
|
|
|
Status []int `yaml:"status,omitempty"`
|
|
|
|
|
// Size is the acceptable size for the response
|
|
|
|
|
Size []int `yaml:"size,omitempty"`
|
|
|
|
|
// Words are the words required to be present in the response
|
|
|
|
|
Words []string `yaml:"words,omitempty"`
|
|
|
|
|
// Regex are the regex pattern required to be present in the response
|
|
|
|
|
Regex []string `yaml:"regex,omitempty"`
|
2020-04-24 06:54:46 +05:30
|
|
|
// Binary are the binary characters required to be present in the response
|
|
|
|
|
Binary []string `yaml:"binary,omitempty"`
|
2020-04-26 23:32:58 +02:00
|
|
|
// DSL are the dsl queries
|
|
|
|
|
DSL []string `yaml:"dsl,omitempty"`
|
2021-02-24 11:23:22 +05:30
|
|
|
// Encoding specifies the encoding for the word content if any.
|
|
|
|
|
Encoding string `yaml:"encoding,omitempty"`
|
2020-04-04 00:16:27 +05:30
|
|
|
|
2020-12-21 15:51:43 +05:30
|
|
|
// cached data for the compiled matcher
|
|
|
|
|
condition ConditionType
|
|
|
|
|
matcherType MatcherType
|
|
|
|
|
regexCompiled []*regexp.Regexp
|
|
|
|
|
dslCompiled []*govaluate.EvaluableExpression
|
2020-04-04 00:16:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MatcherType is the type of the matcher specified
|
|
|
|
|
type MatcherType = int
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// WordsMatcher matches responses with words
|
|
|
|
|
WordsMatcher MatcherType = iota + 1
|
|
|
|
|
// RegexMatcher matches responses with regexes
|
|
|
|
|
RegexMatcher
|
2020-04-21 20:50:35 +02:00
|
|
|
// BinaryMatcher matches responses with words
|
2020-04-24 06:54:46 +05:30
|
|
|
BinaryMatcher
|
2020-04-04 00:16:27 +05:30
|
|
|
// StatusMatcher matches responses with status codes
|
|
|
|
|
StatusMatcher
|
|
|
|
|
// SizeMatcher matches responses with response size
|
|
|
|
|
SizeMatcher
|
2020-04-26 23:32:58 +02:00
|
|
|
// DSLMatcher matches based upon dsl syntax
|
|
|
|
|
DSLMatcher
|
2020-04-04 00:16:27 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// MatcherTypes is an table for conversion of matcher type from string.
|
|
|
|
|
var MatcherTypes = map[string]MatcherType{
|
|
|
|
|
"status": StatusMatcher,
|
|
|
|
|
"size": SizeMatcher,
|
|
|
|
|
"word": WordsMatcher,
|
|
|
|
|
"regex": RegexMatcher,
|
2020-04-21 20:50:35 +02:00
|
|
|
"binary": BinaryMatcher,
|
2020-04-26 23:32:58 +02:00
|
|
|
"dsl": DSLMatcher,
|
2020-04-04 00:16:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ConditionType is the type of condition for matcher
|
|
|
|
|
type ConditionType int
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// ANDCondition matches responses with AND condition in arguments.
|
|
|
|
|
ANDCondition ConditionType = iota + 1
|
|
|
|
|
// ORCondition matches responses with AND condition in arguments.
|
|
|
|
|
ORCondition
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ConditionTypes is an table for conversion of condition type from string.
|
|
|
|
|
var ConditionTypes = map[string]ConditionType{
|
|
|
|
|
"and": ANDCondition,
|
|
|
|
|
"or": ORCondition,
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 20:47:41 +05:30
|
|
|
// Result reverts the results of the match if the matcher is of type negative.
|
|
|
|
|
func (m *Matcher) Result(data bool) bool {
|
2020-08-23 22:55:11 +05:30
|
|
|
if m.Negative {
|
|
|
|
|
return !data
|
|
|
|
|
}
|
|
|
|
|
return data
|
|
|
|
|
}
|
2020-12-24 20:47:41 +05:30
|
|
|
|
|
|
|
|
// GetType returns the type of the matcher
|
|
|
|
|
func (m *Matcher) GetType() MatcherType {
|
|
|
|
|
return m.matcherType
|
|
|
|
|
}
|