Skip to content

Commit

Permalink
Added negative matchers support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ice3man543 committed Aug 23, 2020
1 parent 10a084d commit 4e74cfd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
24 changes: 12 additions & 12 deletions v2/pkg/matchers/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,39 @@ import (
func (m *Matcher) Match(resp *http.Response, body, headers string) bool {
switch m.matcherType {
case StatusMatcher:
return m.matchStatusCode(resp.StatusCode)
return m.isNegative(m.matchStatusCode(resp.StatusCode))
case SizeMatcher:
return m.matchSizeCode(len(body))
return m.isNegative(m.matchSizeCode(len(body)))
case WordsMatcher:
// Match the parts as required for word check
if m.part == BodyPart {
return m.matchWords(body)
return m.isNegative(m.matchWords(body))
} else if m.part == HeaderPart {
return m.matchWords(headers)
return m.isNegative(m.matchWords(headers))
} else {
return m.matchWords(headers) || m.matchWords(body)
return m.isNegative(m.matchWords(headers) || m.matchWords(body))
}
case RegexMatcher:
// Match the parts as required for regex check
if m.part == BodyPart {
return m.matchRegex(body)
return m.isNegative(m.matchRegex(body))
} else if m.part == HeaderPart {
return m.matchRegex(headers)
return m.isNegative(m.matchRegex(headers))
} else {
return m.matchRegex(headers) || m.matchRegex(body)
return m.isNegative(m.matchRegex(headers) || m.matchRegex(body))
}
case BinaryMatcher:
// Match the parts as required for binary characters check
if m.part == BodyPart {
return m.matchBinary(body)
return m.isNegative(m.matchBinary(body))
} else if m.part == HeaderPart {
return m.matchBinary(headers)
return m.isNegative(m.matchBinary(headers))
} else {
return m.matchBinary(headers) || m.matchBinary(body)
return m.isNegative(m.matchBinary(headers) || m.matchBinary(body))
}
case DSLMatcher:
// Match complex query
return m.matchDSL(httpToMap(resp, body, headers))
return m.isNegative(m.matchDSL(httpToMap(resp, body, headers)))
}
return false
}
Expand Down
13 changes: 13 additions & 0 deletions v2/pkg/matchers/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ type Matcher struct {
Part string `yaml:"part,omitempty"`
// part is the part of the request to match
part Part

// Negative specifies if the match should be reversed
// It will only match if the condition is not true.
Negative bool `yaml:"negative,omitempty"`
}

// MatcherType is the type of the matcher specified
Expand Down Expand Up @@ -114,3 +118,12 @@ var PartTypes = map[string]Part{
func (m *Matcher) GetPart() Part {
return m.part
}

// isNegative reverts the results of the match if the matcher
// is of type negative.
func (m *Matcher) isNegative(data bool) bool {
if m.Negative {
return !data
}
return data
}

0 comments on commit 4e74cfd

Please sign in to comment.