2019-05-10 22:07:02 -05:00
|
|
|
package caddyhttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
2019-05-20 11:59:20 -05:00
|
|
|
|
|
|
|
"bitbucket.org/lightcodelabs/caddy2"
|
2019-05-10 22:07:02 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHostMatcher(t *testing.T) {
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
req := &http.Request{Host: 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 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
|
|
|
|
err := tc.match.Provision()
|
|
|
|
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-05-21 00:48:43 -05:00
|
|
|
repl := caddy2.NewReplacer()
|
2019-05-20 11:59:20 -05:00
|
|
|
ctx := context.WithValue(req.Context(), caddy2.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
|
|
|
|
err := tc.match.Provision()
|
|
|
|
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-05-21 00:48:43 -05:00
|
|
|
repl := caddy2.NewReplacer()
|
2019-05-20 11:59:20 -05:00
|
|
|
ctx := context.WithValue(req.Context(), caddy2.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|