Clean up some naming in mux_test.go
This commit is contained in:
680
mux_test.go
680
mux_test.go
@@ -31,8 +31,8 @@ type routeTest struct {
|
|||||||
vars map[string]string // the expected vars of the match
|
vars map[string]string // the expected vars of the match
|
||||||
host string // the expected host of the match
|
host string // the expected host of the match
|
||||||
path string // the expected path of the match
|
path string // the expected path of the match
|
||||||
path_template string // the expected path template to match
|
pathTemplate string // the expected path template to match
|
||||||
host_template string // the expected host template to match
|
hostTemplate string // the expected host template to match
|
||||||
shouldMatch bool // whether the request is expected to match the route at all
|
shouldMatch bool // whether the request is expected to match the route at all
|
||||||
shouldRedirect bool // whether the request should result in a redirect
|
shouldRedirect bool // whether the request should result in a redirect
|
||||||
}
|
}
|
||||||
@@ -114,124 +114,124 @@ func TestHost(t *testing.T) {
|
|||||||
shouldMatch: false,
|
shouldMatch: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Host route with pattern, match",
|
title: "Host route with pattern, match",
|
||||||
route: new(Route).Host("aaa.{v1:[a-z]{3}}.ccc"),
|
route: new(Route).Host("aaa.{v1:[a-z]{3}}.ccc"),
|
||||||
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
|
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
|
||||||
vars: map[string]string{"v1": "bbb"},
|
vars: map[string]string{"v1": "bbb"},
|
||||||
host: "aaa.bbb.ccc",
|
host: "aaa.bbb.ccc",
|
||||||
path: "",
|
path: "",
|
||||||
host_template: `aaa.{v1:[a-z]{3}}.ccc`,
|
hostTemplate: `aaa.{v1:[a-z]{3}}.ccc`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Host route with pattern, additional capturing group, match",
|
title: "Host route with pattern, additional capturing group, match",
|
||||||
route: new(Route).Host("aaa.{v1:[a-z]{2}(b|c)}.ccc"),
|
route: new(Route).Host("aaa.{v1:[a-z]{2}(b|c)}.ccc"),
|
||||||
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
|
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
|
||||||
vars: map[string]string{"v1": "bbb"},
|
vars: map[string]string{"v1": "bbb"},
|
||||||
host: "aaa.bbb.ccc",
|
host: "aaa.bbb.ccc",
|
||||||
path: "",
|
path: "",
|
||||||
host_template: `aaa.{v1:[a-z]{2}(b|c)}.ccc`,
|
hostTemplate: `aaa.{v1:[a-z]{2}(b|c)}.ccc`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Host route with pattern, wrong host in request URL",
|
title: "Host route with pattern, wrong host in request URL",
|
||||||
route: new(Route).Host("aaa.{v1:[a-z]{3}}.ccc"),
|
route: new(Route).Host("aaa.{v1:[a-z]{3}}.ccc"),
|
||||||
request: newRequest("GET", "http://aaa.222.ccc/111/222/333"),
|
request: newRequest("GET", "http://aaa.222.ccc/111/222/333"),
|
||||||
vars: map[string]string{"v1": "bbb"},
|
vars: map[string]string{"v1": "bbb"},
|
||||||
host: "aaa.bbb.ccc",
|
host: "aaa.bbb.ccc",
|
||||||
path: "",
|
path: "",
|
||||||
host_template: `aaa.{v1:[a-z]{3}}.ccc`,
|
hostTemplate: `aaa.{v1:[a-z]{3}}.ccc`,
|
||||||
shouldMatch: false,
|
shouldMatch: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Host route with multiple patterns, match",
|
title: "Host route with multiple patterns, match",
|
||||||
route: new(Route).Host("{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}"),
|
route: new(Route).Host("{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}"),
|
||||||
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
|
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
|
||||||
vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc"},
|
vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc"},
|
||||||
host: "aaa.bbb.ccc",
|
host: "aaa.bbb.ccc",
|
||||||
path: "",
|
path: "",
|
||||||
host_template: `{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}`,
|
hostTemplate: `{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Host route with multiple patterns, wrong host in request URL",
|
title: "Host route with multiple patterns, wrong host in request URL",
|
||||||
route: new(Route).Host("{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}"),
|
route: new(Route).Host("{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}"),
|
||||||
request: newRequest("GET", "http://aaa.222.ccc/111/222/333"),
|
request: newRequest("GET", "http://aaa.222.ccc/111/222/333"),
|
||||||
vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc"},
|
vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc"},
|
||||||
host: "aaa.bbb.ccc",
|
host: "aaa.bbb.ccc",
|
||||||
path: "",
|
path: "",
|
||||||
host_template: `{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}`,
|
hostTemplate: `{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}`,
|
||||||
shouldMatch: false,
|
shouldMatch: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Host route with hyphenated name and pattern, match",
|
title: "Host route with hyphenated name and pattern, match",
|
||||||
route: new(Route).Host("aaa.{v-1:[a-z]{3}}.ccc"),
|
route: new(Route).Host("aaa.{v-1:[a-z]{3}}.ccc"),
|
||||||
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
|
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
|
||||||
vars: map[string]string{"v-1": "bbb"},
|
vars: map[string]string{"v-1": "bbb"},
|
||||||
host: "aaa.bbb.ccc",
|
host: "aaa.bbb.ccc",
|
||||||
path: "",
|
path: "",
|
||||||
host_template: `aaa.{v-1:[a-z]{3}}.ccc`,
|
hostTemplate: `aaa.{v-1:[a-z]{3}}.ccc`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Host route with hyphenated name and pattern, additional capturing group, match",
|
title: "Host route with hyphenated name and pattern, additional capturing group, match",
|
||||||
route: new(Route).Host("aaa.{v-1:[a-z]{2}(b|c)}.ccc"),
|
route: new(Route).Host("aaa.{v-1:[a-z]{2}(b|c)}.ccc"),
|
||||||
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
|
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
|
||||||
vars: map[string]string{"v-1": "bbb"},
|
vars: map[string]string{"v-1": "bbb"},
|
||||||
host: "aaa.bbb.ccc",
|
host: "aaa.bbb.ccc",
|
||||||
path: "",
|
path: "",
|
||||||
host_template: `aaa.{v-1:[a-z]{2}(b|c)}.ccc`,
|
hostTemplate: `aaa.{v-1:[a-z]{2}(b|c)}.ccc`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Host route with multiple hyphenated names and patterns, match",
|
title: "Host route with multiple hyphenated names and patterns, match",
|
||||||
route: new(Route).Host("{v-1:[a-z]{3}}.{v-2:[a-z]{3}}.{v-3:[a-z]{3}}"),
|
route: new(Route).Host("{v-1:[a-z]{3}}.{v-2:[a-z]{3}}.{v-3:[a-z]{3}}"),
|
||||||
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
|
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
|
||||||
vars: map[string]string{"v-1": "aaa", "v-2": "bbb", "v-3": "ccc"},
|
vars: map[string]string{"v-1": "aaa", "v-2": "bbb", "v-3": "ccc"},
|
||||||
host: "aaa.bbb.ccc",
|
host: "aaa.bbb.ccc",
|
||||||
path: "",
|
path: "",
|
||||||
host_template: `{v-1:[a-z]{3}}.{v-2:[a-z]{3}}.{v-3:[a-z]{3}}`,
|
hostTemplate: `{v-1:[a-z]{3}}.{v-2:[a-z]{3}}.{v-3:[a-z]{3}}`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Path route with single pattern with pipe, match",
|
title: "Path route with single pattern with pipe, match",
|
||||||
route: new(Route).Path("/{category:a|b/c}"),
|
route: new(Route).Path("/{category:a|b/c}"),
|
||||||
request: newRequest("GET", "http://localhost/a"),
|
request: newRequest("GET", "http://localhost/a"),
|
||||||
vars: map[string]string{"category": "a"},
|
vars: map[string]string{"category": "a"},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/a",
|
path: "/a",
|
||||||
path_template: `/{category:a|b/c}`,
|
pathTemplate: `/{category:a|b/c}`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Path route with single pattern with pipe, match",
|
title: "Path route with single pattern with pipe, match",
|
||||||
route: new(Route).Path("/{category:a|b/c}"),
|
route: new(Route).Path("/{category:a|b/c}"),
|
||||||
request: newRequest("GET", "http://localhost/b/c"),
|
request: newRequest("GET", "http://localhost/b/c"),
|
||||||
vars: map[string]string{"category": "b/c"},
|
vars: map[string]string{"category": "b/c"},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/b/c",
|
path: "/b/c",
|
||||||
path_template: `/{category:a|b/c}`,
|
pathTemplate: `/{category:a|b/c}`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Path route with multiple patterns with pipe, match",
|
title: "Path route with multiple patterns with pipe, match",
|
||||||
route: new(Route).Path("/{category:a|b/c}/{product}/{id:[0-9]+}"),
|
route: new(Route).Path("/{category:a|b/c}/{product}/{id:[0-9]+}"),
|
||||||
request: newRequest("GET", "http://localhost/a/product_name/1"),
|
request: newRequest("GET", "http://localhost/a/product_name/1"),
|
||||||
vars: map[string]string{"category": "a", "product": "product_name", "id": "1"},
|
vars: map[string]string{"category": "a", "product": "product_name", "id": "1"},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/a/product_name/1",
|
path: "/a/product_name/1",
|
||||||
path_template: `/{category:a|b/c}/{product}/{id:[0-9]+}`,
|
pathTemplate: `/{category:a|b/c}/{product}/{id:[0-9]+}`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Path route with multiple patterns with pipe, match",
|
title: "Path route with multiple patterns with pipe, match",
|
||||||
route: new(Route).Path("/{category:a|b/c}/{product}/{id:[0-9]+}"),
|
route: new(Route).Path("/{category:a|b/c}/{product}/{id:[0-9]+}"),
|
||||||
request: newRequest("GET", "http://localhost/b/c/product_name/1"),
|
request: newRequest("GET", "http://localhost/b/c/product_name/1"),
|
||||||
vars: map[string]string{"category": "b/c", "product": "product_name", "id": "1"},
|
vars: map[string]string{"category": "b/c", "product": "product_name", "id": "1"},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/b/c/product_name/1",
|
path: "/b/c/product_name/1",
|
||||||
path_template: `/{category:a|b/c}/{product}/{id:[0-9]+}`,
|
pathTemplate: `/{category:a|b/c}/{product}/{id:[0-9]+}`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
@@ -261,24 +261,24 @@ func TestPath(t *testing.T) {
|
|||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Path route, do not match with trailing slash in path",
|
title: "Path route, do not match with trailing slash in path",
|
||||||
route: new(Route).Path("/111/"),
|
route: new(Route).Path("/111/"),
|
||||||
request: newRequest("GET", "http://localhost/111"),
|
request: newRequest("GET", "http://localhost/111"),
|
||||||
vars: map[string]string{},
|
vars: map[string]string{},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/111",
|
path: "/111",
|
||||||
path_template: `/111/`,
|
pathTemplate: `/111/`,
|
||||||
shouldMatch: false,
|
shouldMatch: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Path route, do not match with trailing slash in request",
|
title: "Path route, do not match with trailing slash in request",
|
||||||
route: new(Route).Path("/111"),
|
route: new(Route).Path("/111"),
|
||||||
request: newRequest("GET", "http://localhost/111/"),
|
request: newRequest("GET", "http://localhost/111/"),
|
||||||
vars: map[string]string{},
|
vars: map[string]string{},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/111/",
|
path: "/111/",
|
||||||
path_template: `/111`,
|
pathTemplate: `/111`,
|
||||||
shouldMatch: false,
|
shouldMatch: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Path route, wrong path in request in request URL",
|
title: "Path route, wrong path in request in request URL",
|
||||||
@@ -290,94 +290,94 @@ func TestPath(t *testing.T) {
|
|||||||
shouldMatch: false,
|
shouldMatch: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Path route with pattern, match",
|
title: "Path route with pattern, match",
|
||||||
route: new(Route).Path("/111/{v1:[0-9]{3}}/333"),
|
route: new(Route).Path("/111/{v1:[0-9]{3}}/333"),
|
||||||
request: newRequest("GET", "http://localhost/111/222/333"),
|
request: newRequest("GET", "http://localhost/111/222/333"),
|
||||||
vars: map[string]string{"v1": "222"},
|
vars: map[string]string{"v1": "222"},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/111/222/333",
|
path: "/111/222/333",
|
||||||
path_template: `/111/{v1:[0-9]{3}}/333`,
|
pathTemplate: `/111/{v1:[0-9]{3}}/333`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Path route with pattern, URL in request does not match",
|
title: "Path route with pattern, URL in request does not match",
|
||||||
route: new(Route).Path("/111/{v1:[0-9]{3}}/333"),
|
route: new(Route).Path("/111/{v1:[0-9]{3}}/333"),
|
||||||
request: newRequest("GET", "http://localhost/111/aaa/333"),
|
request: newRequest("GET", "http://localhost/111/aaa/333"),
|
||||||
vars: map[string]string{"v1": "222"},
|
vars: map[string]string{"v1": "222"},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/111/222/333",
|
path: "/111/222/333",
|
||||||
path_template: `/111/{v1:[0-9]{3}}/333`,
|
pathTemplate: `/111/{v1:[0-9]{3}}/333`,
|
||||||
shouldMatch: false,
|
shouldMatch: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Path route with multiple patterns, match",
|
title: "Path route with multiple patterns, match",
|
||||||
route: new(Route).Path("/{v1:[0-9]{3}}/{v2:[0-9]{3}}/{v3:[0-9]{3}}"),
|
route: new(Route).Path("/{v1:[0-9]{3}}/{v2:[0-9]{3}}/{v3:[0-9]{3}}"),
|
||||||
request: newRequest("GET", "http://localhost/111/222/333"),
|
request: newRequest("GET", "http://localhost/111/222/333"),
|
||||||
vars: map[string]string{"v1": "111", "v2": "222", "v3": "333"},
|
vars: map[string]string{"v1": "111", "v2": "222", "v3": "333"},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/111/222/333",
|
path: "/111/222/333",
|
||||||
path_template: `/{v1:[0-9]{3}}/{v2:[0-9]{3}}/{v3:[0-9]{3}}`,
|
pathTemplate: `/{v1:[0-9]{3}}/{v2:[0-9]{3}}/{v3:[0-9]{3}}`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Path route with multiple patterns, URL in request does not match",
|
title: "Path route with multiple patterns, URL in request does not match",
|
||||||
route: new(Route).Path("/{v1:[0-9]{3}}/{v2:[0-9]{3}}/{v3:[0-9]{3}}"),
|
route: new(Route).Path("/{v1:[0-9]{3}}/{v2:[0-9]{3}}/{v3:[0-9]{3}}"),
|
||||||
request: newRequest("GET", "http://localhost/111/aaa/333"),
|
request: newRequest("GET", "http://localhost/111/aaa/333"),
|
||||||
vars: map[string]string{"v1": "111", "v2": "222", "v3": "333"},
|
vars: map[string]string{"v1": "111", "v2": "222", "v3": "333"},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/111/222/333",
|
path: "/111/222/333",
|
||||||
path_template: `/{v1:[0-9]{3}}/{v2:[0-9]{3}}/{v3:[0-9]{3}}`,
|
pathTemplate: `/{v1:[0-9]{3}}/{v2:[0-9]{3}}/{v3:[0-9]{3}}`,
|
||||||
shouldMatch: false,
|
shouldMatch: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Path route with multiple patterns with pipe, match",
|
title: "Path route with multiple patterns with pipe, match",
|
||||||
route: new(Route).Path("/{category:a|(b/c)}/{product}/{id:[0-9]+}"),
|
route: new(Route).Path("/{category:a|(b/c)}/{product}/{id:[0-9]+}"),
|
||||||
request: newRequest("GET", "http://localhost/a/product_name/1"),
|
request: newRequest("GET", "http://localhost/a/product_name/1"),
|
||||||
vars: map[string]string{"category": "a", "product": "product_name", "id": "1"},
|
vars: map[string]string{"category": "a", "product": "product_name", "id": "1"},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/a/product_name/1",
|
path: "/a/product_name/1",
|
||||||
path_template: `/{category:a|(b/c)}/{product}/{id:[0-9]+}`,
|
pathTemplate: `/{category:a|(b/c)}/{product}/{id:[0-9]+}`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Path route with hyphenated name and pattern, match",
|
title: "Path route with hyphenated name and pattern, match",
|
||||||
route: new(Route).Path("/111/{v-1:[0-9]{3}}/333"),
|
route: new(Route).Path("/111/{v-1:[0-9]{3}}/333"),
|
||||||
request: newRequest("GET", "http://localhost/111/222/333"),
|
request: newRequest("GET", "http://localhost/111/222/333"),
|
||||||
vars: map[string]string{"v-1": "222"},
|
vars: map[string]string{"v-1": "222"},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/111/222/333",
|
path: "/111/222/333",
|
||||||
path_template: `/111/{v-1:[0-9]{3}}/333`,
|
pathTemplate: `/111/{v-1:[0-9]{3}}/333`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Path route with multiple hyphenated names and patterns, match",
|
title: "Path route with multiple hyphenated names and patterns, match",
|
||||||
route: new(Route).Path("/{v-1:[0-9]{3}}/{v-2:[0-9]{3}}/{v-3:[0-9]{3}}"),
|
route: new(Route).Path("/{v-1:[0-9]{3}}/{v-2:[0-9]{3}}/{v-3:[0-9]{3}}"),
|
||||||
request: newRequest("GET", "http://localhost/111/222/333"),
|
request: newRequest("GET", "http://localhost/111/222/333"),
|
||||||
vars: map[string]string{"v-1": "111", "v-2": "222", "v-3": "333"},
|
vars: map[string]string{"v-1": "111", "v-2": "222", "v-3": "333"},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/111/222/333",
|
path: "/111/222/333",
|
||||||
path_template: `/{v-1:[0-9]{3}}/{v-2:[0-9]{3}}/{v-3:[0-9]{3}}`,
|
pathTemplate: `/{v-1:[0-9]{3}}/{v-2:[0-9]{3}}/{v-3:[0-9]{3}}`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Path route with multiple hyphenated names and patterns with pipe, match",
|
title: "Path route with multiple hyphenated names and patterns with pipe, match",
|
||||||
route: new(Route).Path("/{product-category:a|(b/c)}/{product-name}/{product-id:[0-9]+}"),
|
route: new(Route).Path("/{product-category:a|(b/c)}/{product-name}/{product-id:[0-9]+}"),
|
||||||
request: newRequest("GET", "http://localhost/a/product_name/1"),
|
request: newRequest("GET", "http://localhost/a/product_name/1"),
|
||||||
vars: map[string]string{"product-category": "a", "product-name": "product_name", "product-id": "1"},
|
vars: map[string]string{"product-category": "a", "product-name": "product_name", "product-id": "1"},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/a/product_name/1",
|
path: "/a/product_name/1",
|
||||||
path_template: `/{product-category:a|(b/c)}/{product-name}/{product-id:[0-9]+}`,
|
pathTemplate: `/{product-category:a|(b/c)}/{product-name}/{product-id:[0-9]+}`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Path route with multiple hyphenated names and patterns with pipe and case insensitive, match",
|
title: "Path route with multiple hyphenated names and patterns with pipe and case insensitive, match",
|
||||||
route: new(Route).Path("/{type:(?i:daily|mini|variety)}-{date:\\d{4,4}-\\d{2,2}-\\d{2,2}}"),
|
route: new(Route).Path("/{type:(?i:daily|mini|variety)}-{date:\\d{4,4}-\\d{2,2}-\\d{2,2}}"),
|
||||||
request: newRequest("GET", "http://localhost/daily-2016-01-01"),
|
request: newRequest("GET", "http://localhost/daily-2016-01-01"),
|
||||||
vars: map[string]string{"type": "daily", "date": "2016-01-01"},
|
vars: map[string]string{"type": "daily", "date": "2016-01-01"},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/daily-2016-01-01",
|
path: "/daily-2016-01-01",
|
||||||
path_template: `/{type:(?i:daily|mini|variety)}-{date:\d{4,4}-\d{2,2}-\d{2,2}}`,
|
pathTemplate: `/{type:(?i:daily|mini|variety)}-{date:\d{4,4}-\d{2,2}-\d{2,2}}`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -417,44 +417,44 @@ func TestPathPrefix(t *testing.T) {
|
|||||||
shouldMatch: false,
|
shouldMatch: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "PathPrefix route with pattern, match",
|
title: "PathPrefix route with pattern, match",
|
||||||
route: new(Route).PathPrefix("/111/{v1:[0-9]{3}}"),
|
route: new(Route).PathPrefix("/111/{v1:[0-9]{3}}"),
|
||||||
request: newRequest("GET", "http://localhost/111/222/333"),
|
request: newRequest("GET", "http://localhost/111/222/333"),
|
||||||
vars: map[string]string{"v1": "222"},
|
vars: map[string]string{"v1": "222"},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/111/222",
|
path: "/111/222",
|
||||||
path_template: `/111/{v1:[0-9]{3}}`,
|
pathTemplate: `/111/{v1:[0-9]{3}}`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "PathPrefix route with pattern, URL prefix in request does not match",
|
title: "PathPrefix route with pattern, URL prefix in request does not match",
|
||||||
route: new(Route).PathPrefix("/111/{v1:[0-9]{3}}"),
|
route: new(Route).PathPrefix("/111/{v1:[0-9]{3}}"),
|
||||||
request: newRequest("GET", "http://localhost/111/aaa/333"),
|
request: newRequest("GET", "http://localhost/111/aaa/333"),
|
||||||
vars: map[string]string{"v1": "222"},
|
vars: map[string]string{"v1": "222"},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/111/222",
|
path: "/111/222",
|
||||||
path_template: `/111/{v1:[0-9]{3}}`,
|
pathTemplate: `/111/{v1:[0-9]{3}}`,
|
||||||
shouldMatch: false,
|
shouldMatch: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "PathPrefix route with multiple patterns, match",
|
title: "PathPrefix route with multiple patterns, match",
|
||||||
route: new(Route).PathPrefix("/{v1:[0-9]{3}}/{v2:[0-9]{3}}"),
|
route: new(Route).PathPrefix("/{v1:[0-9]{3}}/{v2:[0-9]{3}}"),
|
||||||
request: newRequest("GET", "http://localhost/111/222/333"),
|
request: newRequest("GET", "http://localhost/111/222/333"),
|
||||||
vars: map[string]string{"v1": "111", "v2": "222"},
|
vars: map[string]string{"v1": "111", "v2": "222"},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/111/222",
|
path: "/111/222",
|
||||||
path_template: `/{v1:[0-9]{3}}/{v2:[0-9]{3}}`,
|
pathTemplate: `/{v1:[0-9]{3}}/{v2:[0-9]{3}}`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "PathPrefix route with multiple patterns, URL prefix in request does not match",
|
title: "PathPrefix route with multiple patterns, URL prefix in request does not match",
|
||||||
route: new(Route).PathPrefix("/{v1:[0-9]{3}}/{v2:[0-9]{3}}"),
|
route: new(Route).PathPrefix("/{v1:[0-9]{3}}/{v2:[0-9]{3}}"),
|
||||||
request: newRequest("GET", "http://localhost/111/aaa/333"),
|
request: newRequest("GET", "http://localhost/111/aaa/333"),
|
||||||
vars: map[string]string{"v1": "111", "v2": "222"},
|
vars: map[string]string{"v1": "111", "v2": "222"},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/111/222",
|
path: "/111/222",
|
||||||
path_template: `/{v1:[0-9]{3}}/{v2:[0-9]{3}}`,
|
pathTemplate: `/{v1:[0-9]{3}}/{v2:[0-9]{3}}`,
|
||||||
shouldMatch: false,
|
shouldMatch: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -467,70 +467,70 @@ func TestPathPrefix(t *testing.T) {
|
|||||||
func TestHostPath(t *testing.T) {
|
func TestHostPath(t *testing.T) {
|
||||||
tests := []routeTest{
|
tests := []routeTest{
|
||||||
{
|
{
|
||||||
title: "Host and Path route, match",
|
title: "Host and Path route, match",
|
||||||
route: new(Route).Host("aaa.bbb.ccc").Path("/111/222/333"),
|
route: new(Route).Host("aaa.bbb.ccc").Path("/111/222/333"),
|
||||||
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
|
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
|
||||||
vars: map[string]string{},
|
vars: map[string]string{},
|
||||||
host: "",
|
host: "",
|
||||||
path: "",
|
path: "",
|
||||||
path_template: `/111/222/333`,
|
pathTemplate: `/111/222/333`,
|
||||||
host_template: `aaa.bbb.ccc`,
|
hostTemplate: `aaa.bbb.ccc`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Host and Path route, wrong host in request URL",
|
title: "Host and Path route, wrong host in request URL",
|
||||||
route: new(Route).Host("aaa.bbb.ccc").Path("/111/222/333"),
|
route: new(Route).Host("aaa.bbb.ccc").Path("/111/222/333"),
|
||||||
request: newRequest("GET", "http://aaa.222.ccc/111/222/333"),
|
request: newRequest("GET", "http://aaa.222.ccc/111/222/333"),
|
||||||
vars: map[string]string{},
|
vars: map[string]string{},
|
||||||
host: "",
|
host: "",
|
||||||
path: "",
|
path: "",
|
||||||
path_template: `/111/222/333`,
|
pathTemplate: `/111/222/333`,
|
||||||
host_template: `aaa.bbb.ccc`,
|
hostTemplate: `aaa.bbb.ccc`,
|
||||||
shouldMatch: false,
|
shouldMatch: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Host and Path route with pattern, match",
|
title: "Host and Path route with pattern, match",
|
||||||
route: new(Route).Host("aaa.{v1:[a-z]{3}}.ccc").Path("/111/{v2:[0-9]{3}}/333"),
|
route: new(Route).Host("aaa.{v1:[a-z]{3}}.ccc").Path("/111/{v2:[0-9]{3}}/333"),
|
||||||
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
|
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
|
||||||
vars: map[string]string{"v1": "bbb", "v2": "222"},
|
vars: map[string]string{"v1": "bbb", "v2": "222"},
|
||||||
host: "aaa.bbb.ccc",
|
host: "aaa.bbb.ccc",
|
||||||
path: "/111/222/333",
|
path: "/111/222/333",
|
||||||
path_template: `/111/{v2:[0-9]{3}}/333`,
|
pathTemplate: `/111/{v2:[0-9]{3}}/333`,
|
||||||
host_template: `aaa.{v1:[a-z]{3}}.ccc`,
|
hostTemplate: `aaa.{v1:[a-z]{3}}.ccc`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Host and Path route with pattern, URL in request does not match",
|
title: "Host and Path route with pattern, URL in request does not match",
|
||||||
route: new(Route).Host("aaa.{v1:[a-z]{3}}.ccc").Path("/111/{v2:[0-9]{3}}/333"),
|
route: new(Route).Host("aaa.{v1:[a-z]{3}}.ccc").Path("/111/{v2:[0-9]{3}}/333"),
|
||||||
request: newRequest("GET", "http://aaa.222.ccc/111/222/333"),
|
request: newRequest("GET", "http://aaa.222.ccc/111/222/333"),
|
||||||
vars: map[string]string{"v1": "bbb", "v2": "222"},
|
vars: map[string]string{"v1": "bbb", "v2": "222"},
|
||||||
host: "aaa.bbb.ccc",
|
host: "aaa.bbb.ccc",
|
||||||
path: "/111/222/333",
|
path: "/111/222/333",
|
||||||
path_template: `/111/{v2:[0-9]{3}}/333`,
|
pathTemplate: `/111/{v2:[0-9]{3}}/333`,
|
||||||
host_template: `aaa.{v1:[a-z]{3}}.ccc`,
|
hostTemplate: `aaa.{v1:[a-z]{3}}.ccc`,
|
||||||
shouldMatch: false,
|
shouldMatch: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Host and Path route with multiple patterns, match",
|
title: "Host and Path route with multiple patterns, match",
|
||||||
route: new(Route).Host("{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}").Path("/{v4:[0-9]{3}}/{v5:[0-9]{3}}/{v6:[0-9]{3}}"),
|
route: new(Route).Host("{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}").Path("/{v4:[0-9]{3}}/{v5:[0-9]{3}}/{v6:[0-9]{3}}"),
|
||||||
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
|
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
|
||||||
vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc", "v4": "111", "v5": "222", "v6": "333"},
|
vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc", "v4": "111", "v5": "222", "v6": "333"},
|
||||||
host: "aaa.bbb.ccc",
|
host: "aaa.bbb.ccc",
|
||||||
path: "/111/222/333",
|
path: "/111/222/333",
|
||||||
path_template: `/{v4:[0-9]{3}}/{v5:[0-9]{3}}/{v6:[0-9]{3}}`,
|
pathTemplate: `/{v4:[0-9]{3}}/{v5:[0-9]{3}}/{v6:[0-9]{3}}`,
|
||||||
host_template: `{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}`,
|
hostTemplate: `{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Host and Path route with multiple patterns, URL in request does not match",
|
title: "Host and Path route with multiple patterns, URL in request does not match",
|
||||||
route: new(Route).Host("{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}").Path("/{v4:[0-9]{3}}/{v5:[0-9]{3}}/{v6:[0-9]{3}}"),
|
route: new(Route).Host("{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}").Path("/{v4:[0-9]{3}}/{v5:[0-9]{3}}/{v6:[0-9]{3}}"),
|
||||||
request: newRequest("GET", "http://aaa.222.ccc/111/222/333"),
|
request: newRequest("GET", "http://aaa.222.ccc/111/222/333"),
|
||||||
vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc", "v4": "111", "v5": "222", "v6": "333"},
|
vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc", "v4": "111", "v5": "222", "v6": "333"},
|
||||||
host: "aaa.bbb.ccc",
|
host: "aaa.bbb.ccc",
|
||||||
path: "/111/222/333",
|
path: "/111/222/333",
|
||||||
path_template: `/{v4:[0-9]{3}}/{v5:[0-9]{3}}/{v6:[0-9]{3}}`,
|
pathTemplate: `/{v4:[0-9]{3}}/{v5:[0-9]{3}}/{v6:[0-9]{3}}`,
|
||||||
host_template: `{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}`,
|
hostTemplate: `{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}`,
|
||||||
shouldMatch: false,
|
shouldMatch: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -648,26 +648,26 @@ func TestQueries(t *testing.T) {
|
|||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Queries route, match with a query string",
|
title: "Queries route, match with a query string",
|
||||||
route: new(Route).Host("www.example.com").Path("/api").Queries("foo", "bar", "baz", "ding"),
|
route: new(Route).Host("www.example.com").Path("/api").Queries("foo", "bar", "baz", "ding"),
|
||||||
request: newRequest("GET", "http://www.example.com/api?foo=bar&baz=ding"),
|
request: newRequest("GET", "http://www.example.com/api?foo=bar&baz=ding"),
|
||||||
vars: map[string]string{},
|
vars: map[string]string{},
|
||||||
host: "",
|
host: "",
|
||||||
path: "",
|
path: "",
|
||||||
path_template: `/api`,
|
pathTemplate: `/api`,
|
||||||
host_template: `www.example.com`,
|
hostTemplate: `www.example.com`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Queries route, match with a query string out of order",
|
title: "Queries route, match with a query string out of order",
|
||||||
route: new(Route).Host("www.example.com").Path("/api").Queries("foo", "bar", "baz", "ding"),
|
route: new(Route).Host("www.example.com").Path("/api").Queries("foo", "bar", "baz", "ding"),
|
||||||
request: newRequest("GET", "http://www.example.com/api?baz=ding&foo=bar"),
|
request: newRequest("GET", "http://www.example.com/api?baz=ding&foo=bar"),
|
||||||
vars: map[string]string{},
|
vars: map[string]string{},
|
||||||
host: "",
|
host: "",
|
||||||
path: "",
|
path: "",
|
||||||
path_template: `/api`,
|
pathTemplate: `/api`,
|
||||||
host_template: `www.example.com`,
|
hostTemplate: `www.example.com`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "Queries route, bad query",
|
title: "Queries route, bad query",
|
||||||
@@ -947,10 +947,10 @@ func TestBuildVarsFunc(t *testing.T) {
|
|||||||
vars["v2"] = "a"
|
vars["v2"] = "a"
|
||||||
return vars
|
return vars
|
||||||
}),
|
}),
|
||||||
request: newRequest("GET", "http://localhost/111/2"),
|
request: newRequest("GET", "http://localhost/111/2"),
|
||||||
path: "/111/3a",
|
path: "/111/3a",
|
||||||
path_template: `/111/{v1:\d}{v2:.*}`,
|
pathTemplate: `/111/{v1:\d}{v2:.*}`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "BuildVarsFunc set on route and parent route",
|
title: "BuildVarsFunc set on route and parent route",
|
||||||
@@ -961,10 +961,10 @@ func TestBuildVarsFunc(t *testing.T) {
|
|||||||
vars["v2"] = "b"
|
vars["v2"] = "b"
|
||||||
return vars
|
return vars
|
||||||
}),
|
}),
|
||||||
request: newRequest("GET", "http://localhost/1/a"),
|
request: newRequest("GET", "http://localhost/1/a"),
|
||||||
path: "/2/b",
|
path: "/2/b",
|
||||||
path_template: `/{v1:\d}/{v2:\w}`,
|
pathTemplate: `/{v1:\d}/{v2:\w}`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -980,42 +980,42 @@ func TestSubRouter(t *testing.T) {
|
|||||||
|
|
||||||
tests := []routeTest{
|
tests := []routeTest{
|
||||||
{
|
{
|
||||||
route: subrouter1.Path("/{v2:[a-z]+}"),
|
route: subrouter1.Path("/{v2:[a-z]+}"),
|
||||||
request: newRequest("GET", "http://aaa.google.com/bbb"),
|
request: newRequest("GET", "http://aaa.google.com/bbb"),
|
||||||
vars: map[string]string{"v1": "aaa", "v2": "bbb"},
|
vars: map[string]string{"v1": "aaa", "v2": "bbb"},
|
||||||
host: "aaa.google.com",
|
host: "aaa.google.com",
|
||||||
path: "/bbb",
|
path: "/bbb",
|
||||||
path_template: `/{v2:[a-z]+}`,
|
pathTemplate: `/{v2:[a-z]+}`,
|
||||||
host_template: `{v1:[a-z]+}.google.com`,
|
hostTemplate: `{v1:[a-z]+}.google.com`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
route: subrouter1.Path("/{v2:[a-z]+}"),
|
route: subrouter1.Path("/{v2:[a-z]+}"),
|
||||||
request: newRequest("GET", "http://111.google.com/111"),
|
request: newRequest("GET", "http://111.google.com/111"),
|
||||||
vars: map[string]string{"v1": "aaa", "v2": "bbb"},
|
vars: map[string]string{"v1": "aaa", "v2": "bbb"},
|
||||||
host: "aaa.google.com",
|
host: "aaa.google.com",
|
||||||
path: "/bbb",
|
path: "/bbb",
|
||||||
path_template: `/{v2:[a-z]+}`,
|
pathTemplate: `/{v2:[a-z]+}`,
|
||||||
host_template: `{v1:[a-z]+}.google.com`,
|
hostTemplate: `{v1:[a-z]+}.google.com`,
|
||||||
shouldMatch: false,
|
shouldMatch: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
route: subrouter2.Path("/baz/{v2}"),
|
route: subrouter2.Path("/baz/{v2}"),
|
||||||
request: newRequest("GET", "http://localhost/foo/bar/baz/ding"),
|
request: newRequest("GET", "http://localhost/foo/bar/baz/ding"),
|
||||||
vars: map[string]string{"v1": "bar", "v2": "ding"},
|
vars: map[string]string{"v1": "bar", "v2": "ding"},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/foo/bar/baz/ding",
|
path: "/foo/bar/baz/ding",
|
||||||
path_template: `/foo/{v1}/baz/{v2}`,
|
pathTemplate: `/foo/{v1}/baz/{v2}`,
|
||||||
shouldMatch: true,
|
shouldMatch: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
route: subrouter2.Path("/baz/{v2}"),
|
route: subrouter2.Path("/baz/{v2}"),
|
||||||
request: newRequest("GET", "http://localhost/foo/bar"),
|
request: newRequest("GET", "http://localhost/foo/bar"),
|
||||||
vars: map[string]string{"v1": "bar", "v2": "ding"},
|
vars: map[string]string{"v1": "bar", "v2": "ding"},
|
||||||
host: "",
|
host: "",
|
||||||
path: "/foo/bar/baz/ding",
|
path: "/foo/bar/baz/ding",
|
||||||
path_template: `/foo/{v1}/baz/{v2}`,
|
pathTemplate: `/foo/{v1}/baz/{v2}`,
|
||||||
shouldMatch: false,
|
shouldMatch: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1331,23 +1331,23 @@ func testRoute(t *testing.T, test routeTest) {
|
|||||||
|
|
||||||
func testTemplate(t *testing.T, test routeTest) {
|
func testTemplate(t *testing.T, test routeTest) {
|
||||||
route := test.route
|
route := test.route
|
||||||
path_template := test.path_template
|
pathTemplate := test.pathTemplate
|
||||||
if len(path_template) == 0 {
|
if len(pathTemplate) == 0 {
|
||||||
path_template = test.path
|
pathTemplate = test.path
|
||||||
}
|
}
|
||||||
host_template := test.host_template
|
hostTemplate := test.hostTemplate
|
||||||
if len(host_template) == 0 {
|
if len(hostTemplate) == 0 {
|
||||||
host_template = test.host
|
hostTemplate = test.host
|
||||||
}
|
}
|
||||||
|
|
||||||
path_tmpl, path_err := route.GetPathTemplate()
|
routePathTemplate, pathErr := route.GetPathTemplate()
|
||||||
if path_err == nil && path_tmpl != path_template {
|
if pathErr == nil && routePathTemplate != pathTemplate {
|
||||||
t.Errorf("(%v) GetPathTemplate not equal: expected %v, got %v", test.title, path_template, path_tmpl)
|
t.Errorf("(%v) GetPathTemplate not equal: expected %v, got %v", test.title, pathTemplate, routePathTemplate)
|
||||||
}
|
}
|
||||||
|
|
||||||
host_tmpl, host_err := route.GetHostTemplate()
|
routeHostTemplate, hostErr := route.GetHostTemplate()
|
||||||
if host_err == nil && host_tmpl != host_template {
|
if hostErr == nil && routeHostTemplate != hostTemplate {
|
||||||
t.Errorf("(%v) GetHostTemplate not equal: expected %v, got %v", test.title, host_template, host_tmpl)
|
t.Errorf("(%v) GetHostTemplate not equal: expected %v, got %v", test.title, hostTemplate, routeHostTemplate)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user