diff --git a/pkg/matchers/match.go b/pkg/matchers/match.go index 39f395622..50d3933b3 100644 --- a/pkg/matchers/match.go +++ b/pkg/matchers/match.go @@ -1,6 +1,7 @@ package matchers import ( + "encoding/hex" "net/http" "strings" ) @@ -36,6 +37,18 @@ func (m *Matcher) Match(resp *http.Response, body, headers string) bool { } return m.matchRegex(body) } + case BinaryMatcher: + // Match the parts as required for binary characters check + if m.part == BodyPart { + return m.matchBinary(body) + } else if m.part == HeaderPart { + return m.matchBinary(headers) + } else { + if !m.matchBinary(headers) { + return false + } + return m.matchBinary(body) + } } return false } @@ -127,3 +140,34 @@ func (m *Matcher) matchRegex(corpus string) bool { } return false } + +// matchWords matches a word check against an HTTP Response/Headers. +func (m *Matcher) matchBinary(corpus string) bool { + + // Iterate over all the words accepted as valid + for i, binary := range m.Binary { + // Continue if the word doesn't match + + hexa, _ := hex.DecodeString(binary) + if !strings.Contains(corpus, string(hexa)) { + // If we are in an AND request and a match failed, + // return false as the AND condition fails on any single mismatch. + if m.condition == ANDCondition { + return false + } + // Continue with the flow since its an OR Condition. + continue + } + + // If the condition was an OR, return on the first match. + if m.condition == ORCondition { + return true + } + + // If we are at the end of the words, return with true + if len(m.Binary)-1 == i { + return true + } + } + return false +} diff --git a/pkg/matchers/matchers.go b/pkg/matchers/matchers.go index ffc0bfecf..4629d1f15 100644 --- a/pkg/matchers/matchers.go +++ b/pkg/matchers/matchers.go @@ -19,6 +19,8 @@ type Matcher struct { Words []string `yaml:"words,omitempty"` // Regex are the regex pattern required to be present in the response Regex []string `yaml:"regex,omitempty"` + // Binary are the binary characters required to be present in the response + Binary []string `yaml:"binary,omitempty"` // regexCompiled is the compiled variant regexCompiled []*regexp.Regexp @@ -45,6 +47,8 @@ const ( WordsMatcher MatcherType = iota + 1 // RegexMatcher matches responses with regexes RegexMatcher + // BinaryMatcher matches responses with words + BinaryMatcher MatcherType = iota + 2 // StatusMatcher matches responses with status codes StatusMatcher // SizeMatcher matches responses with response size @@ -57,6 +61,7 @@ var MatcherTypes = map[string]MatcherType{ "size": SizeMatcher, "word": WordsMatcher, "regex": RegexMatcher, + "binary": BinaryMatcher, } // ConditionType is the type of condition for matcher