2019-06-30 17:07:58 -05: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-10 22:07:02 -05:00
|
|
|
package caddyhttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2019-10-14 12:29:36 -05:00
|
|
|
"os"
|
2019-05-10 22:07:02 -05:00
|
|
|
"testing"
|
2019-05-20 11:59:20 -05:00
|
|
|
|
2019-07-02 13:37:06 -05:00
|
|
|
"github.com/caddyserver/caddy/v2"
|
2019-05-10 22:07:02 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHostMatcher(t *testing.T) {
|
2019-10-14 12:29:36 -05:00
|
|
|
err := os.Setenv("GO_BENCHMARK_DOMAIN", "localhost")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error while setting up environment: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-10 22:07:02 -05:00
|
|
|
for i, tc := range []struct {
|
2019-05-22 13:32:36 -05:00
|
|
|
match MatchHost
|
2019-05-10 22:07:02 -05:00
|
|
|
input string
|
|
|
|
expect bool
|
|
|
|
}{
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHost{},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "example.com",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHost{"example.com"},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "example.com",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHost{"example.com"},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "foo.example.com",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHost{"example.com"},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "EXAMPLE.COM",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHost{"foo.example.com"},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "foo.example.com",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHost{"foo.example.com"},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "bar.example.com",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHost{"*.example.com"},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "example.com",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHost{"*.example.com"},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "foo.example.com",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHost{"*.example.com"},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "foo.bar.example.com",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHost{"*.example.com", "example.net"},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "example.net",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHost{"example.net", "*.example.com"},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "foo.example.com",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHost{"*.example.net", "*.*.example.com"},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "foo.bar.example.com",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHost{"*.example.net", "sub.*.example.com"},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "sub.foo.example.com",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHost{"*.example.net", "sub.*.example.com"},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "sub.foo.example.net",
|
|
|
|
expect: false,
|
|
|
|
},
|
2019-06-20 21:24:46 -05:00
|
|
|
{
|
|
|
|
match: MatchHost{"example.com"},
|
|
|
|
input: "example.com:5555",
|
|
|
|
expect: true,
|
|
|
|
},
|
2019-10-14 12:29:36 -05:00
|
|
|
{
|
|
|
|
match: MatchHost{"{env.GO_BENCHMARK_DOMAIN}"},
|
|
|
|
input: "localhost",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match: MatchHost{"{env.GO_NONEXISTENT}"},
|
|
|
|
input: "localhost",
|
|
|
|
expect: false,
|
|
|
|
},
|
2019-05-10 22:07:02 -05:00
|
|
|
} {
|
|
|
|
req := &http.Request{Host: tc.input}
|
2019-10-14 12:29:36 -05:00
|
|
|
repl := caddy.NewReplacer()
|
|
|
|
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
|
2019-05-10 22:07:02 -05: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 13:32:36 -05:00
|
|
|
match MatchPath
|
2019-05-10 22:07:02 -05:00
|
|
|
input string
|
|
|
|
expect bool
|
|
|
|
}{
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchPath{},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "/",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchPath{"/"},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "/",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchPath{"/foo/bar"},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "/",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchPath{"/foo/bar"},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "/foo/bar",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchPath{"/foo/bar/"},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "/foo/bar",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchPath{"/foo/bar/", "/other"},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "/other/",
|
|
|
|
expect: true,
|
|
|
|
},
|
2019-05-20 11:59:20 -05:00
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchPath{"*.ext"},
|
2019-05-20 11:59:20 -05:00
|
|
|
input: "foo.ext",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchPath{"*.ext"},
|
2019-05-20 11:59:20 -05:00
|
|
|
input: "/foo/bar.ext",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchPath{"/foo/*/baz"},
|
2019-05-20 11:59:20 -05:00
|
|
|
input: "/foo/bar/baz",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchPath{"/foo/*/baz/bam"},
|
2019-05-20 11:59:20 -05:00
|
|
|
input: "/foo/bar/bam",
|
|
|
|
expect: false,
|
|
|
|
},
|
2019-05-10 22:07:02 -05:00
|
|
|
} {
|
|
|
|
req := &http.Request{URL: &url.URL{Path: tc.input}}
|
|
|
|
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 TestPathREMatcher(t *testing.T) {
|
|
|
|
for i, tc := range []struct {
|
2019-05-22 13:32:36 -05:00
|
|
|
match MatchPathRE
|
2019-05-10 22:07:02 -05:00
|
|
|
input string
|
|
|
|
expect bool
|
|
|
|
expectRepl map[string]string
|
|
|
|
}{
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchPathRE{},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "/",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 14:13:39 -05:00
|
|
|
match: MatchPathRE{MatchRegexp{Pattern: "/"}},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "/",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 14:13:39 -05:00
|
|
|
match: MatchPathRE{MatchRegexp{Pattern: "/foo"}},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "/foo",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 14:13:39 -05:00
|
|
|
match: MatchPathRE{MatchRegexp{Pattern: "/foo"}},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "/foo/",
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 14:13:39 -05:00
|
|
|
match: MatchPathRE{MatchRegexp{Pattern: "/bar"}},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "/foo/",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 14:13:39 -05:00
|
|
|
match: MatchPathRE{MatchRegexp{Pattern: "^/bar"}},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "/foo/bar",
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 14:13:39 -05:00
|
|
|
match: MatchPathRE{MatchRegexp{Pattern: "^/foo/(.*)/baz$", Name: "name"}},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "/foo/bar/baz",
|
|
|
|
expect: true,
|
|
|
|
expectRepl: map[string]string{"name.1": "bar"},
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 14:13:39 -05:00
|
|
|
match: MatchPathRE{MatchRegexp{Pattern: "^/foo/(?P<myparam>.*)/baz$", Name: "name"}},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: "/foo/bar/baz",
|
|
|
|
expect: true,
|
|
|
|
expectRepl: map[string]string{"name.myparam": "bar"},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
// compile the regexp and validate its name
|
2019-06-21 15:36:26 -05:00
|
|
|
err := tc.match.Provision(caddy.Context{})
|
2019-05-10 22:07:02 -05: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 12:58:28 -05:00
|
|
|
repl := caddy.NewReplacer()
|
|
|
|
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
2019-05-10 22:07:02 -05:00
|
|
|
req = req.WithContext(ctx)
|
2019-05-21 00:48:43 -05:00
|
|
|
addHTTPVarsToReplacer(repl, req, httptest.NewRecorder())
|
2019-05-10 22:07:02 -05: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-05-21 14:10:14 -05:00
|
|
|
placeholder := fmt.Sprintf("{http.matchers.path_regexp.%s}", key)
|
2019-05-20 11:59:20 -05:00
|
|
|
actualVal := repl.ReplaceAll(placeholder, "<empty>")
|
2019-05-10 22:07:02 -05:00
|
|
|
if actualVal != expectVal {
|
2019-05-21 14:10:14 -05:00
|
|
|
t.Errorf("Test %d [%v]: Expected placeholder {http.matchers.path_regexp.%s} to be '%s' but got '%s'",
|
2019-05-10 22:07:02 -05:00
|
|
|
i, tc.match.Pattern, key, expectVal, actualVal)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeaderMatcher(t *testing.T) {
|
|
|
|
for i, tc := range []struct {
|
2019-05-22 13:32:36 -05:00
|
|
|
match MatchHeader
|
2019-05-10 22:07:02 -05:00
|
|
|
input http.Header // make sure these are canonical cased (std lib will do that in a real request)
|
|
|
|
expect bool
|
|
|
|
}{
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHeader{"Field": []string{"foo"}},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: http.Header{"Field": []string{"foo"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHeader{"Field": []string{"foo", "bar"}},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: http.Header{"Field": []string{"bar"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHeader{"Field": []string{"foo", "bar"}},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: http.Header{"Alakazam": []string{"kapow"}},
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHeader{"Field": []string{"foo", "bar"}},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: http.Header{"Field": []string{"kapow"}},
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHeader{"Field": []string{"foo", "bar"}},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: http.Header{"Field": []string{"kapow", "foo"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHeader{"Field1": []string{"foo"}, "Field2": []string{"bar"}},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: http.Header{"Field1": []string{"foo"}, "Field2": []string{"bar"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHeader{"field1": []string{"foo"}, "field2": []string{"bar"}},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: http.Header{"Field1": []string{"foo"}, "Field2": []string{"bar"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 13:32:36 -05:00
|
|
|
match: MatchHeader{"field1": []string{"foo"}, "field2": []string{"bar"}},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: http.Header{"Field1": []string{"foo"}, "Field2": []string{"kapow"}},
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
req := &http.Request{Header: tc.input}
|
|
|
|
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 TestHeaderREMatcher(t *testing.T) {
|
|
|
|
for i, tc := range []struct {
|
2019-05-22 13:32:36 -05:00
|
|
|
match MatchHeaderRE
|
2019-05-10 22:07:02 -05:00
|
|
|
input http.Header // make sure these are canonical cased (std lib will do that in a real request)
|
|
|
|
expect bool
|
|
|
|
expectRepl map[string]string
|
|
|
|
}{
|
|
|
|
{
|
2019-05-22 14:13:39 -05:00
|
|
|
match: MatchHeaderRE{"Field": &MatchRegexp{Pattern: "foo"}},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: http.Header{"Field": []string{"foo"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 14:13:39 -05:00
|
|
|
match: MatchHeaderRE{"Field": &MatchRegexp{Pattern: "$foo^"}},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: http.Header{"Field": []string{"foobar"}},
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 14:13:39 -05:00
|
|
|
match: MatchHeaderRE{"Field": &MatchRegexp{Pattern: "^foo(.*)$", Name: "name"}},
|
2019-05-10 22:07:02 -05:00
|
|
|
input: http.Header{"Field": []string{"foobar"}},
|
|
|
|
expect: true,
|
|
|
|
expectRepl: map[string]string{"name.1": "bar"},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
// compile the regexp and validate its name
|
2019-06-21 15:36:26 -05:00
|
|
|
err := tc.match.Provision(caddy.Context{})
|
2019-05-10 22:07:02 -05: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{Header: tc.input, URL: new(url.URL)}
|
2019-06-14 12:58:28 -05:00
|
|
|
repl := caddy.NewReplacer()
|
|
|
|
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
2019-05-10 22:07:02 -05:00
|
|
|
req = req.WithContext(ctx)
|
2019-05-21 00:48:43 -05:00
|
|
|
addHTTPVarsToReplacer(repl, req, httptest.NewRecorder())
|
2019-05-10 22:07:02 -05: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-05-21 14:10:14 -05:00
|
|
|
placeholder := fmt.Sprintf("{http.matchers.header_regexp.%s}", key)
|
2019-05-20 11:59:20 -05:00
|
|
|
actualVal := repl.ReplaceAll(placeholder, "<empty>")
|
2019-05-10 22:07:02 -05:00
|
|
|
if actualVal != expectVal {
|
2019-05-21 14:10:14 -05:00
|
|
|
t.Errorf("Test %d [%v]: Expected placeholder {http.matchers.header_regexp.%s} to be '%s' but got '%s'",
|
2019-05-10 22:07:02 -05:00
|
|
|
i, tc.match, key, expectVal, actualVal)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 19:53:08 -05:00
|
|
|
|
|
|
|
func TestResponseMatcher(t *testing.T) {
|
|
|
|
for i, tc := range []struct {
|
|
|
|
require ResponseMatcher
|
|
|
|
status int
|
|
|
|
hdr http.Header // make sure these are canonical cased (std lib will do that in a real request)
|
|
|
|
expect bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
require: ResponseMatcher{},
|
|
|
|
status: 200,
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
require: ResponseMatcher{
|
|
|
|
StatusCode: []int{200},
|
|
|
|
},
|
|
|
|
status: 200,
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
require: ResponseMatcher{
|
|
|
|
StatusCode: []int{2},
|
|
|
|
},
|
|
|
|
status: 200,
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
require: ResponseMatcher{
|
|
|
|
StatusCode: []int{201},
|
|
|
|
},
|
|
|
|
status: 200,
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
require: ResponseMatcher{
|
|
|
|
StatusCode: []int{2},
|
|
|
|
},
|
|
|
|
status: 301,
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
require: ResponseMatcher{
|
|
|
|
StatusCode: []int{3},
|
|
|
|
},
|
|
|
|
status: 301,
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
require: ResponseMatcher{
|
|
|
|
StatusCode: []int{3},
|
|
|
|
},
|
|
|
|
status: 399,
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
require: ResponseMatcher{
|
|
|
|
StatusCode: []int{3},
|
|
|
|
},
|
|
|
|
status: 400,
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
require: ResponseMatcher{
|
|
|
|
StatusCode: []int{3, 4},
|
|
|
|
},
|
|
|
|
status: 400,
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
require: ResponseMatcher{
|
|
|
|
StatusCode: []int{3, 401},
|
|
|
|
},
|
|
|
|
status: 401,
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
require: ResponseMatcher{
|
|
|
|
Headers: http.Header{
|
|
|
|
"Foo": []string{"bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
hdr: http.Header{"Foo": []string{"bar"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
require: ResponseMatcher{
|
|
|
|
Headers: http.Header{
|
|
|
|
"Foo2": []string{"bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
hdr: http.Header{"Foo": []string{"bar"}},
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
require: ResponseMatcher{
|
|
|
|
Headers: http.Header{
|
|
|
|
"Foo": []string{"bar", "baz"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
hdr: http.Header{"Foo": []string{"baz"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
require: ResponseMatcher{
|
|
|
|
Headers: http.Header{
|
|
|
|
"Foo": []string{"bar"},
|
|
|
|
"Foo2": []string{"baz"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
hdr: http.Header{"Foo": []string{"baz"}},
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
require: ResponseMatcher{
|
|
|
|
Headers: http.Header{
|
|
|
|
"Foo": []string{"bar"},
|
|
|
|
"Foo2": []string{"baz"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
hdr: http.Header{"Foo": []string{"bar"}, "Foo2": []string{"baz"}},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
actual := tc.require.Match(tc.status, tc.hdr)
|
|
|
|
if actual != tc.expect {
|
|
|
|
t.Errorf("Test %d %v: Expected %t, got %t for HTTP %d %v", i, tc.require, tc.expect, actual, tc.status, tc.hdr)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-14 12:29:36 -05: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)
|
|
|
|
}
|
|
|
|
}
|