Skip to content

Commit

Permalink
Make Context.RoutePattern() nil-safe (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaiaz-iusipov committed Aug 23, 2024
1 parent dac67d9 commit 6fedde2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (x *Context) URLParam(key string) string {
// RoutePattern builds the routing pattern string for the particular
// request, at the particular point during routing. This means, the value
// will change throughout the execution of a request in a router. That is
// why its advised to only use this value after calling the next handler.
// why it's advised to only use this value after calling the next handler.
//
// For example,
//
Expand All @@ -121,6 +121,9 @@ func (x *Context) URLParam(key string) string {
// })
// }
func (x *Context) RoutePattern() string {
if x == nil {
return ""
}
routePattern := strings.Join(x.RoutePatterns, "")
routePattern = replaceWildcards(routePattern)
if routePattern != "/" {
Expand Down
6 changes: 6 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,10 @@ func TestRoutePattern(t *testing.T) {
if p := x.RoutePattern(); p != "/" {
t.Fatal("unexpected route pattern for root: " + p)
}

// Testing empty route pattern for nil context
var nilContext *Context
if p := nilContext.RoutePattern(); p != "" {
t.Fatalf("unexpected non-empty route pattern for nil context: %q", p)
}
}

0 comments on commit 6fedde2

Please sign in to comment.