2019-07-01 01:07:58 +03:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-05-11 06:07:02 +03:00
|
|
|
package caddyhttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2019-10-14 20:29:36 +03:00
|
|
|
"os"
|
2019-05-11 06:07:02 +03:00
|
|
|
"testing"
|
2019-05-20 19:59:20 +03:00
|
|
|
|
2019-07-02 21:37:06 +03:00
|
|
|
"github.com/caddyserver/caddy/v2"
|
2019-05-11 06:07:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHostMatcher(t *testing.T) {
|
2019-10-14 20:29:36 +03:00
|
|
|
err := os.Setenv("GO_BENCHMARK_DOMAIN", "localhost")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error while setting up environment: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-11 06:07:02 +03:00
|
|
|
for i, tc := range []struct {
|
2019-05-22 21:32:36 +03:00
|
|
|
match MatchHost
|
2019-05-11 06:07:02 +03:00
|
|
|
input string
|
|
|
|
expect bool
|
|
|
|
}{
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHost{},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "example.com",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHost{"example.com"},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "example.com",
|
|
|
|
expect: true,
|
|
|
|
},
|
2019-11-15 22:47:06 +03:00
|
|
|
{
|
|
|
|
match: MatchHost{"EXAMPLE.COM"},
|
|
|
|
input: "example.com",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match: MatchHost{"example.com"},
|
|
|
|
input: "EXAMPLE.COM",
|
|
|
|
expect: true,
|
|
|
|
},
|
2019-05-11 06:07:02 +03:00
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHost{"example.com"},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "foo.example.com",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHost{"example.com"},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "EXAMPLE.COM",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHost{"foo.example.com"},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "foo.example.com",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHost{"foo.example.com"},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "bar.example.com",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHost{"*.example.com"},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "example.com",
|
|
|
|
expect: false,
|
|
|
|
},
|
2019-11-15 22:47:06 +03:00
|
|
|
{
|
|
|
|
match: MatchHost{"*.example.com"},
|
|
|
|
input: "SUB.EXAMPLE.COM",
|
|
|
|
expect: true,
|
|
|
|
},
|
2019-05-11 06:07:02 +03:00
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHost{"*.example.com"},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "foo.example.com",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHost{"*.example.com"},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "foo.bar.example.com",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHost{"*.example.com", "example.net"},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "example.net",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHost{"example.net", "*.example.com"},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "foo.example.com",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHost{"*.example.net", "*.*.example.com"},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "foo.bar.example.com",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHost{"*.example.net", "sub.*.example.com"},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "sub.foo.example.com",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHost{"*.example.net", "sub.*.example.com"},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "sub.foo.example.net",
|
|
|
|
expect: false,
|
|
|
|
},
|
2020-07-07 17:11:35 +03:00
|
|
|
{
|
|
|
|
match: MatchHost{"www.*.*"},
|
|
|
|
input: "www.example.com",
|
|
|
|
expect: true,
|
|
|
|
},
|
2019-06-21 05:24:46 +03:00
|
|
|
{
|
|
|
|
match: MatchHost{"example.com"},
|
|
|
|
input: "example.com:5555",
|
|
|
|
expect: true,
|
|
|
|
},
|
2019-10-14 20:29:36 +03:00
|
|
|
{
|
|
|
|
match: MatchHost{"{env.GO_BENCHMARK_DOMAIN}"},
|
|
|
|
input: "localhost",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match: MatchHost{"{env.GO_NONEXISTENT}"},
|
|
|
|
input: "localhost",
|
|
|
|
expect: false,
|
|
|
|
},
|
2019-05-11 06:07:02 +03:00
|
|
|
} {
|
|
|
|
req := &http.Request{Host: tc.input}
|
2019-10-14 20:29:36 +03:00
|
|
|
repl := caddy.NewReplacer()
|
|
|
|
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
|
2019-05-11 06:07:02 +03:00
|
|
|
actual := tc.match.Match(req)
|
|
|
|
if actual != tc.expect {
|
|
|
|
t.Errorf("Test %d %v: Expected %t, got %t for '%s'", i, tc.match, tc.expect, actual, tc.input)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPathMatcher(t *testing.T) {
|
|
|
|
for i, tc := range []struct {
|
2019-05-22 21:32:36 +03:00
|
|
|
match MatchPath
|
2019-05-11 06:07:02 +03:00
|
|
|
input string
|
|
|
|
expect bool
|
|
|
|
}{
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchPath{},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "/",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchPath{"/"},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "/",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchPath{"/foo/bar"},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "/",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchPath{"/foo/bar"},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "/foo/bar",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchPath{"/foo/bar/"},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "/foo/bar",
|
|
|
|
expect: false,
|
|
|
|
},
|
2020-01-10 00:00:32 +03:00
|
|
|
{
|
|
|
|
match: MatchPath{"/foo/bar/"},
|
|
|
|
input: "/foo/bar/",
|
|
|
|
expect: true,
|
|
|
|
},
|
2019-05-11 06:07:02 +03:00
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchPath{"/foo/bar/", "/other"},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "/other/",
|
2020-01-10 00:00:32 +03:00
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match: MatchPath{"/foo/bar/", "/other"},
|
|
|
|
input: "/other",
|
2019-05-11 06:07:02 +03:00
|
|
|
expect: true,
|
|
|
|
},
|
2019-05-20 19:59:20 +03:00
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchPath{"*.ext"},
|
2020-01-17 03:08:52 +03:00
|
|
|
input: "/foo/bar.ext",
|
2019-11-15 22:47:06 +03:00
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match: MatchPath{"*.php"},
|
|
|
|
input: "/index.PHP",
|
2019-05-20 19:59:20 +03:00
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchPath{"*.ext"},
|
2019-05-20 19:59:20 +03:00
|
|
|
input: "/foo/bar.ext",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchPath{"/foo/*/baz"},
|
2019-05-20 19:59:20 +03:00
|
|
|
input: "/foo/bar/baz",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchPath{"/foo/*/baz/bam"},
|
2019-05-20 19:59:20 +03:00
|
|
|
input: "/foo/bar/bam",
|
|
|
|
expect: false,
|
|
|
|
},
|
2019-11-29 07:11:45 +03:00
|
|
|
{
|
2020-01-10 00:00:32 +03:00
|
|
|
match: MatchPath{"*substring*"},
|
|
|
|
input: "/foo/substring/bar.txt",
|
2019-11-29 07:11:45 +03:00
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2020-01-10 00:00:32 +03:00
|
|
|
match: MatchPath{"/foo"},
|
2019-11-29 07:11:45 +03:00
|
|
|
input: "/foo/bar",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2020-01-10 00:00:32 +03:00
|
|
|
match: MatchPath{"/foo"},
|
|
|
|
input: "/foo/bar",
|
|
|
|
expect: false,
|
2019-11-29 07:11:45 +03:00
|
|
|
},
|
2019-11-15 22:47:06 +03:00
|
|
|
{
|
|
|
|
match: MatchPath{"/foo"},
|
|
|
|
input: "/FOO",
|
|
|
|
expect: true,
|
|
|
|
},
|
2020-01-10 00:00:32 +03:00
|
|
|
{
|
|
|
|
match: MatchPath{"/foo*"},
|
|
|
|
input: "/FOOOO",
|
|
|
|
expect: true,
|
|
|
|
},
|
2019-11-15 22:47:06 +03:00
|
|
|
{
|
|
|
|
match: MatchPath{"/foo/bar.txt"},
|
|
|
|
input: "/foo/BAR.txt",
|
|
|
|
expect: true,
|
|
|
|
},
|
2020-03-17 01:08:33 +03:00
|
|
|
{
|
|
|
|
match: MatchPath{"*"},
|
|
|
|
input: "/",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match: MatchPath{"*"},
|
|
|
|
input: "/foo/bar",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match: MatchPath{"**"},
|
|
|
|
input: "/",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match: MatchPath{"**"},
|
|
|
|
input: "/foo/bar",
|
|
|
|
expect: true,
|
|
|
|
},
|
2019-05-11 06:07:02 +03:00
|
|
|
} {
|
|
|
|
req := &http.Request{URL: &url.URL{Path: tc.input}}
|
2020-01-10 00:00:32 +03:00
|
|
|
repl := caddy.NewReplacer()
|
|
|
|
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
|
2019-05-11 06:07:02 +03:00
|
|
|
actual := tc.match.Match(req)
|
|
|
|
if actual != tc.expect {
|
|
|
|
t.Errorf("Test %d %v: Expected %t, got %t for '%s'", i, tc.match, tc.expect, actual, tc.input)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 20:14:04 +03:00
|
|
|
func TestPathMatcherWindows(t *testing.T) {
|
|
|
|
// only Windows has this bug where it will ignore
|
|
|
|
// trailing dots and spaces in a filename, but we
|
|
|
|
// test for it on all platforms to be more consistent
|
2020-01-10 00:00:32 +03:00
|
|
|
|
2019-12-17 20:14:04 +03:00
|
|
|
req := &http.Request{URL: &url.URL{Path: "/index.php . . .."}}
|
2020-01-10 00:00:32 +03:00
|
|
|
repl := caddy.NewReplacer()
|
|
|
|
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
|
|
|
|
match := MatchPath{"*.php"}
|
2019-12-17 20:14:04 +03:00
|
|
|
matched := match.Match(req)
|
|
|
|
if !matched {
|
|
|
|
t.Errorf("Expected to match; should ignore trailing dots and spaces")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 06:07:02 +03:00
|
|
|
func TestPathREMatcher(t *testing.T) {
|
|
|
|
for i, tc := range []struct {
|
2019-05-22 21:32:36 +03:00
|
|
|
match MatchPathRE
|
2019-05-11 06:07:02 +03:00
|
|
|
input string
|
|
|
|
expect bool
|
|
|
|
expectRepl map[string]string
|
|
|
|
}{
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchPathRE{},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "/",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 22:13:39 +03:00
|
|
|
match: MatchPathRE{MatchRegexp{Pattern: "/"}},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "/",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 22:13:39 +03:00
|
|
|
match: MatchPathRE{MatchRegexp{Pattern: "/foo"}},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "/foo",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 22:13:39 +03:00
|
|
|
match: MatchPathRE{MatchRegexp{Pattern: "/foo"}},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "/foo/",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 22:13:39 +03:00
|
|
|
match: MatchPathRE{MatchRegexp{Pattern: "/bar"}},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "/foo/",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 22:13:39 +03:00
|
|
|
match: MatchPathRE{MatchRegexp{Pattern: "^/bar"}},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "/foo/bar",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 22:13:39 +03:00
|
|
|
match: MatchPathRE{MatchRegexp{Pattern: "^/foo/(.*)/baz$", Name: "name"}},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "/foo/bar/baz",
|
|
|
|
expect: true,
|
|
|
|
expectRepl: map[string]string{"name.1": "bar"},
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 22:13:39 +03:00
|
|
|
match: MatchPathRE{MatchRegexp{Pattern: "^/foo/(?P<myparam>.*)/baz$", Name: "name"}},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: "/foo/bar/baz",
|
|
|
|
expect: true,
|
|
|
|
expectRepl: map[string]string{"name.myparam": "bar"},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
// compile the regexp and validate its name
|
2019-06-21 23:36:26 +03:00
|
|
|
err := tc.match.Provision(caddy.Context{})
|
2019-05-11 06:07:02 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test %d %v: Provisioning: %v", i, tc.match, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = tc.match.Validate()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test %d %v: Validating: %v", i, tc.match, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up the fake request and its Replacer
|
|
|
|
req := &http.Request{URL: &url.URL{Path: tc.input}}
|
2019-06-14 20:58:28 +03:00
|
|
|
repl := caddy.NewReplacer()
|
|
|
|
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
2019-05-11 06:07:02 +03:00
|
|
|
req = req.WithContext(ctx)
|
2019-05-21 08:48:43 +03:00
|
|
|
addHTTPVarsToReplacer(repl, req, httptest.NewRecorder())
|
2019-05-11 06:07:02 +03:00
|
|
|
|
|
|
|
actual := tc.match.Match(req)
|
|
|
|
if actual != tc.expect {
|
|
|
|
t.Errorf("Test %d [%v]: Expected %t, got %t for input '%s'",
|
|
|
|
i, tc.match.Pattern, tc.expect, actual, tc.input)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, expectVal := range tc.expectRepl {
|
2019-11-29 21:23:49 +03:00
|
|
|
placeholder := fmt.Sprintf("{http.regexp.%s}", key)
|
2019-05-20 19:59:20 +03:00
|
|
|
actualVal := repl.ReplaceAll(placeholder, "<empty>")
|
2019-05-11 06:07:02 +03:00
|
|
|
if actualVal != expectVal {
|
2019-11-29 21:23:49 +03:00
|
|
|
t.Errorf("Test %d [%v]: Expected placeholder {http.regexp.%s} to be '%s' but got '%s'",
|
2019-05-11 06:07:02 +03:00
|
|
|
i, tc.match.Pattern, key, expectVal, actualVal)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeaderMatcher(t *testing.T) {
|
2021-02-12 02:27:09 +03:00
|
|
|
repl := caddy.NewReplacer()
|
|
|
|
repl.Set("a", "foobar")
|
|
|
|
|
2019-05-11 06:07:02 +03:00
|
|
|
for i, tc := range []struct {
|
2019-05-22 21:32:36 +03:00
|
|
|
match MatchHeader
|
2019-05-11 06:07:02 +03:00
|
|
|
input http.Header // make sure these are canonical cased (std lib will do that in a real request)
|
2020-02-20 20:55:47 +03:00
|
|
|
host string
|
2019-05-11 06:07:02 +03:00
|
|
|
expect bool
|
|
|
|
}{
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHeader{"Field": []string{"foo"}},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: http.Header{"Field": []string{"foo"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHeader{"Field": []string{"foo", "bar"}},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: http.Header{"Field": []string{"bar"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHeader{"Field": []string{"foo", "bar"}},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: http.Header{"Alakazam": []string{"kapow"}},
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHeader{"Field": []string{"foo", "bar"}},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: http.Header{"Field": []string{"kapow"}},
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHeader{"Field": []string{"foo", "bar"}},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: http.Header{"Field": []string{"kapow", "foo"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHeader{"Field1": []string{"foo"}, "Field2": []string{"bar"}},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: http.Header{"Field1": []string{"foo"}, "Field2": []string{"bar"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHeader{"field1": []string{"foo"}, "field2": []string{"bar"}},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: http.Header{"Field1": []string{"foo"}, "Field2": []string{"bar"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 21:32:36 +03:00
|
|
|
match: MatchHeader{"field1": []string{"foo"}, "field2": []string{"bar"}},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: http.Header{"Field1": []string{"foo"}, "Field2": []string{"kapow"}},
|
|
|
|
expect: false,
|
|
|
|
},
|
2020-02-20 20:55:47 +03:00
|
|
|
{
|
|
|
|
match: MatchHeader{"field1": []string{"*"}},
|
|
|
|
input: http.Header{"Field1": []string{"foo"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match: MatchHeader{"field1": []string{"*"}},
|
|
|
|
input: http.Header{"Field2": []string{"foo"}},
|
|
|
|
expect: false,
|
|
|
|
},
|
2020-05-27 02:35:27 +03:00
|
|
|
{
|
|
|
|
match: MatchHeader{"Field1": []string{"foo*"}},
|
|
|
|
input: http.Header{"Field1": []string{"foo"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match: MatchHeader{"Field1": []string{"foo*"}},
|
|
|
|
input: http.Header{"Field1": []string{"asdf", "foobar"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match: MatchHeader{"Field1": []string{"*bar"}},
|
|
|
|
input: http.Header{"Field1": []string{"asdf", "foobar"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
2020-02-20 20:55:47 +03:00
|
|
|
{
|
|
|
|
match: MatchHeader{"host": []string{"localhost"}},
|
|
|
|
input: http.Header{},
|
|
|
|
host: "localhost",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match: MatchHeader{"host": []string{"localhost"}},
|
|
|
|
input: http.Header{},
|
|
|
|
host: "caddyserver.com",
|
|
|
|
expect: false,
|
|
|
|
},
|
2020-11-17 21:29:43 +03:00
|
|
|
{
|
|
|
|
match: MatchHeader{"Must-Not-Exist": nil},
|
|
|
|
input: http.Header{},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match: MatchHeader{"Must-Not-Exist": nil},
|
|
|
|
input: http.Header{"Must-Not-Exist": []string{"do not match"}},
|
|
|
|
expect: false,
|
|
|
|
},
|
2021-02-12 02:27:09 +03:00
|
|
|
{
|
|
|
|
match: MatchHeader{"Foo": []string{"{a}"}},
|
|
|
|
input: http.Header{"Foo": []string{"foobar"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match: MatchHeader{"Foo": []string{"{a}"}},
|
|
|
|
input: http.Header{"Foo": []string{"asdf"}},
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match: MatchHeader{"Foo": []string{"{a}*"}},
|
|
|
|
input: http.Header{"Foo": []string{"foobar-baz"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
2019-05-11 06:07:02 +03:00
|
|
|
} {
|
2020-02-20 20:55:47 +03:00
|
|
|
req := &http.Request{Header: tc.input, Host: tc.host}
|
2021-02-12 02:27:09 +03:00
|
|
|
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
|
2019-05-11 06:07:02 +03:00
|
|
|
actual := tc.match.Match(req)
|
|
|
|
if actual != tc.expect {
|
|
|
|
t.Errorf("Test %d %v: Expected %t, got %t for '%s'", i, tc.match, tc.expect, actual, tc.input)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 23:05:22 +03:00
|
|
|
func TestQueryMatcher(t *testing.T) {
|
|
|
|
for i, tc := range []struct {
|
|
|
|
scenario string
|
|
|
|
match MatchQuery
|
|
|
|
input string
|
|
|
|
expect bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
scenario: "non match against a specific value",
|
|
|
|
match: MatchQuery{"debug": []string{"1"}},
|
|
|
|
input: "/",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
scenario: "match against a specific value",
|
|
|
|
match: MatchQuery{"debug": []string{"1"}},
|
|
|
|
input: "/?debug=1",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
scenario: "match against a wildcard",
|
|
|
|
match: MatchQuery{"debug": []string{"*"}},
|
|
|
|
input: "/?debug=something",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
scenario: "non match against a wildcarded",
|
|
|
|
match: MatchQuery{"debug": []string{"*"}},
|
|
|
|
input: "/?other=something",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
scenario: "match against an empty value",
|
|
|
|
match: MatchQuery{"debug": []string{""}},
|
|
|
|
input: "/?debug",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
scenario: "non match against an empty value",
|
|
|
|
match: MatchQuery{"debug": []string{""}},
|
|
|
|
input: "/?someparam",
|
|
|
|
expect: false,
|
|
|
|
},
|
2020-06-16 19:41:37 +03:00
|
|
|
{
|
2020-06-16 21:02:23 +03:00
|
|
|
scenario: "empty matcher value should match empty query",
|
|
|
|
match: MatchQuery{},
|
2020-06-16 19:41:37 +03:00
|
|
|
input: "/?",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
scenario: "nil matcher value should NOT match a non-empty query",
|
2020-06-16 21:02:23 +03:00
|
|
|
match: MatchQuery{},
|
2020-06-16 19:41:37 +03:00
|
|
|
input: "/?foo=bar",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
scenario: "non-nil matcher should NOT match an empty query",
|
2020-06-16 21:02:23 +03:00
|
|
|
match: MatchQuery{"": nil},
|
2020-06-16 19:41:37 +03:00
|
|
|
input: "/?",
|
|
|
|
expect: false,
|
|
|
|
},
|
2020-06-27 00:14:47 +03:00
|
|
|
{
|
|
|
|
scenario: "match against a placeholder value",
|
|
|
|
match: MatchQuery{"debug": []string{"{http.vars.debug}"}},
|
|
|
|
input: "/?debug=1",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
scenario: "match against a placeholder key",
|
|
|
|
match: MatchQuery{"{http.vars.key}": []string{"1"}},
|
|
|
|
input: "/?somekey=1",
|
|
|
|
expect: true,
|
|
|
|
},
|
2019-11-29 23:05:22 +03:00
|
|
|
} {
|
|
|
|
|
|
|
|
u, _ := url.Parse(tc.input)
|
|
|
|
|
|
|
|
req := &http.Request{URL: u}
|
2020-06-27 00:14:47 +03:00
|
|
|
repl := caddy.NewReplacer()
|
|
|
|
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
|
|
|
repl.Set("http.vars.debug", "1")
|
|
|
|
repl.Set("http.vars.key", "somekey")
|
|
|
|
req = req.WithContext(ctx)
|
2019-11-29 23:05:22 +03:00
|
|
|
actual := tc.match.Match(req)
|
|
|
|
if actual != tc.expect {
|
|
|
|
t.Errorf("Test %d %v: Expected %t, got %t for '%s'", i, tc.match, tc.expect, actual, tc.input)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 06:07:02 +03:00
|
|
|
func TestHeaderREMatcher(t *testing.T) {
|
|
|
|
for i, tc := range []struct {
|
2019-05-22 21:32:36 +03:00
|
|
|
match MatchHeaderRE
|
2019-05-11 06:07:02 +03:00
|
|
|
input http.Header // make sure these are canonical cased (std lib will do that in a real request)
|
2020-02-20 20:55:47 +03:00
|
|
|
host string
|
2019-05-11 06:07:02 +03:00
|
|
|
expect bool
|
|
|
|
expectRepl map[string]string
|
|
|
|
}{
|
|
|
|
{
|
2019-05-22 22:13:39 +03:00
|
|
|
match: MatchHeaderRE{"Field": &MatchRegexp{Pattern: "foo"}},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: http.Header{"Field": []string{"foo"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 22:13:39 +03:00
|
|
|
match: MatchHeaderRE{"Field": &MatchRegexp{Pattern: "$foo^"}},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: http.Header{"Field": []string{"foobar"}},
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 22:13:39 +03:00
|
|
|
match: MatchHeaderRE{"Field": &MatchRegexp{Pattern: "^foo(.*)$", Name: "name"}},
|
2019-05-11 06:07:02 +03:00
|
|
|
input: http.Header{"Field": []string{"foobar"}},
|
|
|
|
expect: true,
|
|
|
|
expectRepl: map[string]string{"name.1": "bar"},
|
|
|
|
},
|
2020-02-20 20:55:47 +03:00
|
|
|
{
|
|
|
|
match: MatchHeaderRE{"Field": &MatchRegexp{Pattern: "^foo.*$", Name: "name"}},
|
|
|
|
input: http.Header{"Field": []string{"barfoo", "foobar"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match: MatchHeaderRE{"host": &MatchRegexp{Pattern: "^localhost$", Name: "name"}},
|
|
|
|
input: http.Header{},
|
|
|
|
host: "localhost",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match: MatchHeaderRE{"host": &MatchRegexp{Pattern: "^local$", Name: "name"}},
|
|
|
|
input: http.Header{},
|
|
|
|
host: "localhost",
|
|
|
|
expect: false,
|
|
|
|
},
|
2019-05-11 06:07:02 +03:00
|
|
|
} {
|
|
|
|
// compile the regexp and validate its name
|
2019-06-21 23:36:26 +03:00
|
|
|
err := tc.match.Provision(caddy.Context{})
|
2019-05-11 06:07:02 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test %d %v: Provisioning: %v", i, tc.match, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = tc.match.Validate()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test %d %v: Validating: %v", i, tc.match, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up the fake request and its Replacer
|
2020-02-20 20:55:47 +03:00
|
|
|
req := &http.Request{Header: tc.input, URL: new(url.URL), Host: tc.host}
|
2019-06-14 20:58:28 +03:00
|
|
|
repl := caddy.NewReplacer()
|
|
|
|
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
2019-05-11 06:07:02 +03:00
|
|
|
req = req.WithContext(ctx)
|
2019-05-21 08:48:43 +03:00
|
|
|
addHTTPVarsToReplacer(repl, req, httptest.NewRecorder())
|
2019-05-11 06:07:02 +03:00
|
|
|
|
|
|
|
actual := tc.match.Match(req)
|
|
|
|
if actual != tc.expect {
|
|
|
|
t.Errorf("Test %d [%v]: Expected %t, got %t for input '%s'",
|
|
|
|
i, tc.match, tc.expect, actual, tc.input)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, expectVal := range tc.expectRepl {
|
2019-11-29 21:23:49 +03:00
|
|
|
placeholder := fmt.Sprintf("{http.regexp.%s}", key)
|
2019-05-20 19:59:20 +03:00
|
|
|
actualVal := repl.ReplaceAll(placeholder, "<empty>")
|
2019-05-11 06:07:02 +03:00
|
|
|
if actualVal != expectVal {
|
2019-11-29 21:23:49 +03:00
|
|
|
t.Errorf("Test %d [%v]: Expected placeholder {http.regexp.%s} to be '%s' but got '%s'",
|
2019-05-11 06:07:02 +03:00
|
|
|
i, tc.match, key, expectVal, actualVal)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 03:53:08 +03:00
|
|
|
|
2021-05-02 19:35:28 +03:00
|
|
|
func BenchmarkHeaderREMatcher(b *testing.B) {
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
match := MatchHeaderRE{"Field": &MatchRegexp{Pattern: "^foo(.*)$", Name: "name"}}
|
|
|
|
input := http.Header{"Field": []string{"foobar"}}
|
|
|
|
var host string
|
|
|
|
err := match.Provision(caddy.Context{})
|
|
|
|
if err != nil {
|
|
|
|
b.Errorf("Test %d %v: Provisioning: %v", i, match, err)
|
|
|
|
}
|
|
|
|
err = match.Validate()
|
|
|
|
if err != nil {
|
|
|
|
b.Errorf("Test %d %v: Validating: %v", i, match, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up the fake request and its Replacer
|
|
|
|
req := &http.Request{Header: input, URL: new(url.URL), Host: host}
|
|
|
|
repl := caddy.NewReplacer()
|
|
|
|
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
addHTTPVarsToReplacer(repl, req, httptest.NewRecorder())
|
|
|
|
for run := 0; run < b.N; run++ {
|
|
|
|
match.Match(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 22:26:31 +03:00
|
|
|
func TestVarREMatcher(t *testing.T) {
|
|
|
|
for i, tc := range []struct {
|
|
|
|
desc string
|
|
|
|
match MatchVarsRE
|
|
|
|
input VarsMiddleware
|
|
|
|
expect bool
|
|
|
|
expectRepl map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "match static value within var set by the VarsMiddleware succeeds",
|
|
|
|
match: MatchVarsRE{"Var1": &MatchRegexp{Pattern: "foo"}},
|
|
|
|
input: VarsMiddleware{"Var1": "here is foo val"},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "value set by VarsMiddleware not satisfying regexp matcher fails to match",
|
|
|
|
match: MatchVarsRE{"Var1": &MatchRegexp{Pattern: "$foo^"}},
|
|
|
|
input: VarsMiddleware{"Var1": "foobar"},
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "successfully matched value is captured and its placeholder is added to replacer",
|
|
|
|
match: MatchVarsRE{"Var1": &MatchRegexp{Pattern: "^foo(.*)$", Name: "name"}},
|
|
|
|
input: VarsMiddleware{"Var1": "foobar"},
|
|
|
|
expect: true,
|
|
|
|
expectRepl: map[string]string{"name.1": "bar"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "matching against a value of standard variables succeeds",
|
|
|
|
match: MatchVarsRE{"{http.request.method}": &MatchRegexp{Pattern: "^G.[tT]$"}},
|
|
|
|
input: VarsMiddleware{},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2020-02-20 20:55:47 +03:00
|
|
|
desc: "matching against value of var set by the VarsMiddleware and referenced by its placeholder succeeds",
|
2020-02-08 22:26:31 +03:00
|
|
|
match: MatchVarsRE{"{http.vars.Var1}": &MatchRegexp{Pattern: "[vV]ar[0-9]"}},
|
|
|
|
input: VarsMiddleware{"Var1": "var1Value"},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
tc := tc // capture range value
|
|
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
// compile the regexp and validate its name
|
|
|
|
err := tc.match.Provision(caddy.Context{})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test %d %v: Provisioning: %v", i, tc.match, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = tc.match.Validate()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test %d %v: Validating: %v", i, tc.match, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up the fake request and its Replacer
|
|
|
|
req := &http.Request{URL: new(url.URL), Method: http.MethodGet}
|
|
|
|
repl := caddy.NewReplacer()
|
|
|
|
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
|
|
|
ctx = context.WithValue(ctx, VarsCtxKey, make(map[string]interface{}))
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
|
|
|
|
addHTTPVarsToReplacer(repl, req, httptest.NewRecorder())
|
|
|
|
|
|
|
|
tc.input.ServeHTTP(httptest.NewRecorder(), req, emptyHandler)
|
|
|
|
|
|
|
|
actual := tc.match.Match(req)
|
|
|
|
if actual != tc.expect {
|
|
|
|
t.Errorf("Test %d [%v]: Expected %t, got %t for input '%s'",
|
|
|
|
i, tc.match, tc.expect, actual, tc.input)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, expectVal := range tc.expectRepl {
|
|
|
|
placeholder := fmt.Sprintf("{http.regexp.%s}", key)
|
|
|
|
actualVal := repl.ReplaceAll(placeholder, "<empty>")
|
|
|
|
if actualVal != expectVal {
|
|
|
|
t.Errorf("Test %d [%v]: Expected placeholder {http.regexp.%s} to be '%s' but got '%s'",
|
|
|
|
i, tc.match, key, expectVal, actualVal)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:58:29 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-02 23:26:28 +03:00
|
|
|
func BenchmarkLargeHostMatcher(b *testing.B) {
|
|
|
|
// this benchmark simulates a large host matcher (thousands of entries) where each
|
|
|
|
// value is an exact hostname (not a placeholder or wildcard) - compare the results
|
|
|
|
// of this with and without the binary search (comment out the various fast path
|
|
|
|
// sections in Match) to conduct experiments
|
|
|
|
|
|
|
|
const n = 10000
|
|
|
|
lastHost := fmt.Sprintf("%d.example.com", n-1)
|
|
|
|
req := &http.Request{Host: lastHost}
|
|
|
|
repl := caddy.NewReplacer()
|
|
|
|
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
|
|
|
|
matcher := make(MatchHost, n)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
matcher[i] = fmt.Sprintf("%d.example.com", i)
|
|
|
|
}
|
|
|
|
err := matcher.Provision(caddy.Context{})
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
matcher.Match(req)
|
|
|
|
}
|
|
|
|
}
|
2020-04-01 19:58:29 +03:00
|
|
|
|
2019-10-14 20:29:36 +03:00
|
|
|
func BenchmarkHostMatcherWithoutPlaceholder(b *testing.B) {
|
|
|
|
req := &http.Request{Host: "localhost"}
|
|
|
|
repl := caddy.NewReplacer()
|
|
|
|
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
|
|
|
|
match := MatchHost{"localhost"}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
match.Match(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHostMatcherWithPlaceholder(b *testing.B) {
|
|
|
|
err := os.Setenv("GO_BENCHMARK_DOMAIN", "localhost")
|
|
|
|
if err != nil {
|
|
|
|
b.Errorf("error while setting up environment: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &http.Request{Host: "localhost"}
|
|
|
|
repl := caddy.NewReplacer()
|
|
|
|
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
match := MatchHost{"{env.GO_BENCHMARK_DOMAIN}"}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
match.Match(req)
|
|
|
|
}
|
|
|
|
}
|