From d8c7f04c95043d4a7305dd8f466adaa7e84cd86e Mon Sep 17 00:00:00 2001 From: toim Date: Mon, 2 Feb 2026 08:51:19 +0200 Subject: [PATCH] Fill c.Request().Pattern field with route path to help standard library based middlewares. --- router.go | 4 ++-- router_test.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/router.go b/router.go index 558c9e8e1..0db3bc04c 100644 --- a/router.go +++ b/router.go @@ -1039,8 +1039,8 @@ func (r *DefaultRouter) Route(c *Context) HandlerFunc { } c.InitializeRoute(rInfo, &pathValues) - c.SetPath(rPath) // after InitializeRoute so we would not accidentally change `notFoundRouteInfo` or `methodNotAllowedRouteInfo` Path - + c.SetPath(rPath) // after InitializeRoute so we would not accidentally change `notFoundRouteInfo` or `methodNotAllowedRouteInfo` Path + c.request.Pattern = rPath // help standard library based middlewares. This is a deliberate choice not to call `request.SetPathValue` for params. return rHandler } diff --git a/router_test.go b/router_test.go index e69512c8b..1dd306a36 100644 --- a/router_test.go +++ b/router_test.go @@ -671,6 +671,21 @@ func checkUnusedParamValues(t *testing.T, c *Context, expectParam map[string]str } } +func TestRouterFillsRequestPatternField(t *testing.T) { + path := "/folders/a/files/echo.gif" + req := httptest.NewRequest(http.MethodGet, path, nil) + rec := httptest.NewRecorder() + + e := New() + e.GET(path, handlerFunc) + + c := e.NewContext(req, rec) + _ = e.router.Route(c) + + assert.Equal(t, path, c.Path()) + assert.Equal(t, path, c.Request().Pattern) +} + func TestRouterStatic(t *testing.T) { path := "/folders/a/files/echo.gif" req := httptest.NewRequest(http.MethodGet, path, nil)