From 8eb02a9f12e527fe2ce9ab3b640579a6104f2d44 Mon Sep 17 00:00:00 2001 From: Dj Gilcrease Date: Wed, 8 Jul 2015 09:47:01 -0700 Subject: [PATCH 1/4] Add URLPathTemplate to Route to make it easier to generate a API page that lists all routes by template --- route.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/route.go b/route.go index 6cb12d3..77fca93 100644 --- a/route.go +++ b/route.go @@ -508,6 +508,19 @@ func (r *Route) URLPath(pairs ...string) (*url.URL, error) { }, nil } +// URLPathTemplate returns the template used to match against for the route +// +// The route must have a path defined. +func (r *Route) URLPathTemplate() (string, error) { + if r.err != nil { + return "", r.err + } + if r.regexp == nil || r.regexp.path == nil { + return "", errors.New("mux: route doesn't have a path") + } + return r.regexp.path.template, nil +} + // prepareVars converts the route variable pairs into a map. If the route has a // BuildVarsFunc, it is invoked. func (r *Route) prepareVars(pairs ...string) (map[string]string, error) { From 0d60c4bfebed9448afb78ab65727a497cc2d9f3d Mon Sep 17 00:00:00 2001 From: Dj Gilcrease Date: Sun, 28 Feb 2016 14:42:09 -0800 Subject: [PATCH 2/4] Add tests for GetPathTemplate. Added GetHostTemplate and associated tests as well --- mux_test.go | 102 +++++++++++++++++++++++++++++++++++++++++++++++----- route.go | 17 +++++++-- 2 files changed, 108 insertions(+), 11 deletions(-) diff --git a/mux_test.go b/mux_test.go index 1ea439a..6fe249a 100644 --- a/mux_test.go +++ b/mux_test.go @@ -32,6 +32,8 @@ type routeTest struct { 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 + path_template string // the expected path template to match + host_template string // the expected host template to match shouldMatch bool // whether the request is expected to match the route at all shouldRedirect bool // whether the request should result in a redirect } @@ -119,6 +121,7 @@ func TestHost(t *testing.T) { vars: map[string]string{"v1": "bbb"}, host: "aaa.bbb.ccc", path: "", + host_template: `aaa.{v1:[a-z]{3}}.ccc`, shouldMatch: true, }, { @@ -128,6 +131,7 @@ func TestHost(t *testing.T) { vars: map[string]string{"v1": "bbb"}, host: "aaa.bbb.ccc", path: "", + host_template: `aaa.{v1:[a-z]{2}(b|c)}.ccc`, shouldMatch: true, }, { @@ -137,6 +141,7 @@ func TestHost(t *testing.T) { vars: map[string]string{"v1": "bbb"}, host: "aaa.bbb.ccc", path: "", + host_template: `aaa.{v1:[a-z]{3}}.ccc`, shouldMatch: false, }, { @@ -146,6 +151,7 @@ func TestHost(t *testing.T) { vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc"}, host: "aaa.bbb.ccc", path: "", + host_template: `{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}`, shouldMatch: true, }, { @@ -155,6 +161,7 @@ func TestHost(t *testing.T) { vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc"}, host: "aaa.bbb.ccc", path: "", + host_template: `{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}`, shouldMatch: false, }, { @@ -164,6 +171,7 @@ func TestHost(t *testing.T) { vars: map[string]string{"v-1": "bbb"}, host: "aaa.bbb.ccc", path: "", + host_template: `aaa.{v-1:[a-z]{3}}.ccc`, shouldMatch: true, }, { @@ -173,6 +181,7 @@ func TestHost(t *testing.T) { vars: map[string]string{"v-1": "bbb"}, host: "aaa.bbb.ccc", path: "", + host_template: `aaa.{v-1:[a-z]{2}(b|c)}.ccc`, shouldMatch: true, }, { @@ -182,6 +191,7 @@ func TestHost(t *testing.T) { vars: map[string]string{"v-1": "aaa", "v-2": "bbb", "v-3": "ccc"}, host: "aaa.bbb.ccc", path: "", + host_template: `{v-1:[a-z]{3}}.{v-2:[a-z]{3}}.{v-3:[a-z]{3}}`, shouldMatch: true, }, { @@ -191,6 +201,7 @@ func TestHost(t *testing.T) { vars: map[string]string{"category": "a"}, host: "", path: "/a", + path_template: `/{category:a|b/c}`, shouldMatch: true, }, { @@ -200,6 +211,7 @@ func TestHost(t *testing.T) { vars: map[string]string{"category": "b/c"}, host: "", path: "/b/c", + path_template: `/{category:a|b/c}`, shouldMatch: true, }, { @@ -209,6 +221,7 @@ func TestHost(t *testing.T) { vars: map[string]string{"category": "a", "product": "product_name", "id": "1"}, host: "", path: "/a/product_name/1", + path_template: `/{category:a|b/c}/{product}/{id:[0-9]+}`, shouldMatch: true, }, { @@ -218,11 +231,13 @@ func TestHost(t *testing.T) { vars: map[string]string{"category": "b/c", "product": "product_name", "id": "1"}, host: "", path: "/b/c/product_name/1", + path_template: `/{category:a|b/c}/{product}/{id:[0-9]+}`, shouldMatch: true, }, } for _, test := range tests { testRoute(t, test) + testTemplate(t, test) } } @@ -253,6 +268,7 @@ func TestPath(t *testing.T) { vars: map[string]string{}, host: "", path: "/111", + path_template: `/111/`, shouldMatch: false, }, { @@ -262,6 +278,7 @@ func TestPath(t *testing.T) { vars: map[string]string{}, host: "", path: "/111/", + path_template: `/111`, shouldMatch: false, }, { @@ -280,6 +297,7 @@ func TestPath(t *testing.T) { vars: map[string]string{"v1": "222"}, host: "", path: "/111/222/333", + path_template: `/111/{v1:[0-9]{3}}/333`, shouldMatch: true, }, { @@ -289,6 +307,7 @@ func TestPath(t *testing.T) { vars: map[string]string{"v1": "222"}, host: "", path: "/111/222/333", + path_template: `/111/{v1:[0-9]{3}}/333`, shouldMatch: false, }, { @@ -298,6 +317,7 @@ func TestPath(t *testing.T) { vars: map[string]string{"v1": "111", "v2": "222", "v3": "333"}, host: "", path: "/111/222/333", + path_template: `/{v1:[0-9]{3}}/{v2:[0-9]{3}}/{v3:[0-9]{3}}`, shouldMatch: true, }, { @@ -307,6 +327,7 @@ func TestPath(t *testing.T) { vars: map[string]string{"v1": "111", "v2": "222", "v3": "333"}, host: "", path: "/111/222/333", + path_template: `/{v1:[0-9]{3}}/{v2:[0-9]{3}}/{v3:[0-9]{3}}`, shouldMatch: false, }, { @@ -316,6 +337,7 @@ func TestPath(t *testing.T) { vars: map[string]string{"category": "a", "product": "product_name", "id": "1"}, host: "", path: "/a/product_name/1", + path_template: `/{category:a|(b/c)}/{product}/{id:[0-9]+}`, shouldMatch: true, }, { @@ -325,6 +347,7 @@ func TestPath(t *testing.T) { vars: map[string]string{"v-1": "222"}, host: "", path: "/111/222/333", + path_template: `/111/{v-1:[0-9]{3}}/333`, shouldMatch: true, }, { @@ -334,6 +357,7 @@ func TestPath(t *testing.T) { vars: map[string]string{"v-1": "111", "v-2": "222", "v-3": "333"}, host: "", path: "/111/222/333", + path_template: `/{v-1:[0-9]{3}}/{v-2:[0-9]{3}}/{v-3:[0-9]{3}}`, shouldMatch: true, }, { @@ -343,12 +367,14 @@ func TestPath(t *testing.T) { vars: map[string]string{"product-category": "a", "product-name": "product_name", "product-id": "1"}, host: "", path: "/a/product_name/1", + path_template: `/{product-category:a|(b/c)}/{product-name}/{product-id:[0-9]+}`, shouldMatch: true, }, } for _, test := range tests { testRoute(t, test) + testTemplate(t, test) } } @@ -388,6 +414,7 @@ func TestPathPrefix(t *testing.T) { vars: map[string]string{"v1": "222"}, host: "", path: "/111/222", + path_template: `/111/{v1:[0-9]{3}}`, shouldMatch: true, }, { @@ -397,6 +424,7 @@ func TestPathPrefix(t *testing.T) { vars: map[string]string{"v1": "222"}, host: "", path: "/111/222", + path_template: `/111/{v1:[0-9]{3}}`, shouldMatch: false, }, { @@ -406,6 +434,7 @@ func TestPathPrefix(t *testing.T) { vars: map[string]string{"v1": "111", "v2": "222"}, host: "", path: "/111/222", + path_template: `/{v1:[0-9]{3}}/{v2:[0-9]{3}}`, shouldMatch: true, }, { @@ -415,12 +444,14 @@ func TestPathPrefix(t *testing.T) { vars: map[string]string{"v1": "111", "v2": "222"}, host: "", path: "/111/222", + path_template: `/{v1:[0-9]{3}}/{v2:[0-9]{3}}`, shouldMatch: false, }, } for _, test := range tests { testRoute(t, test) + testTemplate(t, test) } } @@ -433,6 +464,8 @@ func TestHostPath(t *testing.T) { vars: map[string]string{}, host: "", path: "", + path_template: `/111/222/333`, + host_template: `aaa.bbb.ccc`, shouldMatch: true, }, { @@ -442,6 +475,8 @@ func TestHostPath(t *testing.T) { vars: map[string]string{}, host: "", path: "", + path_template: `/111/222/333`, + host_template: `aaa.bbb.ccc`, shouldMatch: false, }, { @@ -451,6 +486,8 @@ func TestHostPath(t *testing.T) { vars: map[string]string{"v1": "bbb", "v2": "222"}, host: "aaa.bbb.ccc", path: "/111/222/333", + path_template: `/111/{v2:[0-9]{3}}/333`, + host_template: `aaa.{v1:[a-z]{3}}.ccc`, shouldMatch: true, }, { @@ -460,6 +497,8 @@ func TestHostPath(t *testing.T) { vars: map[string]string{"v1": "bbb", "v2": "222"}, host: "aaa.bbb.ccc", path: "/111/222/333", + path_template: `/111/{v2:[0-9]{3}}/333`, + host_template: `aaa.{v1:[a-z]{3}}.ccc`, shouldMatch: false, }, { @@ -469,6 +508,8 @@ func TestHostPath(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", + path_template: `/{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}}`, shouldMatch: true, }, { @@ -478,12 +519,15 @@ func TestHostPath(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", + path_template: `/{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}}`, shouldMatch: false, }, } for _, test := range tests { testRoute(t, test) + testTemplate(t, test) } } @@ -541,6 +585,7 @@ func TestHeaders(t *testing.T) { for _, test := range tests { testRoute(t, test) + testTemplate(t, test) } } @@ -578,6 +623,7 @@ func TestMethods(t *testing.T) { for _, test := range tests { testRoute(t, test) + testTemplate(t, test) } } @@ -599,6 +645,8 @@ func TestQueries(t *testing.T) { vars: map[string]string{}, host: "", path: "", + path_template: `/api`, + host_template: `www.example.com`, shouldMatch: true, }, { @@ -608,6 +656,8 @@ func TestQueries(t *testing.T) { vars: map[string]string{}, host: "", path: "", + path_template: `/api`, + host_template: `www.example.com`, shouldMatch: true, }, { @@ -803,6 +853,7 @@ func TestQueries(t *testing.T) { for _, test := range tests { testRoute(t, test) + testTemplate(t, test) } } @@ -839,6 +890,7 @@ func TestSchemes(t *testing.T) { } for _, test := range tests { testRoute(t, test) + testTemplate(t, test) } } @@ -873,6 +925,7 @@ func TestMatcherFunc(t *testing.T) { for _, test := range tests { testRoute(t, test) + testTemplate(t, test) } } @@ -887,6 +940,7 @@ func TestBuildVarsFunc(t *testing.T) { }), request: newRequest("GET", "http://localhost/111/2"), path: "/111/3a", + path_template: `/111/{v1:\d}{v2:.*}`, shouldMatch: true, }, { @@ -900,12 +954,14 @@ func TestBuildVarsFunc(t *testing.T) { }), request: newRequest("GET", "http://localhost/1/a"), path: "/2/b", + path_template: `/{v1:\d}/{v2:\w}`, shouldMatch: true, }, } for _, test := range tests { testRoute(t, test) + testTemplate(t, test) } } @@ -920,6 +976,8 @@ func TestSubRouter(t *testing.T) { vars: map[string]string{"v1": "aaa", "v2": "bbb"}, host: "aaa.google.com", path: "/bbb", + path_template: `/{v2:[a-z]+}`, + host_template: `{v1:[a-z]+}.google.com`, shouldMatch: true, }, { @@ -928,6 +986,8 @@ func TestSubRouter(t *testing.T) { vars: map[string]string{"v1": "aaa", "v2": "bbb"}, host: "aaa.google.com", path: "/bbb", + path_template: `/{v2:[a-z]+}`, + host_template: `{v1:[a-z]+}.google.com`, shouldMatch: false, }, { @@ -936,6 +996,7 @@ func TestSubRouter(t *testing.T) { vars: map[string]string{"v1": "bar", "v2": "ding"}, host: "", path: "/foo/bar/baz/ding", + path_template: `/foo/{v1}/baz/{v2}`, shouldMatch: true, }, { @@ -944,12 +1005,14 @@ func TestSubRouter(t *testing.T) { vars: map[string]string{"v1": "bar", "v2": "ding"}, host: "", path: "/foo/bar/baz/ding", + path_template: `/foo/{v1}/baz/{v2}`, shouldMatch: false, }, } for _, test := range tests { testRoute(t, test) + testTemplate(t, test) } } @@ -1045,6 +1108,7 @@ func TestStrictSlash(t *testing.T) { for _, test := range tests { testRoute(t, test) + testTemplate(t, test) } } @@ -1152,14 +1216,13 @@ func TestSubrouterErrorHandling(t *testing.T) { // ---------------------------------------------------------------------------- func getRouteTemplate(route *Route) string { - host, path := "none", "none" - if route.regexp != nil { - if route.regexp.host != nil { - host = route.regexp.host.template - } - if route.regexp.path != nil { - path = route.regexp.path.template - } + host, err := route.GetHostTemplate() + if err != nil { + host = "none" + } + path, err := route.GetPathTemplate() + if err != nil { + path = "none" } return fmt.Sprintf("Host: %v, Path: %v", host, path) } @@ -1221,6 +1284,29 @@ func testRoute(t *testing.T, test routeTest) { } } +func testTemplate(t *testing.T, test routeTest) { + route := test.route + path_template := test.path_template + if len(path_template) == 0 { + path_template = test.path + } + host_template := test.host_template + if len(host_template) == 0 { + host_template = test.host + } + + path_tmpl, path_err := route.GetPathTemplate() + if path_err == nil && path_tmpl != path_template { + t.Errorf("(%v) GetPathTemplate not equal: expected %v, got %v", test.title, path_template, path_tmpl) + } + + + host_tmpl, host_err := route.GetHostTemplate() + if host_err == nil && host_tmpl != host_template { + t.Errorf("(%v) GetHostTemplate not equal: expected %v, got %v", test.title, host_template, host_tmpl) + } +} + // Tests that the context is cleared or not cleared properly depending on // the configuration of the router func TestKeepContext(t *testing.T) { diff --git a/route.go b/route.go index cb0e57d..37d0336 100644 --- a/route.go +++ b/route.go @@ -532,10 +532,11 @@ func (r *Route) URLPath(pairs ...string) (*url.URL, error) { }, nil } -// URLPathTemplate returns the template used to match against for the route -// +// GetPathTemplate and GetHostTemplate returns the template used to match against for the route +// This is userful for building simple REST API documentation, +// and instrumentation for services like New Relic to ensure consistent reporting // The route must have a path defined. -func (r *Route) URLPathTemplate() (string, error) { +func (r *Route) GetPathTemplate() (string, error) { if r.err != nil { return "", r.err } @@ -545,6 +546,16 @@ func (r *Route) URLPathTemplate() (string, error) { return r.regexp.path.template, nil } +func (r *Route) GetHostTemplate() (string, error) { + if r.err != nil { + return "", r.err + } + if r.regexp == nil || r.regexp.host == nil { + return "", errors.New("mux: route doesn't have a host") + } + return r.regexp.host.template, nil +} + // prepareVars converts the route variable pairs into a map. If the route has a // BuildVarsFunc, it is invoked. func (r *Route) prepareVars(pairs ...string) (map[string]string, error) { From f84ab9ab620f5ac2d587d5688b7b0820b5bdf9bf Mon Sep 17 00:00:00 2001 From: Dj Gilcrease Date: Sun, 28 Feb 2016 14:46:18 -0800 Subject: [PATCH 3/4] Fix go fmt issues --- mux_test.go | 545 ++++++++++++++++++++++++++-------------------------- 1 file changed, 272 insertions(+), 273 deletions(-) diff --git a/mux_test.go b/mux_test.go index 6fe249a..8912d09 100644 --- a/mux_test.go +++ b/mux_test.go @@ -32,8 +32,8 @@ type routeTest struct { 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 - path_template string // the expected path template to match - host_template string // the expected host template to match + path_template string // the expected path template to match + host_template string // the expected host template to match shouldMatch bool // whether the request is expected to match the route at all shouldRedirect bool // whether the request should result in a redirect } @@ -115,124 +115,124 @@ func TestHost(t *testing.T) { shouldMatch: false, }, { - title: "Host route with pattern, match", - route: new(Route).Host("aaa.{v1:[a-z]{3}}.ccc"), - request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), - vars: map[string]string{"v1": "bbb"}, - host: "aaa.bbb.ccc", - path: "", + title: "Host route with pattern, match", + route: new(Route).Host("aaa.{v1:[a-z]{3}}.ccc"), + request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), + vars: map[string]string{"v1": "bbb"}, + host: "aaa.bbb.ccc", + path: "", host_template: `aaa.{v1:[a-z]{3}}.ccc`, - shouldMatch: true, + shouldMatch: true, }, { - title: "Host route with pattern, additional capturing group, match", - route: new(Route).Host("aaa.{v1:[a-z]{2}(b|c)}.ccc"), - request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), - vars: map[string]string{"v1": "bbb"}, - host: "aaa.bbb.ccc", - path: "", + title: "Host route with pattern, additional capturing group, match", + route: new(Route).Host("aaa.{v1:[a-z]{2}(b|c)}.ccc"), + request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), + vars: map[string]string{"v1": "bbb"}, + host: "aaa.bbb.ccc", + path: "", host_template: `aaa.{v1:[a-z]{2}(b|c)}.ccc`, - shouldMatch: true, + shouldMatch: true, }, { - title: "Host route with pattern, wrong host in request URL", - route: new(Route).Host("aaa.{v1:[a-z]{3}}.ccc"), - request: newRequest("GET", "http://aaa.222.ccc/111/222/333"), - vars: map[string]string{"v1": "bbb"}, - host: "aaa.bbb.ccc", - path: "", + title: "Host route with pattern, wrong host in request URL", + route: new(Route).Host("aaa.{v1:[a-z]{3}}.ccc"), + request: newRequest("GET", "http://aaa.222.ccc/111/222/333"), + vars: map[string]string{"v1": "bbb"}, + host: "aaa.bbb.ccc", + path: "", host_template: `aaa.{v1:[a-z]{3}}.ccc`, - shouldMatch: false, + shouldMatch: false, }, { - title: "Host route with multiple patterns, match", - 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"), - vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc"}, - host: "aaa.bbb.ccc", - path: "", + title: "Host route with multiple patterns, match", + 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"), + vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc"}, + host: "aaa.bbb.ccc", + path: "", host_template: `{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", - 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"), - vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc"}, - host: "aaa.bbb.ccc", - path: "", + 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}}"), + request: newRequest("GET", "http://aaa.222.ccc/111/222/333"), + vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc"}, + host: "aaa.bbb.ccc", + path: "", host_template: `{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", - route: new(Route).Host("aaa.{v-1:[a-z]{3}}.ccc"), - request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), - vars: map[string]string{"v-1": "bbb"}, - host: "aaa.bbb.ccc", - path: "", + title: "Host route with hyphenated name and pattern, match", + route: new(Route).Host("aaa.{v-1:[a-z]{3}}.ccc"), + request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), + vars: map[string]string{"v-1": "bbb"}, + host: "aaa.bbb.ccc", + path: "", host_template: `aaa.{v-1:[a-z]{3}}.ccc`, - shouldMatch: true, + shouldMatch: true, }, { - 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"), - request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), - vars: map[string]string{"v-1": "bbb"}, - host: "aaa.bbb.ccc", - path: "", + 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"), + request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), + vars: map[string]string{"v-1": "bbb"}, + host: "aaa.bbb.ccc", + path: "", host_template: `aaa.{v-1:[a-z]{2}(b|c)}.ccc`, - shouldMatch: true, + shouldMatch: true, }, { - 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}}"), - request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), - vars: map[string]string{"v-1": "aaa", "v-2": "bbb", "v-3": "ccc"}, - host: "aaa.bbb.ccc", - path: "", + 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}}"), + request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), + vars: map[string]string{"v-1": "aaa", "v-2": "bbb", "v-3": "ccc"}, + host: "aaa.bbb.ccc", + path: "", host_template: `{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", - route: new(Route).Path("/{category:a|b/c}"), - request: newRequest("GET", "http://localhost/a"), - vars: map[string]string{"category": "a"}, - host: "", - path: "/a", + title: "Path route with single pattern with pipe, match", + route: new(Route).Path("/{category:a|b/c}"), + request: newRequest("GET", "http://localhost/a"), + vars: map[string]string{"category": "a"}, + host: "", + path: "/a", path_template: `/{category:a|b/c}`, - shouldMatch: true, + shouldMatch: true, }, { - title: "Path route with single pattern with pipe, match", - route: new(Route).Path("/{category:a|b/c}"), - request: newRequest("GET", "http://localhost/b/c"), - vars: map[string]string{"category": "b/c"}, - host: "", - path: "/b/c", + title: "Path route with single pattern with pipe, match", + route: new(Route).Path("/{category:a|b/c}"), + request: newRequest("GET", "http://localhost/b/c"), + vars: map[string]string{"category": "b/c"}, + host: "", + path: "/b/c", path_template: `/{category:a|b/c}`, - shouldMatch: true, + shouldMatch: true, }, { - title: "Path route with multiple patterns with pipe, match", - route: new(Route).Path("/{category:a|b/c}/{product}/{id:[0-9]+}"), - request: newRequest("GET", "http://localhost/a/product_name/1"), - vars: map[string]string{"category": "a", "product": "product_name", "id": "1"}, - host: "", - path: "/a/product_name/1", + title: "Path route with multiple patterns with pipe, match", + route: new(Route).Path("/{category:a|b/c}/{product}/{id:[0-9]+}"), + request: newRequest("GET", "http://localhost/a/product_name/1"), + vars: map[string]string{"category": "a", "product": "product_name", "id": "1"}, + host: "", + path: "/a/product_name/1", path_template: `/{category:a|b/c}/{product}/{id:[0-9]+}`, - shouldMatch: true, + shouldMatch: true, }, { - title: "Path route with multiple patterns with pipe, match", - route: new(Route).Path("/{category:a|b/c}/{product}/{id:[0-9]+}"), - request: newRequest("GET", "http://localhost/b/c/product_name/1"), - vars: map[string]string{"category": "b/c", "product": "product_name", "id": "1"}, - host: "", - path: "/b/c/product_name/1", + title: "Path route with multiple patterns with pipe, match", + route: new(Route).Path("/{category:a|b/c}/{product}/{id:[0-9]+}"), + request: newRequest("GET", "http://localhost/b/c/product_name/1"), + vars: map[string]string{"category": "b/c", "product": "product_name", "id": "1"}, + host: "", + path: "/b/c/product_name/1", path_template: `/{category:a|b/c}/{product}/{id:[0-9]+}`, - shouldMatch: true, + shouldMatch: true, }, } for _, test := range tests { @@ -262,24 +262,24 @@ func TestPath(t *testing.T) { shouldMatch: true, }, { - title: "Path route, do not match with trailing slash in path", - route: new(Route).Path("/111/"), - request: newRequest("GET", "http://localhost/111"), - vars: map[string]string{}, - host: "", - path: "/111", + title: "Path route, do not match with trailing slash in path", + route: new(Route).Path("/111/"), + request: newRequest("GET", "http://localhost/111"), + vars: map[string]string{}, + host: "", + path: "/111", path_template: `/111/`, - shouldMatch: false, + shouldMatch: false, }, { - title: "Path route, do not match with trailing slash in request", - route: new(Route).Path("/111"), - request: newRequest("GET", "http://localhost/111/"), - vars: map[string]string{}, - host: "", - path: "/111/", + title: "Path route, do not match with trailing slash in request", + route: new(Route).Path("/111"), + request: newRequest("GET", "http://localhost/111/"), + vars: map[string]string{}, + host: "", + path: "/111/", path_template: `/111`, - shouldMatch: false, + shouldMatch: false, }, { title: "Path route, wrong path in request in request URL", @@ -291,84 +291,84 @@ func TestPath(t *testing.T) { shouldMatch: false, }, { - title: "Path route with pattern, match", - route: new(Route).Path("/111/{v1:[0-9]{3}}/333"), - request: newRequest("GET", "http://localhost/111/222/333"), - vars: map[string]string{"v1": "222"}, - host: "", - path: "/111/222/333", + title: "Path route with pattern, match", + route: new(Route).Path("/111/{v1:[0-9]{3}}/333"), + request: newRequest("GET", "http://localhost/111/222/333"), + vars: map[string]string{"v1": "222"}, + host: "", + path: "/111/222/333", path_template: `/111/{v1:[0-9]{3}}/333`, - shouldMatch: true, + shouldMatch: true, }, { - title: "Path route with pattern, URL in request does not match", - route: new(Route).Path("/111/{v1:[0-9]{3}}/333"), - request: newRequest("GET", "http://localhost/111/aaa/333"), - vars: map[string]string{"v1": "222"}, - host: "", - path: "/111/222/333", + title: "Path route with pattern, URL in request does not match", + route: new(Route).Path("/111/{v1:[0-9]{3}}/333"), + request: newRequest("GET", "http://localhost/111/aaa/333"), + vars: map[string]string{"v1": "222"}, + host: "", + path: "/111/222/333", path_template: `/111/{v1:[0-9]{3}}/333`, - shouldMatch: false, + shouldMatch: false, }, { - title: "Path route with multiple patterns, match", - route: new(Route).Path("/{v1:[0-9]{3}}/{v2:[0-9]{3}}/{v3:[0-9]{3}}"), - request: newRequest("GET", "http://localhost/111/222/333"), - vars: map[string]string{"v1": "111", "v2": "222", "v3": "333"}, - host: "", - path: "/111/222/333", + title: "Path route with multiple patterns, match", + route: new(Route).Path("/{v1:[0-9]{3}}/{v2:[0-9]{3}}/{v3:[0-9]{3}}"), + request: newRequest("GET", "http://localhost/111/222/333"), + vars: map[string]string{"v1": "111", "v2": "222", "v3": "333"}, + host: "", + path: "/111/222/333", path_template: `/{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", - route: new(Route).Path("/{v1:[0-9]{3}}/{v2:[0-9]{3}}/{v3:[0-9]{3}}"), - request: newRequest("GET", "http://localhost/111/aaa/333"), - vars: map[string]string{"v1": "111", "v2": "222", "v3": "333"}, - host: "", - path: "/111/222/333", + 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}}"), + request: newRequest("GET", "http://localhost/111/aaa/333"), + vars: map[string]string{"v1": "111", "v2": "222", "v3": "333"}, + host: "", + path: "/111/222/333", path_template: `/{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", - route: new(Route).Path("/{category:a|(b/c)}/{product}/{id:[0-9]+}"), - request: newRequest("GET", "http://localhost/a/product_name/1"), - vars: map[string]string{"category": "a", "product": "product_name", "id": "1"}, - host: "", - path: "/a/product_name/1", + title: "Path route with multiple patterns with pipe, match", + route: new(Route).Path("/{category:a|(b/c)}/{product}/{id:[0-9]+}"), + request: newRequest("GET", "http://localhost/a/product_name/1"), + vars: map[string]string{"category": "a", "product": "product_name", "id": "1"}, + host: "", + path: "/a/product_name/1", path_template: `/{category:a|(b/c)}/{product}/{id:[0-9]+}`, - shouldMatch: true, + shouldMatch: true, }, { - title: "Path route with hyphenated name and pattern, match", - route: new(Route).Path("/111/{v-1:[0-9]{3}}/333"), - request: newRequest("GET", "http://localhost/111/222/333"), - vars: map[string]string{"v-1": "222"}, - host: "", - path: "/111/222/333", + title: "Path route with hyphenated name and pattern, match", + route: new(Route).Path("/111/{v-1:[0-9]{3}}/333"), + request: newRequest("GET", "http://localhost/111/222/333"), + vars: map[string]string{"v-1": "222"}, + host: "", + path: "/111/222/333", path_template: `/111/{v-1:[0-9]{3}}/333`, - shouldMatch: true, + shouldMatch: true, }, { - 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}}"), - request: newRequest("GET", "http://localhost/111/222/333"), - vars: map[string]string{"v-1": "111", "v-2": "222", "v-3": "333"}, - host: "", - path: "/111/222/333", + 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}}"), + request: newRequest("GET", "http://localhost/111/222/333"), + vars: map[string]string{"v-1": "111", "v-2": "222", "v-3": "333"}, + host: "", + path: "/111/222/333", path_template: `/{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", - route: new(Route).Path("/{product-category:a|(b/c)}/{product-name}/{product-id:[0-9]+}"), - request: newRequest("GET", "http://localhost/a/product_name/1"), - vars: map[string]string{"product-category": "a", "product-name": "product_name", "product-id": "1"}, - host: "", - path: "/a/product_name/1", + 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]+}"), + request: newRequest("GET", "http://localhost/a/product_name/1"), + vars: map[string]string{"product-category": "a", "product-name": "product_name", "product-id": "1"}, + host: "", + path: "/a/product_name/1", path_template: `/{product-category:a|(b/c)}/{product-name}/{product-id:[0-9]+}`, - shouldMatch: true, + shouldMatch: true, }, } @@ -408,44 +408,44 @@ func TestPathPrefix(t *testing.T) { shouldMatch: false, }, { - title: "PathPrefix route with pattern, match", - route: new(Route).PathPrefix("/111/{v1:[0-9]{3}}"), - request: newRequest("GET", "http://localhost/111/222/333"), - vars: map[string]string{"v1": "222"}, - host: "", - path: "/111/222", + title: "PathPrefix route with pattern, match", + route: new(Route).PathPrefix("/111/{v1:[0-9]{3}}"), + request: newRequest("GET", "http://localhost/111/222/333"), + vars: map[string]string{"v1": "222"}, + host: "", + path: "/111/222", path_template: `/111/{v1:[0-9]{3}}`, - shouldMatch: true, + shouldMatch: true, }, { - title: "PathPrefix route with pattern, URL prefix in request does not match", - route: new(Route).PathPrefix("/111/{v1:[0-9]{3}}"), - request: newRequest("GET", "http://localhost/111/aaa/333"), - vars: map[string]string{"v1": "222"}, - host: "", - path: "/111/222", + title: "PathPrefix route with pattern, URL prefix in request does not match", + route: new(Route).PathPrefix("/111/{v1:[0-9]{3}}"), + request: newRequest("GET", "http://localhost/111/aaa/333"), + vars: map[string]string{"v1": "222"}, + host: "", + path: "/111/222", path_template: `/111/{v1:[0-9]{3}}`, - shouldMatch: false, + shouldMatch: false, }, { - title: "PathPrefix route with multiple patterns, match", - route: new(Route).PathPrefix("/{v1:[0-9]{3}}/{v2:[0-9]{3}}"), - request: newRequest("GET", "http://localhost/111/222/333"), - vars: map[string]string{"v1": "111", "v2": "222"}, - host: "", - path: "/111/222", + title: "PathPrefix route with multiple patterns, match", + route: new(Route).PathPrefix("/{v1:[0-9]{3}}/{v2:[0-9]{3}}"), + request: newRequest("GET", "http://localhost/111/222/333"), + vars: map[string]string{"v1": "111", "v2": "222"}, + host: "", + path: "/111/222", path_template: `/{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", - route: new(Route).PathPrefix("/{v1:[0-9]{3}}/{v2:[0-9]{3}}"), - request: newRequest("GET", "http://localhost/111/aaa/333"), - vars: map[string]string{"v1": "111", "v2": "222"}, - host: "", - path: "/111/222", + 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}}"), + request: newRequest("GET", "http://localhost/111/aaa/333"), + vars: map[string]string{"v1": "111", "v2": "222"}, + host: "", + path: "/111/222", path_template: `/{v1:[0-9]{3}}/{v2:[0-9]{3}}`, - shouldMatch: false, + shouldMatch: false, }, } @@ -458,70 +458,70 @@ func TestPathPrefix(t *testing.T) { func TestHostPath(t *testing.T) { tests := []routeTest{ { - title: "Host and Path route, match", - route: new(Route).Host("aaa.bbb.ccc").Path("/111/222/333"), - request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), - vars: map[string]string{}, - host: "", - path: "", + title: "Host and Path route, match", + route: new(Route).Host("aaa.bbb.ccc").Path("/111/222/333"), + request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), + vars: map[string]string{}, + host: "", + path: "", path_template: `/111/222/333`, host_template: `aaa.bbb.ccc`, - shouldMatch: true, + shouldMatch: true, }, { - title: "Host and Path route, wrong host in request URL", - route: new(Route).Host("aaa.bbb.ccc").Path("/111/222/333"), - request: newRequest("GET", "http://aaa.222.ccc/111/222/333"), - vars: map[string]string{}, - host: "", - path: "", + title: "Host and Path route, wrong host in request URL", + route: new(Route).Host("aaa.bbb.ccc").Path("/111/222/333"), + request: newRequest("GET", "http://aaa.222.ccc/111/222/333"), + vars: map[string]string{}, + host: "", + path: "", path_template: `/111/222/333`, host_template: `aaa.bbb.ccc`, - shouldMatch: false, + shouldMatch: false, }, { - 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"), - request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), - vars: map[string]string{"v1": "bbb", "v2": "222"}, - host: "aaa.bbb.ccc", - path: "/111/222/333", + 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"), + request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), + vars: map[string]string{"v1": "bbb", "v2": "222"}, + host: "aaa.bbb.ccc", + path: "/111/222/333", path_template: `/111/{v2:[0-9]{3}}/333`, host_template: `aaa.{v1:[a-z]{3}}.ccc`, - shouldMatch: true, + shouldMatch: true, }, { - 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"), - request: newRequest("GET", "http://aaa.222.ccc/111/222/333"), - vars: map[string]string{"v1": "bbb", "v2": "222"}, - host: "aaa.bbb.ccc", - path: "/111/222/333", + 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"), + request: newRequest("GET", "http://aaa.222.ccc/111/222/333"), + vars: map[string]string{"v1": "bbb", "v2": "222"}, + host: "aaa.bbb.ccc", + path: "/111/222/333", path_template: `/111/{v2:[0-9]{3}}/333`, host_template: `aaa.{v1:[a-z]{3}}.ccc`, - shouldMatch: false, + shouldMatch: false, }, { - 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}}"), - 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"}, - host: "aaa.bbb.ccc", - path: "/111/222/333", + 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}}"), + 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"}, + host: "aaa.bbb.ccc", + path: "/111/222/333", path_template: `/{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}}`, - shouldMatch: true, + shouldMatch: true, }, { - 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}}"), - 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"}, - host: "aaa.bbb.ccc", - path: "/111/222/333", + 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}}"), + 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"}, + host: "aaa.bbb.ccc", + path: "/111/222/333", path_template: `/{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}}`, - shouldMatch: false, + shouldMatch: false, }, } @@ -639,26 +639,26 @@ func TestQueries(t *testing.T) { shouldMatch: true, }, { - title: "Queries route, match with a query string", - 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"), - vars: map[string]string{}, - host: "", - path: "", + title: "Queries route, match with a query string", + 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"), + vars: map[string]string{}, + host: "", + path: "", path_template: `/api`, host_template: `www.example.com`, - shouldMatch: true, + shouldMatch: true, }, { - 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"), - request: newRequest("GET", "http://www.example.com/api?baz=ding&foo=bar"), - vars: map[string]string{}, - host: "", - path: "", + 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"), + request: newRequest("GET", "http://www.example.com/api?baz=ding&foo=bar"), + vars: map[string]string{}, + host: "", + path: "", path_template: `/api`, host_template: `www.example.com`, - shouldMatch: true, + shouldMatch: true, }, { title: "Queries route, bad query", @@ -938,10 +938,10 @@ func TestBuildVarsFunc(t *testing.T) { vars["v2"] = "a" return vars }), - request: newRequest("GET", "http://localhost/111/2"), - path: "/111/3a", + request: newRequest("GET", "http://localhost/111/2"), + path: "/111/3a", path_template: `/111/{v1:\d}{v2:.*}`, - shouldMatch: true, + shouldMatch: true, }, { title: "BuildVarsFunc set on route and parent route", @@ -952,10 +952,10 @@ func TestBuildVarsFunc(t *testing.T) { vars["v2"] = "b" return vars }), - request: newRequest("GET", "http://localhost/1/a"), - path: "/2/b", + request: newRequest("GET", "http://localhost/1/a"), + path: "/2/b", path_template: `/{v1:\d}/{v2:\w}`, - shouldMatch: true, + shouldMatch: true, }, } @@ -971,42 +971,42 @@ func TestSubRouter(t *testing.T) { 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", + 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", path_template: `/{v2:[a-z]+}`, host_template: `{v1:[a-z]+}.google.com`, - shouldMatch: true, + 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", + 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", path_template: `/{v2:[a-z]+}`, host_template: `{v1:[a-z]+}.google.com`, - shouldMatch: false, + 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", + 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", path_template: `/foo/{v1}/baz/{v2}`, - shouldMatch: true, + 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", + 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", path_template: `/foo/{v1}/baz/{v2}`, - shouldMatch: false, + shouldMatch: false, }, } @@ -1296,13 +1296,12 @@ func testTemplate(t *testing.T, test routeTest) { } path_tmpl, path_err := route.GetPathTemplate() - if path_err == nil && path_tmpl != path_template { + if path_err == nil && path_tmpl != path_template { t.Errorf("(%v) GetPathTemplate not equal: expected %v, got %v", test.title, path_template, path_tmpl) } - host_tmpl, host_err := route.GetHostTemplate() - if host_err == nil && host_tmpl != host_template { + if host_err == nil && host_tmpl != host_template { t.Errorf("(%v) GetHostTemplate not equal: expected %v, got %v", test.title, host_template, host_tmpl) } } From f7ac7251e3b615c63207d481413aab0726812499 Mon Sep 17 00:00:00 2001 From: Dj Gilcrease Date: Mon, 29 Feb 2016 07:54:15 -0800 Subject: [PATCH 4/4] Update doc strings based on feedback --- route.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/route.go b/route.go index 37d0336..6e63389 100644 --- a/route.go +++ b/route.go @@ -532,10 +532,11 @@ func (r *Route) URLPath(pairs ...string) (*url.URL, error) { }, nil } -// GetPathTemplate and GetHostTemplate returns the template used to match against for the route -// This is userful for building simple REST API documentation, -// and instrumentation for services like New Relic to ensure consistent reporting -// The route must have a path defined. +// GetPathTemplate returns the template used to build the +// route match. +// This is useful for building simple REST API documentation and for instrumentation +// against third-party services. +// An error will be returned if the route does not define a path. func (r *Route) GetPathTemplate() (string, error) { if r.err != nil { return "", r.err @@ -546,6 +547,11 @@ func (r *Route) GetPathTemplate() (string, error) { return r.regexp.path.template, nil } +// GetHostTemplate returns the template used to build the +// route match. +// This is useful for building simple REST API documentation and for instrumentation +// against third-party services. +// An error will be returned if the route does not define a host. func (r *Route) GetHostTemplate() (string, error) { if r.err != nil { return "", r.err