From 6fedde2a70dc2adce0a3dc41b8aebc0b2bec8185 Mon Sep 17 00:00:00 2001 From: Gaiaz Iusipov Date: Fri, 23 Aug 2024 18:23:19 +0400 Subject: [PATCH] Make Context.RoutePattern() nil-safe (#927) --- context.go | 5 ++++- context_test.go | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/context.go b/context.go index e2cd908d..aacf6eff 100644 --- a/context.go +++ b/context.go @@ -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, // @@ -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 != "/" { diff --git a/context_test.go b/context_test.go index 4731c709..fa3c9f5b 100644 --- a/context_test.go +++ b/context_test.go @@ -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) + } }