mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 03:27:23 -05: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
					
				@ -52,6 +52,7 @@ const (
 | 
			
		||||
	startsWithOp    = "starts_with"
 | 
			
		||||
	notStartsWithOp = "not_starts_with"
 | 
			
		||||
	endsWithOp      = "ends_with"
 | 
			
		||||
	notEndsWithOp   = "not_ends_with"
 | 
			
		||||
	matchOp         = "match"
 | 
			
		||||
	notMatchOp      = "not_match"
 | 
			
		||||
)
 | 
			
		||||
@ -71,6 +72,7 @@ var ifConditions = map[string]ifCondition{
 | 
			
		||||
	startsWithOp:    startsWithFunc,
 | 
			
		||||
	notStartsWithOp: notStartsWithFunc,
 | 
			
		||||
	endsWithOp:      endsWithFunc,
 | 
			
		||||
	notEndsWithOp:   notEndsWithFunc,
 | 
			
		||||
	matchOp:         matchFunc,
 | 
			
		||||
	notMatchOp:      notMatchFunc,
 | 
			
		||||
}
 | 
			
		||||
@ -84,7 +86,7 @@ func isFunc(a, b string) bool {
 | 
			
		||||
// notFunc is condition for Not operator.
 | 
			
		||||
// It checks for inequality.
 | 
			
		||||
func notFunc(a, b string) bool {
 | 
			
		||||
	return a != b
 | 
			
		||||
	return !isFunc(a, b)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// hasFunc is condition for Has operator.
 | 
			
		||||
@ -96,7 +98,7 @@ func hasFunc(a, b string) bool {
 | 
			
		||||
// notHasFunc is condition for NotHas operator.
 | 
			
		||||
// It checks if b is not a substring of a.
 | 
			
		||||
func notHasFunc(a, b string) bool {
 | 
			
		||||
	return !strings.Contains(a, b)
 | 
			
		||||
	return !hasFunc(a, b)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// startsWithFunc is condition for StartsWith operator.
 | 
			
		||||
@ -108,7 +110,7 @@ func startsWithFunc(a, b string) bool {
 | 
			
		||||
// notStartsWithFunc is condition for NotStartsWith operator.
 | 
			
		||||
// It checks if b is not a prefix of a.
 | 
			
		||||
func notStartsWithFunc(a, b string) bool {
 | 
			
		||||
	return !strings.HasPrefix(a, b)
 | 
			
		||||
	return !startsWithFunc(a, b)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// endsWithFunc is condition for EndsWith operator.
 | 
			
		||||
@ -117,6 +119,12 @@ func endsWithFunc(a, b string) bool {
 | 
			
		||||
	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.
 | 
			
		||||
// It does regexp matching of a against pattern in b
 | 
			
		||||
// 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
 | 
			
		||||
// and returns if they do not match.
 | 
			
		||||
func notMatchFunc(a, b string) bool {
 | 
			
		||||
	matched, _ := regexp.MatchString(b, a)
 | 
			
		||||
	return !matched
 | 
			
		||||
	return !matchFunc(a, b)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ifCond is statement for a IfMatcher condition.
 | 
			
		||||
 | 
			
		||||
@ -38,6 +38,9 @@ func TestConditions(t *testing.T) {
 | 
			
		||||
		{"bab ends_with bb", false},
 | 
			
		||||
		{"bab ends_with bab", 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 a", true},
 | 
			
		||||
		{"a match .*", true},
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user