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

Most upvoted comments

Actually, this will pass:

{
    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,
},

We could probably close this.