Skip to content

Commit bc74824

Browse files
authored
🧹 chore: Add FullPath() helper to context (#3837)
1 parent 0edd81d commit bc74824

4 files changed

Lines changed: 103 additions & 0 deletions

File tree

ctx.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ func (c *DefaultCtx) Route() *Route {
317317
return c.route
318318
}
319319

320+
// FullPath returns the matched route path, including any group prefixes.
321+
func (c *DefaultCtx) FullPath() string {
322+
return c.Route().Path
323+
}
324+
320325
// Matched returns true if the current request path was matched by the router.
321326
func (c *DefaultCtx) Matched() bool {
322327
return c.getMatched()

ctx_interface_gen.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ctx_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3653,6 +3653,73 @@ func Test_Ctx_Route(t *testing.T) {
36533653
require.Empty(t, c.Route().Handlers)
36543654
}
36553655

3656+
// go test -run Test_Ctx_FullPath
3657+
func Test_Ctx_FullPath(t *testing.T) {
3658+
t.Parallel()
3659+
3660+
app := New()
3661+
app.Get("/test", func(c Ctx) error {
3662+
require.Equal(t, "/test", c.FullPath())
3663+
return c.SendStatus(StatusOK)
3664+
})
3665+
3666+
resp, err := app.Test(httptest.NewRequest(MethodGet, "/test", nil))
3667+
require.NoError(t, err, "app.Test(req)")
3668+
defer func() { require.NoError(t, resp.Body.Close()) }()
3669+
3670+
require.Equal(t, StatusOK, resp.StatusCode)
3671+
}
3672+
3673+
// go test -run Test_Ctx_FullPath_Group
3674+
func Test_Ctx_FullPath_Group(t *testing.T) {
3675+
t.Parallel()
3676+
3677+
app := New()
3678+
group := app.Group("/v1")
3679+
group.Get("/test", func(c Ctx) error {
3680+
require.Equal(t, "/v1/test", c.FullPath())
3681+
return c.SendStatus(StatusOK)
3682+
})
3683+
3684+
resp, err := app.Test(httptest.NewRequest(MethodGet, "/v1/test", nil))
3685+
require.NoError(t, err, "app.Test(req)")
3686+
defer func() { require.NoError(t, resp.Body.Close()) }()
3687+
3688+
require.Equal(t, StatusOK, resp.StatusCode)
3689+
}
3690+
3691+
// go test -run Test_Ctx_FullPath_Middleware
3692+
func Test_Ctx_FullPath_Middleware(t *testing.T) {
3693+
t.Parallel()
3694+
3695+
app := New()
3696+
3697+
var recorded []string
3698+
3699+
app.Use(func(c Ctx) error {
3700+
recorded = append(recorded, c.FullPath())
3701+
3702+
if err := c.Next(); err != nil {
3703+
return err
3704+
}
3705+
3706+
recorded = append(recorded, c.FullPath())
3707+
return nil
3708+
})
3709+
3710+
app.Get("/test", func(c Ctx) error {
3711+
require.Equal(t, "/test", c.FullPath())
3712+
return c.SendStatus(StatusOK)
3713+
})
3714+
3715+
resp, err := app.Test(httptest.NewRequest(MethodGet, "/test", nil))
3716+
require.NoError(t, err, "app.Test(req)")
3717+
defer func() { require.NoError(t, resp.Body.Close()) }()
3718+
3719+
require.Equal(t, StatusOK, resp.StatusCode)
3720+
require.Equal(t, []string{"/", "/test"}, recorded)
3721+
}
3722+
36563723
// go test -run Test_Ctx_RouteNormalized
36573724
func Test_Ctx_RouteNormalized(t *testing.T) {
36583725
t.Parallel()

docs/api/ctx.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,35 @@ func MyMiddleware() fiber.Handler {
420420
}
421421
```
422422

423+
### FullPath
424+
425+
Returns the full path of the matched route. This includes any prefixes that were added by [groups](../guide/routing.md#grouping-routes) or mounts.
426+
427+
```go title="Signature"
428+
func (c fiber.Ctx) FullPath() string
429+
```
430+
431+
```go title="Example"
432+
api := app.Group("/api")
433+
api.Get("/users/:id", func(c fiber.Ctx) error {
434+
return c.JSON(fiber.Map{
435+
"route": c.FullPath(), // "/api/users/:id"
436+
})
437+
})
438+
439+
app.Use(func(c fiber.Ctx) error {
440+
beforeNext := c.FullPath() // "/"
441+
442+
if err := c.Next(); err != nil {
443+
return err
444+
}
445+
446+
afterNext := c.FullPath() // "/api/users/:id"
447+
// ... react to the downstream handler's route path
448+
return nil
449+
})
450+
```
451+
423452
### Matched
424453

425454
Returns `true` if the current request path was matched by the router.

0 commit comments

Comments
 (0)