Skip to content

Commit 09ccfba

Browse files
committed
Fill c.Request().Pattern field with route path to help standard library based middlewares.
1 parent 68aaf3a commit 09ccfba

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

router.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,8 +1039,8 @@ func (r *DefaultRouter) Route(c *Context) HandlerFunc {
10391039
}
10401040

10411041
c.InitializeRoute(rInfo, &pathValues)
1042-
c.SetPath(rPath) // after InitializeRoute so we would not accidentally change `notFoundRouteInfo` or `methodNotAllowedRouteInfo` Path
1043-
1042+
c.SetPath(rPath) // after InitializeRoute so we would not accidentally change `notFoundRouteInfo` or `methodNotAllowedRouteInfo` Path
1043+
c.request.Pattern = rPath // help standard library based middlewares. This is a deliberate choice not to call `request.SetPathValue` for params.
10441044
return rHandler
10451045
}
10461046

router_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,21 @@ func checkUnusedParamValues(t *testing.T, c *Context, expectParam map[string]str
671671
}
672672
}
673673

674+
func TestRouterFillsRequestPatternField(t *testing.T) {
675+
path := "/folders/a/files/echo.gif"
676+
req := httptest.NewRequest(http.MethodGet, path, nil)
677+
rec := httptest.NewRecorder()
678+
679+
e := New()
680+
e.GET(path, handlerFunc)
681+
682+
c := e.NewContext(req, rec)
683+
_ = e.router.Route(c)
684+
685+
assert.Equal(t, path, c.Path())
686+
assert.Equal(t, path, c.Request().Pattern)
687+
}
688+
674689
func TestRouterStatic(t *testing.T) {
675690
path := "/folders/a/files/echo.gif"
676691
req := httptest.NewRequest(http.MethodGet, path, nil)

0 commit comments

Comments
 (0)