Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ calling mux.Vars():
vars := mux.Vars(request)
category := vars["category"]

Note that if any capturing groups are present, mux will panic() during parsing. To prevent
this, convert any capturing groups to non-capturing, e.g. change "/{sort:(asc|desc)}" to
"/{sort:(?:asc|desc)}". This is a change from prior versions which behaved unpredictably
when capturing groups were present.

And this is all you need to know about the basic usage. More advanced options
are explained below.

Expand Down
10 changes: 10 additions & 0 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,16 @@ func TestSubrouterErrorHandling(t *testing.T) {
}
}

// See: https://github.com/gorilla/mux/issues/200
func TestPanicOnCapturingGroups(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just rewrite this function:

defer func() {
    if recover() == nil {
        t.Errorf("(%v) Expected panic, however test completed sucessfully.\n", test.title)
    }
}()
r.NewRoute().Path("/{type:(promo|special)}/{promoId}.json")

The rest of the test changes can then be removed. There are already tests for capturing groups.

defer func() {
if recover() == nil {
t.Errorf("(Test that capturing groups now fail fast) Expected panic, however test completed sucessfully.\n")
}
}()
NewRouter().NewRoute().Path("/{type:(promo|special)}/{promoId}.json")
}

// ----------------------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ func newRouteRegexp(tpl string, matchHost, matchPrefix, matchQuery, strictSlash,
if errCompile != nil {
return nil, errCompile
}

// Check for capturing groups which used to work in older versions
if reg.NumSubexp() != len(idxs)/2 {
panic(fmt.Sprintf("route %s contains capture groups in its regexp. ", template) +
"Only non-capturing groups are accepted: e.g. (?:pattern) instead of (pattern)")
}

// Done!
return &routeRegexp{
template: template,
Expand Down