|
|
|
|
@ -10,58 +10,28 @@ import (
@@ -10,58 +10,28 @@ import (
|
|
|
|
|
"testing" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// helper function to create a new request with a method and url
|
|
|
|
|
func newRequest(method, url string) *http.Request { |
|
|
|
|
req, err := http.NewRequest(method, url, nil) |
|
|
|
|
if err != nil { |
|
|
|
|
panic(err) |
|
|
|
|
} |
|
|
|
|
return req |
|
|
|
|
type routeTest struct { |
|
|
|
|
title string // title of the test
|
|
|
|
|
route *Route // the route being tested
|
|
|
|
|
request *http.Request // a request to test the route
|
|
|
|
|
vars map[string]string // the expected vars of the match
|
|
|
|
|
host string // the expected host of the match
|
|
|
|
|
path string // the expected path of the match
|
|
|
|
|
shouldMatch bool // whether the request is expected to match the route at all
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// helper function to create a new request with a method, url, and host header
|
|
|
|
|
func newRequestHost(method, url, host string) *http.Request { |
|
|
|
|
func TestHost(t *testing.T) { |
|
|
|
|
// newRequestHost a new request with a method, url, and host header
|
|
|
|
|
newRequestHost := func(method, url, host string) *http.Request { |
|
|
|
|
req, err := http.NewRequest(method, url, nil) |
|
|
|
|
if err != nil { |
|
|
|
|
panic(err) |
|
|
|
|
} |
|
|
|
|
req.Host = host |
|
|
|
|
return req |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// helper function to create a new request with a method, url, and headers
|
|
|
|
|
func newRequestHeaders(method, url string, headers map[string]string) *http.Request { |
|
|
|
|
req, err := http.NewRequest(method, url, nil) |
|
|
|
|
if err != nil { |
|
|
|
|
panic(err) |
|
|
|
|
} |
|
|
|
|
for k, v := range headers { |
|
|
|
|
req.Header.Add(k, v) |
|
|
|
|
} |
|
|
|
|
return req |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Tests for Route
|
|
|
|
|
func TestRoute(t *testing.T) { |
|
|
|
|
// match function for Custom tests
|
|
|
|
|
m := func(r *http.Request, m *RouteMatch) bool { |
|
|
|
|
if r.URL.Host == "aaa.bbb.ccc" { |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// the tests
|
|
|
|
|
tests := []struct { |
|
|
|
|
title string // title of the test
|
|
|
|
|
route *Route // the route being tested
|
|
|
|
|
request *http.Request // a request to test the route
|
|
|
|
|
vars map[string]string // the expected vars of the match
|
|
|
|
|
host string // the expected host of the match
|
|
|
|
|
path string // the expected path of the match
|
|
|
|
|
match bool // whether the request is expected to match the route at all
|
|
|
|
|
}{ |
|
|
|
|
// Host
|
|
|
|
|
tests := []routeTest{ |
|
|
|
|
{ |
|
|
|
|
title: "Host route match", |
|
|
|
|
route: new(Route).Host("aaa.bbb.ccc"), |
|
|
|
|
@ -69,7 +39,7 @@ func TestRoute(t *testing.T) {
@@ -69,7 +39,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "aaa.bbb.ccc", |
|
|
|
|
path: "", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Host route, wrong host in request URL", |
|
|
|
|
@ -78,7 +48,7 @@ func TestRoute(t *testing.T) {
@@ -78,7 +48,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "aaa.bbb.ccc", |
|
|
|
|
path: "", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Host route with port, match", |
|
|
|
|
@ -87,7 +57,7 @@ func TestRoute(t *testing.T) {
@@ -87,7 +57,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "aaa.bbb.ccc:1234", |
|
|
|
|
path: "", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Host route with port, wrong port in request URL", |
|
|
|
|
@ -96,7 +66,7 @@ func TestRoute(t *testing.T) {
@@ -96,7 +66,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "aaa.bbb.ccc:1234", |
|
|
|
|
path: "", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Host route, match with host in request header", |
|
|
|
|
@ -105,7 +75,7 @@ func TestRoute(t *testing.T) {
@@ -105,7 +75,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "aaa.bbb.ccc", |
|
|
|
|
path: "", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Host route, wrong host in request header", |
|
|
|
|
@ -114,7 +84,7 @@ func TestRoute(t *testing.T) {
@@ -114,7 +84,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "aaa.bbb.ccc", |
|
|
|
|
path: "", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
// BUG {new(Route).Host("aaa.bbb.ccc:1234"), newRequestHost("GET", "/111/222/333", "aaa.bbb.ccc:1234"), map[string]string{}, "aaa.bbb.ccc:1234", "", true},
|
|
|
|
|
{ |
|
|
|
|
@ -124,7 +94,7 @@ func TestRoute(t *testing.T) {
@@ -124,7 +94,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "aaa.bbb.ccc:1234", |
|
|
|
|
path: "", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Host route with pattern, match", |
|
|
|
|
@ -133,7 +103,7 @@ func TestRoute(t *testing.T) {
@@ -133,7 +103,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{"v1": "bbb"}, |
|
|
|
|
host: "aaa.bbb.ccc", |
|
|
|
|
path: "", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Host route with pattern, wrong host in request URL", |
|
|
|
|
@ -142,7 +112,7 @@ func TestRoute(t *testing.T) {
@@ -142,7 +112,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{"v1": "bbb"}, |
|
|
|
|
host: "aaa.bbb.ccc", |
|
|
|
|
path: "", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Host route with multiple patterns, match", |
|
|
|
|
@ -151,7 +121,7 @@ func TestRoute(t *testing.T) {
@@ -151,7 +121,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc"}, |
|
|
|
|
host: "aaa.bbb.ccc", |
|
|
|
|
path: "", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Host route with multiple patterns, wrong host in request URL", |
|
|
|
|
@ -160,10 +130,16 @@ func TestRoute(t *testing.T) {
@@ -160,10 +130,16 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc"}, |
|
|
|
|
host: "aaa.bbb.ccc", |
|
|
|
|
path: "", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
for _, test := range tests { |
|
|
|
|
testRoute(t, test) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Path
|
|
|
|
|
func TestPath(t *testing.T) { |
|
|
|
|
tests := []routeTest{ |
|
|
|
|
{ |
|
|
|
|
title: "Path route, match", |
|
|
|
|
route: new(Route).Path("/111/222/333"), |
|
|
|
|
@ -171,7 +147,7 @@ func TestRoute(t *testing.T) {
@@ -171,7 +147,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "", |
|
|
|
|
path: "/111/222/333", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Path route, wrong path in request in request URL", |
|
|
|
|
@ -180,7 +156,7 @@ func TestRoute(t *testing.T) {
@@ -180,7 +156,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "", |
|
|
|
|
path: "/111/222/333", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Path route with pattern, match", |
|
|
|
|
@ -189,7 +165,7 @@ func TestRoute(t *testing.T) {
@@ -189,7 +165,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{"v1": "222"}, |
|
|
|
|
host: "", |
|
|
|
|
path: "/111/222/333", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Path route with pattern, URL in request does not match", |
|
|
|
|
@ -198,7 +174,7 @@ func TestRoute(t *testing.T) {
@@ -198,7 +174,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{"v1": "222"}, |
|
|
|
|
host: "", |
|
|
|
|
path: "/111/222/333", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Path route with multiple patterns, match", |
|
|
|
|
@ -207,7 +183,7 @@ func TestRoute(t *testing.T) {
@@ -207,7 +183,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{"v1": "111", "v2": "222", "v3": "333"}, |
|
|
|
|
host: "", |
|
|
|
|
path: "/111/222/333", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Path route with multiple patterns, URL in request does not match", |
|
|
|
|
@ -216,10 +192,17 @@ func TestRoute(t *testing.T) {
@@ -216,10 +192,17 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{"v1": "111", "v2": "222", "v3": "333"}, |
|
|
|
|
host: "", |
|
|
|
|
path: "/111/222/333", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// PathPrefix
|
|
|
|
|
for _, test := range tests { |
|
|
|
|
testRoute(t, test) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestPathPrefix(t *testing.T) { |
|
|
|
|
tests := []routeTest{ |
|
|
|
|
{ |
|
|
|
|
title: "PathPrefix route, match", |
|
|
|
|
route: new(Route).PathPrefix("/111"), |
|
|
|
|
@ -227,7 +210,7 @@ func TestRoute(t *testing.T) {
@@ -227,7 +210,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "", |
|
|
|
|
path: "/111", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "PathPrefix route, URL prefix in request does not match", |
|
|
|
|
@ -236,7 +219,7 @@ func TestRoute(t *testing.T) {
@@ -236,7 +219,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "", |
|
|
|
|
path: "/111", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "PathPrefix route with pattern, match", |
|
|
|
|
@ -245,7 +228,7 @@ func TestRoute(t *testing.T) {
@@ -245,7 +228,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{"v1": "222"}, |
|
|
|
|
host: "", |
|
|
|
|
path: "/111/222", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "PathPrefix route with pattern, URL prefix in request does not match", |
|
|
|
|
@ -254,7 +237,7 @@ func TestRoute(t *testing.T) {
@@ -254,7 +237,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{"v1": "222"}, |
|
|
|
|
host: "", |
|
|
|
|
path: "/111/222", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "PathPrefix route with multiple patterns, match", |
|
|
|
|
@ -263,7 +246,7 @@ func TestRoute(t *testing.T) {
@@ -263,7 +246,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{"v1": "111", "v2": "222"}, |
|
|
|
|
host: "", |
|
|
|
|
path: "/111/222", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "PathPrefix route with multiple patterns, URL prefix in request does not match", |
|
|
|
|
@ -272,10 +255,17 @@ func TestRoute(t *testing.T) {
@@ -272,10 +255,17 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{"v1": "111", "v2": "222"}, |
|
|
|
|
host: "", |
|
|
|
|
path: "/111/222", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, test := range tests { |
|
|
|
|
testRoute(t, test) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Host + Path
|
|
|
|
|
func TestHostPath(t *testing.T) { |
|
|
|
|
tests := []routeTest{ |
|
|
|
|
{ |
|
|
|
|
title: "Host and Path route, match", |
|
|
|
|
route: new(Route).Host("aaa.bbb.ccc").Path("/111/222/333"), |
|
|
|
|
@ -283,7 +273,7 @@ func TestRoute(t *testing.T) {
@@ -283,7 +273,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "", |
|
|
|
|
path: "", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Host and Path route, wrong host in request URL", |
|
|
|
|
@ -292,7 +282,7 @@ func TestRoute(t *testing.T) {
@@ -292,7 +282,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "", |
|
|
|
|
path: "", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Host and Path route with pattern, match", |
|
|
|
|
@ -301,7 +291,7 @@ func TestRoute(t *testing.T) {
@@ -301,7 +291,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{"v1": "bbb", "v2": "222"}, |
|
|
|
|
host: "aaa.bbb.ccc", |
|
|
|
|
path: "/111/222/333", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Host and Path route with pattern, URL in request does not match", |
|
|
|
|
@ -310,7 +300,7 @@ func TestRoute(t *testing.T) {
@@ -310,7 +300,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{"v1": "bbb", "v2": "222"}, |
|
|
|
|
host: "aaa.bbb.ccc", |
|
|
|
|
path: "/111/222/333", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Host and Path route with multiple patterns, match", |
|
|
|
|
@ -319,7 +309,7 @@ func TestRoute(t *testing.T) {
@@ -319,7 +309,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc", "v4": "111", "v5": "222", "v6": "333"}, |
|
|
|
|
host: "aaa.bbb.ccc", |
|
|
|
|
path: "/111/222/333", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Host and Path route with multiple patterns, URL in request does not match", |
|
|
|
|
@ -328,10 +318,29 @@ func TestRoute(t *testing.T) {
@@ -328,10 +318,29 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc", "v4": "111", "v5": "222", "v6": "333"}, |
|
|
|
|
host: "aaa.bbb.ccc", |
|
|
|
|
path: "/111/222/333", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, test := range tests { |
|
|
|
|
testRoute(t, test) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Headers
|
|
|
|
|
func TestHeaders(t *testing.T) { |
|
|
|
|
// newRequestHeaders creates a new request with a method, url, and headers
|
|
|
|
|
newRequestHeaders := func(method, url string, headers map[string]string) *http.Request { |
|
|
|
|
req, err := http.NewRequest(method, url, nil) |
|
|
|
|
if err != nil { |
|
|
|
|
panic(err) |
|
|
|
|
} |
|
|
|
|
for k, v := range headers { |
|
|
|
|
req.Header.Add(k, v) |
|
|
|
|
} |
|
|
|
|
return req |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tests := []routeTest{ |
|
|
|
|
{ |
|
|
|
|
title: "Headers route, match", |
|
|
|
|
route: new(Route).Headers("foo", "bar", "baz", "ding"), |
|
|
|
|
@ -339,7 +348,7 @@ func TestRoute(t *testing.T) {
@@ -339,7 +348,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "", |
|
|
|
|
path: "", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Headers route, bad header values", |
|
|
|
|
@ -348,10 +357,18 @@ func TestRoute(t *testing.T) {
@@ -348,10 +357,18 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "", |
|
|
|
|
path: "", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, test := range tests { |
|
|
|
|
testRoute(t, test) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Methods
|
|
|
|
|
func TestMethods(t *testing.T) { |
|
|
|
|
tests := []routeTest{ |
|
|
|
|
{ |
|
|
|
|
title: "Methods route, match GET", |
|
|
|
|
route: new(Route).Methods("GET", "POST"), |
|
|
|
|
@ -359,7 +376,7 @@ func TestRoute(t *testing.T) {
@@ -359,7 +376,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "", |
|
|
|
|
path: "", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Methods route, match POST", |
|
|
|
|
@ -368,7 +385,7 @@ func TestRoute(t *testing.T) {
@@ -368,7 +385,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "", |
|
|
|
|
path: "", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Methods route, bad method", |
|
|
|
|
@ -377,10 +394,17 @@ func TestRoute(t *testing.T) {
@@ -377,10 +394,17 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "", |
|
|
|
|
path: "", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, test := range tests { |
|
|
|
|
testRoute(t, test) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Queries
|
|
|
|
|
func TestQueries(t *testing.T) { |
|
|
|
|
tests := []routeTest{ |
|
|
|
|
{ |
|
|
|
|
title: "Queries route, match", |
|
|
|
|
route: new(Route).Queries("foo", "bar", "baz", "ding"), |
|
|
|
|
@ -388,7 +412,7 @@ func TestRoute(t *testing.T) {
@@ -388,7 +412,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "", |
|
|
|
|
path: "", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Queries route, bad query", |
|
|
|
|
@ -397,9 +421,17 @@ func TestRoute(t *testing.T) {
@@ -397,9 +421,17 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "", |
|
|
|
|
path: "", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, test := range tests { |
|
|
|
|
testRoute(t, test) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestSchemes(t *testing.T) { |
|
|
|
|
tests := []routeTest{ |
|
|
|
|
// Schemes
|
|
|
|
|
{ |
|
|
|
|
title: "Schemes route, match https", |
|
|
|
|
@ -408,7 +440,7 @@ func TestRoute(t *testing.T) {
@@ -408,7 +440,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "", |
|
|
|
|
path: "", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Schemes route, match ftp", |
|
|
|
|
@ -417,7 +449,7 @@ func TestRoute(t *testing.T) {
@@ -417,7 +449,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "", |
|
|
|
|
path: "", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "Schemes route, bad scheme", |
|
|
|
|
@ -426,10 +458,23 @@ func TestRoute(t *testing.T) {
@@ -426,10 +458,23 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "", |
|
|
|
|
path: "", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
for _, test := range tests { |
|
|
|
|
testRoute(t, test) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Custom
|
|
|
|
|
func TestMatcherFunc(t *testing.T) { |
|
|
|
|
m := func(r *http.Request, m *RouteMatch) bool { |
|
|
|
|
if r.URL.Host == "aaa.bbb.ccc" { |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tests := []routeTest{ |
|
|
|
|
{ |
|
|
|
|
title: "MatchFunc route, match", |
|
|
|
|
route: new(Route).MatcherFunc(m), |
|
|
|
|
@ -437,7 +482,7 @@ func TestRoute(t *testing.T) {
@@ -437,7 +482,7 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "", |
|
|
|
|
path: "", |
|
|
|
|
match: true, |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
title: "MatchFunc route, non-match", |
|
|
|
|
@ -446,56 +491,57 @@ func TestRoute(t *testing.T) {
@@ -446,56 +491,57 @@ func TestRoute(t *testing.T) {
|
|
|
|
|
vars: map[string]string{}, |
|
|
|
|
host: "", |
|
|
|
|
path: "", |
|
|
|
|
match: false, |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for i, test := range tests { |
|
|
|
|
testRoute(t, fmt.Sprintf("%v: %s", i, test.title), test.match, test.route, test.request, test.vars, test.host, test.path, test.host+test.path) |
|
|
|
|
for _, test := range tests { |
|
|
|
|
testRoute(t, test) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestSubRouter(t *testing.T) { |
|
|
|
|
var route *Route |
|
|
|
|
var request *http.Request |
|
|
|
|
var vars map[string]string |
|
|
|
|
var host, path, url string |
|
|
|
|
|
|
|
|
|
subrouter := new(Route).Host("{v1:[a-z]+}.google.com").Subrouter() |
|
|
|
|
|
|
|
|
|
// Setup an id so we can see which test failed. :)
|
|
|
|
|
var idValue int |
|
|
|
|
id := func() int { |
|
|
|
|
idValue++ |
|
|
|
|
return idValue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
route = subrouter.Path("/{v2:[a-z]+}") |
|
|
|
|
request, _ = http.NewRequest("GET", "http://aaa.google.com/bbb", nil) |
|
|
|
|
vars = map[string]string{"v1": "aaa", "v2": "bbb"} |
|
|
|
|
host = "aaa.google.com" |
|
|
|
|
path = "/bbb" |
|
|
|
|
url = host + path |
|
|
|
|
testRoute(t, string(id()), true, route, request, vars, host, path, url) |
|
|
|
|
// Non-match for the same config.
|
|
|
|
|
request, _ = http.NewRequest("GET", "http://111.google.com/111", nil) |
|
|
|
|
testRoute(t, string(id()), false, route, request, vars, host, path, url) |
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
subrouter = new(Route).PathPrefix("/foo/{v1}").Subrouter() |
|
|
|
|
route = subrouter.Path("/baz/{v2}") |
|
|
|
|
request, _ = http.NewRequest("GET", "http://localhost/foo/bar/baz/ding", nil) |
|
|
|
|
vars = map[string]string{"v1": "bar", "v2": "ding"} |
|
|
|
|
host = "" |
|
|
|
|
path = "/foo/bar/baz/ding" |
|
|
|
|
url = host + path |
|
|
|
|
testRoute(t, string(id()), true, route, request, vars, host, path, url) |
|
|
|
|
// Non-match for the same config.
|
|
|
|
|
request, _ = http.NewRequest("GET", "http://localhost/foo/bar", nil) |
|
|
|
|
testRoute(t, string(id()), false, route, request, vars, host, path, url) |
|
|
|
|
subrouter1 := new(Route).Host("{v1:[a-z]+}.google.com").Subrouter() |
|
|
|
|
subrouter2 := new(Route).PathPrefix("/foo/{v1}").Subrouter() |
|
|
|
|
|
|
|
|
|
tests := []routeTest{ |
|
|
|
|
{ |
|
|
|
|
route: subrouter1.Path("/{v2:[a-z]+}"), |
|
|
|
|
request: newRequest("GET", "http://aaa.google.com/bbb"), |
|
|
|
|
vars: map[string]string{"v1": "aaa", "v2": "bbb"}, |
|
|
|
|
host: "aaa.google.com", |
|
|
|
|
path: "/bbb", |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
route: subrouter1.Path("/{v2:[a-z]+}"), |
|
|
|
|
request: newRequest("GET", "http://111.google.com/111"), |
|
|
|
|
vars: map[string]string{"v1": "aaa", "v2": "bbb"}, |
|
|
|
|
host: "aaa.google.com", |
|
|
|
|
path: "/bbb", |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
route: subrouter2.Path("/baz/{v2}"), |
|
|
|
|
request: newRequest("GET", "http://localhost/foo/bar/baz/ding"), |
|
|
|
|
vars: map[string]string{"v1": "bar", "v2": "ding"}, |
|
|
|
|
host: "", |
|
|
|
|
path: "/foo/bar/baz/ding", |
|
|
|
|
shouldMatch: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
route: subrouter2.Path("/baz/{v2}"), |
|
|
|
|
request: newRequest("GET", "http://localhost/foo/bar"), |
|
|
|
|
vars: map[string]string{"v1": "bar", "v2": "ding"}, |
|
|
|
|
host: "", |
|
|
|
|
path: "/foo/bar/baz/ding", |
|
|
|
|
shouldMatch: false, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, test := range tests { |
|
|
|
|
testRoute(t, test) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestNamedRoutes(t *testing.T) { |
|
|
|
|
@ -521,6 +567,30 @@ func TestNamedRoutes(t *testing.T) {
@@ -521,6 +567,30 @@ func TestNamedRoutes(t *testing.T) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestStrictSlash(t *testing.T) { |
|
|
|
|
var r *Router |
|
|
|
|
var req *http.Request |
|
|
|
|
var route *Route |
|
|
|
|
var match *RouteMatch |
|
|
|
|
var matched bool |
|
|
|
|
|
|
|
|
|
// StrictSlash should be ignored for path prefix.
|
|
|
|
|
// So we register a route ending in slash but it doesn't attempt to add
|
|
|
|
|
// the slash for a path not ending in slash.
|
|
|
|
|
r = NewRouter() |
|
|
|
|
r.StrictSlash(true) |
|
|
|
|
route = r.NewRoute().PathPrefix("/static/") |
|
|
|
|
req, _ = http.NewRequest("GET", "http://localhost/static/logo.png", nil) |
|
|
|
|
match = new(RouteMatch) |
|
|
|
|
matched = r.Match(req, match) |
|
|
|
|
if !matched { |
|
|
|
|
t.Errorf("Should match request %q -- %v", req.URL.Path, getRouteTemplate(route)) |
|
|
|
|
} |
|
|
|
|
if match.Handler != nil { |
|
|
|
|
t.Errorf("Should not redirect") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
// Helpers
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
@ -538,8 +608,15 @@ func getRouteTemplate(route *Route) string {
@@ -538,8 +608,15 @@ func getRouteTemplate(route *Route) string {
|
|
|
|
|
return fmt.Sprintf("Host: %v, Path: %v", host, path) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func testRoute(t *testing.T, id string, shouldMatch bool, route *Route, |
|
|
|
|
request *http.Request, vars map[string]string, host, path, url string) { |
|
|
|
|
func testRoute(t *testing.T, test routeTest) { |
|
|
|
|
request := test.request |
|
|
|
|
route := test.route |
|
|
|
|
vars := test.vars |
|
|
|
|
shouldMatch := test.shouldMatch |
|
|
|
|
host := test.host |
|
|
|
|
path := test.path |
|
|
|
|
url := test.host + test.path |
|
|
|
|
|
|
|
|
|
var match RouteMatch |
|
|
|
|
ok := route.Match(request, &match) |
|
|
|
|
if ok != shouldMatch { |
|
|
|
|
@ -547,62 +624,39 @@ func testRoute(t *testing.T, id string, shouldMatch bool, route *Route,
@@ -547,62 +624,39 @@ func testRoute(t *testing.T, id string, shouldMatch bool, route *Route,
|
|
|
|
|
if !shouldMatch { |
|
|
|
|
msg = "Should not match" |
|
|
|
|
} |
|
|
|
|
t.Errorf("(%v) %v:\nRoute: %#v\nRequest: %#v\nVars: %v\n", id, msg, route, request, vars) |
|
|
|
|
t.Errorf("(%v) %v:\nRoute: %#v\nRequest: %#v\nVars: %v\n", test.title, msg, route, request, vars) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
if shouldMatch { |
|
|
|
|
if vars != nil && !stringMapEqual(vars, match.Vars) { |
|
|
|
|
t.Errorf("(%v) Vars not equal: expected %v, got %v", id, vars, match.Vars) |
|
|
|
|
if test.vars != nil && !stringMapEqual(test.vars, match.Vars) { |
|
|
|
|
t.Errorf("(%v) Vars not equal: expected %v, got %v", test.title, vars, match.Vars) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
if host != "" { |
|
|
|
|
u, _ := route.URLHost(mapToPairs(match.Vars)...) |
|
|
|
|
u, _ := test.route.URLHost(mapToPairs(match.Vars)...) |
|
|
|
|
if host != u.Host { |
|
|
|
|
t.Errorf("(%v) URLHost not equal: expected %v, got %v -- %v", id, host, u.Host, getRouteTemplate(route)) |
|
|
|
|
t.Errorf("(%v) URLHost not equal: expected %v, got %v -- %v", test.title, host, u.Host, getRouteTemplate(route)) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if path != "" { |
|
|
|
|
u, _ := route.URLPath(mapToPairs(match.Vars)...) |
|
|
|
|
if path != u.Path { |
|
|
|
|
t.Errorf("(%v) URLPath not equal: expected %v, got %v -- %v", id, path, u.Path, getRouteTemplate(route)) |
|
|
|
|
t.Errorf("(%v) URLPath not equal: expected %v, got %v -- %v", test.title, path, u.Path, getRouteTemplate(route)) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if url != "" { |
|
|
|
|
u, _ := route.URL(mapToPairs(match.Vars)...) |
|
|
|
|
if url != u.Host+u.Path { |
|
|
|
|
t.Errorf("(%v) URL not equal: expected %v, got %v -- %v", id, url, u.Host+u.Path, getRouteTemplate(route)) |
|
|
|
|
t.Errorf("(%v) URL not equal: expected %v, got %v -- %v", test.title, url, u.Host+u.Path, getRouteTemplate(route)) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestStrictSlash(t *testing.T) { |
|
|
|
|
var r *Router |
|
|
|
|
var req *http.Request |
|
|
|
|
var route *Route |
|
|
|
|
var match *RouteMatch |
|
|
|
|
var matched bool |
|
|
|
|
|
|
|
|
|
// StrictSlash should be ignored for path prefix.
|
|
|
|
|
// So we register a route ending in slash but it doesn't attempt to add
|
|
|
|
|
// the slash for a path not ending in slash.
|
|
|
|
|
r = NewRouter() |
|
|
|
|
r.StrictSlash(true) |
|
|
|
|
route = r.NewRoute().PathPrefix("/static/") |
|
|
|
|
req, _ = http.NewRequest("GET", "http://localhost/static/logo.png", nil) |
|
|
|
|
match = new(RouteMatch) |
|
|
|
|
matched = r.Match(req, match) |
|
|
|
|
if !matched { |
|
|
|
|
t.Errorf("Should match request %q -- %v", req.URL.Path, getRouteTemplate(route)) |
|
|
|
|
} |
|
|
|
|
if match.Handler != nil { |
|
|
|
|
t.Errorf("Should not redirect") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// mapToPairs converts a string map to a slice of string pairs
|
|
|
|
|
func mapToPairs(m map[string]string) []string { |
|
|
|
|
var i int |
|
|
|
|
p := make([]string, len(m)*2) |
|
|
|
|
@ -614,6 +668,7 @@ func mapToPairs(m map[string]string) []string {
@@ -614,6 +668,7 @@ func mapToPairs(m map[string]string) []string {
|
|
|
|
|
return p |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// stringMapEqual checks the equality of two string maps
|
|
|
|
|
func stringMapEqual(m1, m2 map[string]string) bool { |
|
|
|
|
nil1 := m1 == nil |
|
|
|
|
nil2 := m2 == nil |
|
|
|
|
@ -627,3 +682,12 @@ func stringMapEqual(m1, m2 map[string]string) bool {
@@ -627,3 +682,12 @@ func stringMapEqual(m1, m2 map[string]string) bool {
|
|
|
|
|
} |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// newRequest is a helper function to create a new request with a method and url
|
|
|
|
|
func newRequest(method, url string) *http.Request { |
|
|
|
|
req, err := http.NewRequest(method, url, nil) |
|
|
|
|
if err != nil { |
|
|
|
|
panic(err) |
|
|
|
|
} |
|
|
|
|
return req |
|
|
|
|
} |
|
|
|
|
|