Check for multiple wildcards per path segment
Fixes #56
diff --git a/tree.go b/tree.go
index c84a489..e96d361 100644
--- a/tree.go
+++ b/tree.go
@@ -186,7 +186,7 @@
}
func (n *node) insertChild(numParams uint8, path string, handle Handle) {
- var offset int
+ var offset int // already handled bytes of the path
// find prefix until first wildcard (beginning with ':'' or '*'')
for i, max := 0, len(path); numParams > 0; i++ {
@@ -195,7 +195,7 @@
continue
}
- // Check if this Node existing children which would be
+ // check if this Node existing children which would be
// unreachable if we insert the wildcard here
if len(n.children) > 0 {
panic("wildcard route conflicts with existing children")
@@ -204,9 +204,16 @@
// find wildcard end (either '/' or path end)
end := i + 1
for end < max && path[end] != '/' {
- end++
+ switch path[end] {
+ // the wildcard name must not contain ':' and '*'
+ case ':', '*':
+ panic("only one wildcard per path segment is allowed")
+ default:
+ end++
+ }
}
+ // check if the wildcard has a name
if end-i < 2 {
panic("wildcards must be named with a non-empty name")
}
diff --git a/tree_test.go b/tree_test.go
index ed1f9a8..484621e 100644
--- a/tree_test.go
+++ b/tree_test.go
@@ -339,6 +339,27 @@
testRoutes(t, routes)
}
+func TestTreeDoubleWildcard(t *testing.T) {
+ const panicMsg = "only one wildcard per path segment is allowed"
+
+ routes := [...]string{
+ "/:foo:bar",
+ "/:foo:bar/",
+ "/:foo*bar",
+ }
+
+ for _, route := range routes {
+ tree := &node{}
+ recv := catchPanic(func() {
+ tree.addRoute(route, nil)
+ })
+
+ if rs, ok := recv.(string); !ok || rs != panicMsg {
+ t.Fatalf(`"Expected panic "%s" for route '%s', got "%v"`, panicMsg, route, recv)
+ }
+ }
+}
+
/*func TestTreeDuplicateWildcard(t *testing.T) {
tree := &node{}