mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-28 09:43:31 +03:00
httpserver: add not_ends_with (#1688)
* de-duplicates code for 'not' ops and replicates 'not' op for ends_with * fixes incorrect test expectations
This commit is contained in:
parent
33e1560d53
commit
474f119702
2 changed files with 18 additions and 8 deletions
caddyhttp/httpserver
|
@ -52,6 +52,7 @@ const (
|
||||||
startsWithOp = "starts_with"
|
startsWithOp = "starts_with"
|
||||||
notStartsWithOp = "not_starts_with"
|
notStartsWithOp = "not_starts_with"
|
||||||
endsWithOp = "ends_with"
|
endsWithOp = "ends_with"
|
||||||
|
notEndsWithOp = "not_ends_with"
|
||||||
matchOp = "match"
|
matchOp = "match"
|
||||||
notMatchOp = "not_match"
|
notMatchOp = "not_match"
|
||||||
)
|
)
|
||||||
|
@ -71,6 +72,7 @@ var ifConditions = map[string]ifCondition{
|
||||||
startsWithOp: startsWithFunc,
|
startsWithOp: startsWithFunc,
|
||||||
notStartsWithOp: notStartsWithFunc,
|
notStartsWithOp: notStartsWithFunc,
|
||||||
endsWithOp: endsWithFunc,
|
endsWithOp: endsWithFunc,
|
||||||
|
notEndsWithOp: notEndsWithFunc,
|
||||||
matchOp: matchFunc,
|
matchOp: matchFunc,
|
||||||
notMatchOp: notMatchFunc,
|
notMatchOp: notMatchFunc,
|
||||||
}
|
}
|
||||||
|
@ -84,7 +86,7 @@ func isFunc(a, b string) bool {
|
||||||
// notFunc is condition for Not operator.
|
// notFunc is condition for Not operator.
|
||||||
// It checks for inequality.
|
// It checks for inequality.
|
||||||
func notFunc(a, b string) bool {
|
func notFunc(a, b string) bool {
|
||||||
return a != b
|
return !isFunc(a, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// hasFunc is condition for Has operator.
|
// hasFunc is condition for Has operator.
|
||||||
|
@ -96,7 +98,7 @@ func hasFunc(a, b string) bool {
|
||||||
// notHasFunc is condition for NotHas operator.
|
// notHasFunc is condition for NotHas operator.
|
||||||
// It checks if b is not a substring of a.
|
// It checks if b is not a substring of a.
|
||||||
func notHasFunc(a, b string) bool {
|
func notHasFunc(a, b string) bool {
|
||||||
return !strings.Contains(a, b)
|
return !hasFunc(a, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// startsWithFunc is condition for StartsWith operator.
|
// startsWithFunc is condition for StartsWith operator.
|
||||||
|
@ -108,7 +110,7 @@ func startsWithFunc(a, b string) bool {
|
||||||
// notStartsWithFunc is condition for NotStartsWith operator.
|
// notStartsWithFunc is condition for NotStartsWith operator.
|
||||||
// It checks if b is not a prefix of a.
|
// It checks if b is not a prefix of a.
|
||||||
func notStartsWithFunc(a, b string) bool {
|
func notStartsWithFunc(a, b string) bool {
|
||||||
return !strings.HasPrefix(a, b)
|
return !startsWithFunc(a, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// endsWithFunc is condition for EndsWith operator.
|
// endsWithFunc is condition for EndsWith operator.
|
||||||
|
@ -117,6 +119,12 @@ func endsWithFunc(a, b string) bool {
|
||||||
return strings.HasSuffix(a, b)
|
return strings.HasSuffix(a, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// notEndsWithFunc is condition for NotEndsWith operator.
|
||||||
|
// It checks if b is not a suffix of a.
|
||||||
|
func notEndsWithFunc(a, b string) bool {
|
||||||
|
return !endsWithFunc(a, b)
|
||||||
|
}
|
||||||
|
|
||||||
// matchFunc is condition for Match operator.
|
// matchFunc is condition for Match operator.
|
||||||
// It does regexp matching of a against pattern in b
|
// It does regexp matching of a against pattern in b
|
||||||
// and returns if they match.
|
// and returns if they match.
|
||||||
|
@ -129,8 +137,7 @@ func matchFunc(a, b string) bool {
|
||||||
// It does regexp matching of a against pattern in b
|
// It does regexp matching of a against pattern in b
|
||||||
// and returns if they do not match.
|
// and returns if they do not match.
|
||||||
func notMatchFunc(a, b string) bool {
|
func notMatchFunc(a, b string) bool {
|
||||||
matched, _ := regexp.MatchString(b, a)
|
return !matchFunc(a, b)
|
||||||
return !matched
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ifCond is statement for a IfMatcher condition.
|
// ifCond is statement for a IfMatcher condition.
|
||||||
|
|
|
@ -38,6 +38,9 @@ func TestConditions(t *testing.T) {
|
||||||
{"bab ends_with bb", false},
|
{"bab ends_with bb", false},
|
||||||
{"bab ends_with bab", true},
|
{"bab ends_with bab", true},
|
||||||
{"bab ends_with ab", true},
|
{"bab ends_with ab", true},
|
||||||
|
{"bab not_ends_with bb", true},
|
||||||
|
{"bab not_ends_with ab", false},
|
||||||
|
{"bab not_ends_with bab", false},
|
||||||
{"a match *", false},
|
{"a match *", false},
|
||||||
{"a match a", true},
|
{"a match a", true},
|
||||||
{"a match .*", true},
|
{"a match .*", true},
|
||||||
|
|
Loading…
Reference in a new issue