Skip to content

Conversation

aeijdenberg
Copy link
Contributor

No description provided.


// Check for capturing groups which used to work in older versions
if reg.NumSubexp() != len(idxs)/2 {
panic("Regexp appears to be using capturing groups. To fix, see: https://github.com/gorilla/mux/issues/200")
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't link to the issue. We should instead show the failing route - "route %q contains capture groups in its regexp. Only non-capturing groups are accepted: e.g. (?:pattern) instead of (pattern)"

(or an approximation of that)

@@ -35,8 +35,10 @@ 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
additionalPath string // additional path to add to route (and parse) during test
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this not be rolled into an existing field for testing? (just wary of adding once-off test fields)

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed, I don't think it's necessary at all. Since calling Path panics right away, that can just be done as its own test. Not all the tests need to follow the exact same structure and go through testRoute.

This way shouldPanic can also be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just added an alternative test - and I didn't see this last comment before I pushed it.

Let me know if you're happy with the alternative which I think is probably more reusable than it previously was, or if you'd like me to just test the parsing separately and remove the additional fields.

Copy link
Contributor

@elithrar elithrar left a comment

Choose a reason for hiding this comment

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

Beyond the testing change - LGTM.

@@ -1389,6 +1391,30 @@ 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.

@aeijdenberg
Copy link
Contributor Author

Simplified test as suggested, and rebased to squash commits.

@kisielk
Copy link
Contributor

kisielk commented Jan 18, 2017

LGTM. @elithrar merge when ready :)

@elithrar elithrar merged commit 392c28f into gorilla:master Jan 18, 2017
@elithrar
Copy link
Contributor

Thanks @aeijdenberg - appreciate it!

@aeijdenberg
Copy link
Contributor Author

You're welcome @elithrar - I think I spent most of a morning debugging an issue that ended up being due to this (after I upgraded a heap of vendored dependencies), so hopefully this saves someone else that time. :)

ldez added a commit to containous/mux that referenced this pull request Oct 20, 2017
juliens pushed a commit to juliens/mux that referenced this pull request Jun 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants