Skip to content

Commit 818fdcf

Browse files
authored
fix: set Request.Pattern from RoutePattern() (#1097)
Use the composed RoutePattern() value instead of the per-router routePattern field so nested routes populate http.Request.Pattern correctly on Go 1.23+. Fixes #1089
1 parent f975af0 commit 818fdcf

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

mux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ func (mx *Mux) routeHTTP(w http.ResponseWriter, r *http.Request) {
472472
value := rctx.URLParams.Values[i]
473473
r.SetPathValue(key, value)
474474
}
475-
r.Pattern = rctx.routePattern
475+
r.Pattern = rctx.RoutePattern()
476476

477477
h.ServeHTTP(w, r)
478478
return

pattern_test.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,29 @@ func TestPattern(t *testing.T) {
3434
method: "POST",
3535
requestPath: "/users/Gojo/friends/all-of-them/and/more",
3636
},
37+
{
38+
name: "Nested sub-router",
39+
pattern: "/accounts/{accountID}/hi",
40+
method: "GET",
41+
requestPath: "/accounts/44/hi",
42+
},
3743
}
3844

3945
for _, tc := range testCases {
4046
t.Run(tc.name, func(t *testing.T) {
4147
r := NewRouter()
4248

43-
r.Handle(tc.method+" "+tc.pattern, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
44-
w.Write([]byte(r.Pattern))
45-
}))
49+
if tc.name == "Nested sub-router" {
50+
r.Route("/accounts/{accountID}", func(r Router) {
51+
r.Handle(tc.method+" /hi", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
52+
w.Write([]byte(r.Pattern))
53+
}))
54+
})
55+
} else {
56+
r.Handle(tc.method+" "+tc.pattern, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
57+
w.Write([]byte(r.Pattern))
58+
}))
59+
}
4660

4761
ts := httptest.NewServer(r)
4862
defer ts.Close()

0 commit comments

Comments
 (0)