Skip to content

Commit

Permalink
Document behaviour of StrictSlash and PathPrefix better, and add test…
Browse files Browse the repository at this point in the history
…s to nail this down
  • Loading branch information
ttencate committed Apr 23, 2014
1 parent 525eff4 commit 033224c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
9 changes: 7 additions & 2 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,15 @@ func (r *Router) GetRoute(name string) *Route {
return r.getNamedRoutes()[name]
}

// StrictSlash defines the slash behavior for new routes.
// StrictSlash defines the trailing slash behavior for new routes. The initial
// value is false.
//
// When true, if the route path is "/path/", accessing "/path" will redirect
// to the former and vice versa.
// to the former and vice versa. In other words, your application will always
// see the path as specified in the route.
//
// When false, if the route path is "/path", accessing "/path/" will not match
// this route and vice versa.
//
// Special case: when a route sets a path prefix, strict slash is
// automatically set to false for that route because the redirect behavior
Expand Down
27 changes: 27 additions & 0 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,33 @@ func TestPath(t *testing.T) {
path: "/111/222/333",
shouldMatch: true,
},
{
title: "Path route, match with trailing slash in request and path",
route: new(Route).Path("/111/"),
request: newRequest("GET", "http://localhost/111/"),
vars: map[string]string{},
host: "",
path: "/111/",
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",
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/",
shouldMatch: false,
},
{
title: "Path route, wrong path in request in request URL",
route: new(Route).Path("/111/222/333"),
Expand Down
13 changes: 9 additions & 4 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,15 @@ func (r *Route) Path(tpl string) *Route {

// PathPrefix -----------------------------------------------------------------

// PathPrefix adds a matcher for the URL path prefix. Note that it does not
// treat slashes specially ("/foobar/" will be matched by the prefix "/foo") so
// in most cases you'll want to use a trailing slash here. See Route.Path() for
// details on the tpl argument.
// PathPrefix adds a matcher for the URL path prefix. This matches if the given
// template is a prefix of the full URL path. See Route.Path() for details on
// the tpl argument.
//
// Note that it does not treat slashes specially ("/foobar/" will be matched by
// the prefix "/foo") so you may want to use a trailing slash here.
//
// Also note that the setting of Router.StrictSlash() has no effect on routes
// with a PathPrefix matcher.
func (r *Route) PathPrefix(tpl string) *Route {
r.strictSlash = false
r.err = r.addRegexpMatcher(tpl, false, true)
Expand Down

0 comments on commit 033224c

Please sign in to comment.