Browse Source

Simplify extractVars, fixes edge cases. (#185)

Also make sure all regexp groups in tests are non-capturing.
pull/127/merge
Richard Musiol 9 years ago committed by Matt Silverlock
parent
commit
0b13a92220
  1. 30
      mux_test.go
  2. 11
      regexp.go

30
mux_test.go

@ -127,12 +127,12 @@ func TestHost(t *testing.T) { @@ -127,12 +127,12 @@ func TestHost(t *testing.T) {
},
{
title: "Host route with pattern, additional capturing group, match",
route: new(Route).Host("aaa.{v1:[a-z]{2}(b|c)}.ccc"),
route: new(Route).Host("aaa.{v1:[a-z]{2}(?:b|c)}.ccc"),
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
vars: map[string]string{"v1": "bbb"},
host: "aaa.bbb.ccc",
path: "",
hostTemplate: `aaa.{v1:[a-z]{2}(b|c)}.ccc`,
hostTemplate: `aaa.{v1:[a-z]{2}(?:b|c)}.ccc`,
shouldMatch: true,
},
{
@ -177,12 +177,12 @@ func TestHost(t *testing.T) { @@ -177,12 +177,12 @@ func TestHost(t *testing.T) {
},
{
title: "Host route with hyphenated name and pattern, additional capturing group, match",
route: new(Route).Host("aaa.{v-1:[a-z]{2}(b|c)}.ccc"),
route: new(Route).Host("aaa.{v-1:[a-z]{2}(?:b|c)}.ccc"),
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
vars: map[string]string{"v-1": "bbb"},
host: "aaa.bbb.ccc",
path: "",
hostTemplate: `aaa.{v-1:[a-z]{2}(b|c)}.ccc`,
hostTemplate: `aaa.{v-1:[a-z]{2}(?:b|c)}.ccc`,
shouldMatch: true,
},
{
@ -367,12 +367,12 @@ func TestPath(t *testing.T) { @@ -367,12 +367,12 @@ func TestPath(t *testing.T) {
},
{
title: "Path route with multiple patterns with pipe, match",
route: new(Route).Path("/{category:a|(b/c)}/{product}/{id:[0-9]+}"),
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",
pathTemplate: `/{category:a|(b/c)}/{product}/{id:[0-9]+}`,
pathTemplate: `/{category:a|(?:b/c)}/{product}/{id:[0-9]+}`,
shouldMatch: true,
},
{
@ -397,12 +397,12 @@ func TestPath(t *testing.T) { @@ -397,12 +397,12 @@ func TestPath(t *testing.T) {
},
{
title: "Path route with multiple hyphenated names and patterns with pipe, match",
route: new(Route).Path("/{product-category:a|(b/c)}/{product-name}/{product-id:[0-9]+}"),
route: new(Route).Path("/{product-category:a|(?:b/c)}/{product-name}/{product-id:[0-9]+}"),
request: newRequest("GET", "http://localhost/a/product_name/1"),
vars: map[string]string{"product-category": "a", "product-name": "product_name", "product-id": "1"},
host: "",
path: "/a/product_name/1",
pathTemplate: `/{product-category:a|(b/c)}/{product-name}/{product-id:[0-9]+}`,
pathTemplate: `/{product-category:a|(?:b/c)}/{product-name}/{product-id:[0-9]+}`,
shouldMatch: true,
},
{
@ -415,6 +415,16 @@ func TestPath(t *testing.T) { @@ -415,6 +415,16 @@ func TestPath(t *testing.T) {
pathTemplate: `/{type:(?i:daily|mini|variety)}-{date:\d{4,4}-\d{2,2}-\d{2,2}}`,
shouldMatch: true,
},
{
title: "Path route with empty match right after other match",
route: new(Route).Path(`/{v1:[0-9]*}{v2:[a-z]*}/{v3:[0-9]*}`),
request: newRequest("GET", "http://localhost/111/222"),
vars: map[string]string{"v1": "111", "v2": "", "v3": "222"},
host: "",
path: "/111/222",
pathTemplate: `/{v1:[0-9]*}{v2:[a-z]*}/{v3:[0-9]*}`,
shouldMatch: true,
},
}
for _, test := range tests {
@ -779,7 +789,7 @@ func TestQueries(t *testing.T) { @@ -779,7 +789,7 @@ func TestQueries(t *testing.T) {
},
{
title: "Queries route with regexp pattern with quantifier, additional capturing group",
route: new(Route).Queries("foo", "{v1:[0-9]{1}(a|b)}"),
route: new(Route).Queries("foo", "{v1:[0-9]{1}(?:a|b)}"),
request: newRequest("GET", "http://localhost?foo=1a"),
vars: map[string]string{"v1": "1a"},
host: "",
@ -824,7 +834,7 @@ func TestQueries(t *testing.T) { @@ -824,7 +834,7 @@ func TestQueries(t *testing.T) {
},
{
title: "Queries route with hyphenated name and pattern with quantifier, additional capturing group",
route: new(Route).Queries("foo", "{v-1:[0-9]{1}(a|b)}"),
route: new(Route).Queries("foo", "{v-1:[0-9]{1}(?:a|b)}"),
request: newRequest("GET", "http://localhost?foo=1a"),
vars: map[string]string{"v-1": "1a"},
host: "",

11
regexp.go

@ -300,14 +300,7 @@ func getHost(r *http.Request) string { @@ -300,14 +300,7 @@ func getHost(r *http.Request) string {
}
func extractVars(input string, matches []int, names []string, output map[string]string) {
matchesCount := 0
prevEnd := -1
for i := 2; i < len(matches) && matchesCount < len(names); i += 2 {
if prevEnd < matches[i+1] {
value := input[matches[i]:matches[i+1]]
output[names[matchesCount]] = value
prevEnd = matches[i+1]
matchesCount++
}
for i, name := range names {
output[name] = input[matches[2*i+2]:matches[2*i+3]]
}
}

Loading…
Cancel
Save