mux: URL variables bug for multiple patterns with relatively complex regexp
Hi,
There’s a bug with URL variable with a relatively complex regexp.
For example, let’s take the regexp a|(b/c)
which should match a
or b/c
.
If I use the regexp in a path with one pattern, it works. You can test it with these routeTests:
tests := []routeTest{
{
title: "Path route with single pattern with pipe, match",
route: new(Route).Path("/{category:a|(b/c)}"),
request: newRequest("GET", "http://localhost/a"),
vars: map[string]string{"category": "a"},
host: "",
path: "/a",
shouldMatch: true,
},
{
title: "Path route with single pattern with pipe, match",
route: new(Route).Path("/{category:a|(b/c)}"),
request: newRequest("GET", "http://localhost/b/c"),
vars: map[string]string{"category": "b/c"},
host: "",
path: "/b/c",
shouldMatch: true,
},
}
But if I try to use it in a path with multiple patterns, it produces weird results.
This test…
tests := []routeTest{
{
title: "Path route with multiple patterns with pipe, match",
route: new(Route).Path("/{category:a|(b/c)}/{product}/{id:[0-9]+}"),
request: newRequest("GET", "http://localhost/a/product_name/1"),
vars: map[string]string{"category": "a", "product": "product_name", "id": "1"},
host: "",
path: "/a/product_name/1",
shouldMatch: true,
},
}
…fails because the variables don’t match:
$ go test
--- FAIL: TestPath (0.00 seconds)
mux_test.go:810: (Path route with multiple patterns with pipe, match) Vars not equal:
expected map[category:a product:product_name id:1],
got map[category:a product: id:product_name]
And this test…
tests := []routeTest{
{
title: "Path route with multiple patterns with pipe, match",
route: new(Route).Path("/{category:a|(b/c)}/{product}/{id:[0-9]+}"),
request: newRequest("GET", "http://localhost/b/c/product_name/1"),
vars: map[string]string{"category": "b/c", "product": "product_name", "id": "1"},
host: "",
path: "/b/c/product_name/1",
shouldMatch: true,
},
}
…also fails because the variables don’t match either (but in a different way):
$ go test
--- FAIL: TestPath (0.00 seconds)
mux_test.go:810: (Path route with multiple patterns with pipe, match) Vars not equal:
expected map[category:b/c product:product_name id:1],
got map[category:b/c product:b/c id:product_name]
EDIT: Fix manual editing mistakes (category_name instead of product_name)
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 18 (7 by maintainers)
Commits related to this issue
- Add tests for patterns with pipe closes #62 — committed to gemnasium/mux by deleted user 9 years ago
- fix use of capturing subexpressions in pattern matches. The router now associates a regexp named group with each mux variable. It only fills variables when capturing group name match instead of relyi... — committed to gorilla/mux by kisielk 9 years ago
- Merge pull request #144 from tumdum/regexp-speedup [refactor] Only wrap regexp in group if not already in a group + perf. gains. - Addresses https://github.com/gorilla/mux/issues/62 - ~27% less a... — committed to gorilla/mux by elithrar 8 years ago
Actually, this will pass:
We could probably close this.