Skip to content

Commit

Permalink
Added method Route.GetPathRegexp
Browse files Browse the repository at this point in the history
  • Loading branch information
bgaifullin authored and kisielk committed May 20, 2017
1 parent 4c1c395 commit 1856953
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
21 changes: 21 additions & 0 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type routeTest struct {
path string // the expected path of the match
pathTemplate string // the expected path template to match
hostTemplate string // the expected host template to match
pathRegexp string // the expected path regexp
shouldMatch bool // whether the request is expected to match the route at all
shouldRedirect bool // whether the request should result in a redirect
}
Expand Down Expand Up @@ -270,6 +271,7 @@ func TestPath(t *testing.T) {
host: "",
path: "/111",
pathTemplate: `/111/`,
pathRegexp: `^/111/$`,
shouldMatch: false,
},
{
Expand All @@ -290,6 +292,7 @@ func TestPath(t *testing.T) {
host: "",
path: "/",
pathTemplate: `/`,
pathRegexp: `^/$`,
shouldMatch: true,
},
{
Expand Down Expand Up @@ -333,6 +336,7 @@ func TestPath(t *testing.T) {
host: "",
path: "/111/222/333",
pathTemplate: `/111/{v1:[0-9]{3}}/333`,
pathRegexp: `^/111/(?P<v0>[0-9]{3})/333$`,
shouldMatch: false,
},
{
Expand All @@ -343,6 +347,7 @@ func TestPath(t *testing.T) {
host: "",
path: "/111/222/333",
pathTemplate: `/{v1:[0-9]{3}}/{v2:[0-9]{3}}/{v3:[0-9]{3}}`,
pathRegexp: `^/(?P<v0>[0-9]{3})/(?P<v1>[0-9]{3})/(?P<v2>[0-9]{3})$`,
shouldMatch: true,
},
{
Expand All @@ -353,6 +358,7 @@ func TestPath(t *testing.T) {
host: "",
path: "/111/222/333",
pathTemplate: `/{v1:[0-9]{3}}/{v2:[0-9]{3}}/{v3:[0-9]{3}}`,
pathRegexp: `^/(?P<v0>[0-9]{3})/(?P<v1>[0-9]{3})/(?P<v2>[0-9]{3})$`,
shouldMatch: false,
},
{
Expand All @@ -363,6 +369,7 @@ func TestPath(t *testing.T) {
host: "",
path: "/a/product_name/1",
pathTemplate: `/{category:a|(?:b/c)}/{product}/{id:[0-9]+}`,
pathRegexp: `^/(?P<v0>a|(?:b/c))/(?P<v1>[^/]+)/(?P<v2>[0-9]+)$`,
shouldMatch: true,
},
{
Expand All @@ -373,6 +380,7 @@ func TestPath(t *testing.T) {
host: "",
path: "/111/222/333",
pathTemplate: `/111/{v-1:[0-9]{3}}/333`,
pathRegexp: `^/111/(?P<v0>[0-9]{3})/333$`,
shouldMatch: true,
},
{
Expand All @@ -383,6 +391,7 @@ func TestPath(t *testing.T) {
host: "",
path: "/111/222/333",
pathTemplate: `/{v-1:[0-9]{3}}/{v-2:[0-9]{3}}/{v-3:[0-9]{3}}`,
pathRegexp: `^/(?P<v0>[0-9]{3})/(?P<v1>[0-9]{3})/(?P<v2>[0-9]{3})$`,
shouldMatch: true,
},
{
Expand All @@ -393,6 +402,7 @@ func TestPath(t *testing.T) {
host: "",
path: "/a/product_name/1",
pathTemplate: `/{product-category:a|(?:b/c)}/{product-name}/{product-id:[0-9]+}`,
pathRegexp: `^/(?P<v0>a|(?:b/c))/(?P<v1>[^/]+)/(?P<v2>[0-9]+)$`,
shouldMatch: true,
},
{
Expand All @@ -403,6 +413,7 @@ func TestPath(t *testing.T) {
host: "",
path: "/daily-2016-01-01",
pathTemplate: `/{type:(?i:daily|mini|variety)}-{date:\d{4,4}-\d{2,2}-\d{2,2}}`,
pathRegexp: `^/(?P<v0>(?i:daily|mini|variety))-(?P<v1>\d{4,4}-\d{2,2}-\d{2,2})$`,
shouldMatch: true,
},
{
Expand All @@ -413,6 +424,7 @@ func TestPath(t *testing.T) {
host: "",
path: "/111/222",
pathTemplate: `/{v1:[0-9]*}{v2:[a-z]*}/{v3:[0-9]*}`,
pathRegexp: `^/(?P<v0>[0-9]*)(?P<v1>[a-z]*)/(?P<v2>[0-9]*)$`,
shouldMatch: true,
},
}
Expand All @@ -421,6 +433,7 @@ func TestPath(t *testing.T) {
testRoute(t, test)
testTemplate(t, test)
testUseEscapedRoute(t, test)
testRegexp(t, test)
}
}

Expand Down Expand Up @@ -1499,6 +1512,14 @@ func testTemplate(t *testing.T, test routeTest) {
}
}

func testRegexp(t *testing.T, test routeTest) {
route := test.route
routePathRegexp, regexpErr := route.GetPathRegexp()
if test.pathRegexp != "" && regexpErr == nil && routePathRegexp != test.pathRegexp {
t.Errorf("(%v) GetPathRegexp not equal: expected %v, got %v", test.title, test.pathRegexp, routePathRegexp)
}
}

type TestA301ResponseWriter struct {
hh http.Header
status int
Expand Down
14 changes: 14 additions & 0 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,20 @@ func (r *Route) GetPathTemplate() (string, error) {
return r.regexp.path.template, nil
}

// GetPathRegexp returns the expanded regular expression used to match route path.
// 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) GetPathRegexp() (string, error) {
if r.err != nil {
return "", r.err
}
if r.regexp == nil || r.regexp.path == nil {
return "", errors.New("mux: route does not have a path")
}
return r.regexp.path.regexp.String(), nil
}

// GetHostTemplate returns the template used to build the
// route match.
// This is useful for building simple REST API documentation and for instrumentation
Expand Down

0 comments on commit 1856953

Please sign in to comment.