aboutsummaryrefslogtreecommitdiffhomepage
path: root/modules/caddyhttp/matchers_test.go
diff options
context:
space:
mode:
authorMatt Holt <[email protected]>2020-04-01 10:58:29 -0600
committerGitHub <[email protected]>2020-04-01 10:58:29 -0600
commit73643ea736ca7c6a9ef32dbf78b38fbcdf5e92b2 (patch)
tree4338b090eece5c822860e5817f6207c187c35eee /modules/caddyhttp/matchers_test.go
parent809e72792c501ceeadac4a3b9b327dfc575b9dfd (diff)
downloadcaddy-73643ea736ca7c6a9ef32dbf78b38fbcdf5e92b2.tar.gz
caddy-73643ea736ca7c6a9ef32dbf78b38fbcdf5e92b2.zip
caddyhttp: 'not' matcher now accepts multiple matcher sets and OR's them (#3208)
See https://caddy.community/t/v2-matcher-or-in-not/7355/
Diffstat (limited to 'modules/caddyhttp/matchers_test.go')
-rw-r--r--modules/caddyhttp/matchers_test.go113
1 files changed, 113 insertions, 0 deletions
diff --git a/modules/caddyhttp/matchers_test.go b/modules/caddyhttp/matchers_test.go
index 1e50776e5..021bb9838 100644
--- a/modules/caddyhttp/matchers_test.go
+++ b/modules/caddyhttp/matchers_test.go
@@ -823,6 +823,119 @@ func TestResponseMatcher(t *testing.T) {
}
}
+func TestNotMatcher(t *testing.T) {
+ for i, tc := range []struct {
+ host, path string
+ match MatchNot
+ expect bool
+ }{
+ {
+ host: "example.com", path: "/",
+ match: MatchNot{},
+ expect: true,
+ },
+ {
+ host: "example.com", path: "/foo",
+ match: MatchNot{
+ MatcherSets: []MatcherSet{
+ {
+ MatchPath{"/foo"},
+ },
+ },
+ },
+ expect: false,
+ },
+ {
+ host: "example.com", path: "/bar",
+ match: MatchNot{
+ MatcherSets: []MatcherSet{
+ {
+ MatchPath{"/foo"},
+ },
+ },
+ },
+ expect: true,
+ },
+ {
+ host: "example.com", path: "/bar",
+ match: MatchNot{
+ MatcherSets: []MatcherSet{
+ {
+ MatchPath{"/foo"},
+ },
+ {
+ MatchHost{"example.com"},
+ },
+ },
+ },
+ expect: false,
+ },
+ {
+ host: "example.com", path: "/bar",
+ match: MatchNot{
+ MatcherSets: []MatcherSet{
+ {
+ MatchPath{"/bar"},
+ },
+ {
+ MatchHost{"example.com"},
+ },
+ },
+ },
+ expect: false,
+ },
+ {
+ host: "example.com", path: "/foo",
+ match: MatchNot{
+ MatcherSets: []MatcherSet{
+ {
+ MatchPath{"/bar"},
+ },
+ {
+ MatchHost{"sub.example.com"},
+ },
+ },
+ },
+ expect: true,
+ },
+ {
+ host: "example.com", path: "/foo",
+ match: MatchNot{
+ MatcherSets: []MatcherSet{
+ {
+ MatchPath{"/foo"},
+ MatchHost{"example.com"},
+ },
+ },
+ },
+ expect: false,
+ },
+ {
+ host: "example.com", path: "/foo",
+ match: MatchNot{
+ MatcherSets: []MatcherSet{
+ {
+ MatchPath{"/bar"},
+ MatchHost{"example.com"},
+ },
+ },
+ },
+ expect: true,
+ },
+ } {
+ req := &http.Request{Host: tc.host, URL: &url.URL{Path: tc.path}}
+ repl := caddy.NewReplacer()
+ ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
+ req = req.WithContext(ctx)
+
+ actual := tc.match.Match(req)
+ if actual != tc.expect {
+ t.Errorf("Test %d %+v: Expected %t, got %t for: host=%s path=%s'", i, tc.match, tc.expect, actual, tc.host, tc.path)
+ continue
+ }
+ }
+}
+
func BenchmarkHostMatcherWithoutPlaceholder(b *testing.B) {
req := &http.Request{Host: "localhost"}
repl := caddy.NewReplacer()