Skip to content

Commit

Permalink
Merge pull request #409 from ReneWerner87/panic_on_optional_param
Browse files Browse the repository at this point in the history
Panic on static ":param" path #405
  • Loading branch information
Fenny authored May 24, 2020
2 parents a1896b7 + dacec74 commit 73a2906
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
15 changes: 9 additions & 6 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ type Route struct {
}

func (r *Route) match(path, original string) (match bool, values []string) {
// root path check
if r.root && path == "/" {
return true, values
// '*' wildcard matches any path
} else if r.star {
values := getAllocFreeParams(1)
values[0] = original[1:]
return true, values
}
// Does this route have parameters
if len(r.routeParams) > 0 {
// Match params
Expand All @@ -47,12 +56,6 @@ func (r *Route) match(path, original string) (match bool, values []string) {
// Check for a simple path match
} else if len(r.path) == len(path) && r.path == path {
return true, values
} else if r.root && path == "/" {
return true, values
}
// '*' wildcard matches any path
if r.star {
return true, []string{utils.TrimLeft(original, '/')}
}
// No match
return false, values
Expand Down
53 changes: 53 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func Benchmark_Route_Match(b *testing.B) {
star: false,
routeParser: parsed,
routeParams: parsed.params,
path: "/user/keys/:id",

Path: "/user/keys/:id",
Method: "DELETE",
Expand All @@ -286,6 +287,58 @@ func Benchmark_Route_Match(b *testing.B) {
utils.AssertEqual(b, []string{"1337"}, params)
}

// go test -v ./... -run=^$ -bench=Benchmark_Route_Match_Star -benchmem -count=4
func Benchmark_Route_Match_Star(b *testing.B) {
var match bool
var params []string

parsed := parseRoute("/*")
route := &Route{
use: false,
root: false,
star: true,
routeParser: parsed,
routeParams: parsed.params,
path: "/user/keys/bla",

Path: "/user/keys/bla",
Method: "DELETE",
}
route.Handlers = append(route.Handlers, func(c *Ctx) {})
for n := 0; n < b.N; n++ {
match, params = route.match("/user/keys/bla", "/user/keys/bla")
}

utils.AssertEqual(b, true, match)
utils.AssertEqual(b, []string{"user/keys/bla"}, params)
}

// go test -v ./... -run=^$ -bench=Benchmark_Route_Match_Root -benchmem -count=4
func Benchmark_Route_Match_Root(b *testing.B) {
var match bool
var params []string

parsed := parseRoute("/")
route := &Route{
use: false,
root: true,
star: false,
path: "/",
routeParser: parsed,
routeParams: parsed.params,

Path: "/",
Method: "DELETE",
}
route.Handlers = append(route.Handlers, func(c *Ctx) {})
for n := 0; n < b.N; n++ {
match, params = route.match("/", "/")
}

utils.AssertEqual(b, true, match)
utils.AssertEqual(b, []string(nil), params)
}

// go test -v ./... -run=^$ -bench=Benchmark_Router_Handler_CaseSensitive -benchmem -count=4
func Benchmark_Router_Handler_CaseSensitive(b *testing.B) {
app := New()
Expand Down

0 comments on commit 73a2906

Please sign in to comment.