Skip to content

Commit 843d55e

Browse files
authored
Fix GetRoutePattern for subrouters (#115)
1 parent c4ca66b commit 843d55e

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

pkg/zrouter/zmiddlewares/common.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,20 @@ func PathToRegexp(path string) *regexp.Regexp {
2121
}
2222

2323
func GetRoutePattern(r *http.Request) string {
24-
return chi.RouteContext(r.Context()).RoutePattern()
24+
rctx := chi.RouteContext(r.Context())
25+
if pattern := rctx.RoutePattern(); pattern != "" && !strings.HasSuffix(pattern, "*") {
26+
return pattern
27+
}
28+
29+
routePath := r.URL.Path
30+
tctx := chi.NewRouteContext()
31+
if !rctx.Routes.Match(tctx, r.Method, routePath) {
32+
// No matching pattern, so just return the request path.
33+
return routePath
34+
}
35+
36+
// tctx has the updated pattern, since Match mutates it
37+
return tctx.RoutePattern()
2538
}
2639

2740
func getRequestBody(r *http.Request) ([]byte, error) {

pkg/zrouter/zmiddlewares/common_test.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,38 @@ import (
1010
"testing"
1111
)
1212

13-
func TestGetRoutePattern(t *testing.T) {
13+
func TestGetRoutePatternIncludingSubrouters(t *testing.T) {
1414
r := chi.NewRouter()
15+
subRouter := chi.NewRouter()
1516

16-
routePattern := "/test/{param}"
17-
r.Get(routePattern, func(w http.ResponseWriter, r *http.Request) {
17+
// Configure a test route on the subrouter
18+
subRoutePattern := "/sub/{subParam}"
19+
subRouter.Get(subRoutePattern, func(w http.ResponseWriter, r *http.Request) {
1820
routePattern := GetRoutePattern(r)
19-
20-
assert.Equal(t, routePattern, "/test/{param}", "The returned route pattern should match the one configured in the router.")
21+
assert.Equal(t, "/test/sub/{subParam}", routePattern, "The returned route pattern should match the subrouter pattern.")
2122
})
2223

23-
req := httptest.NewRequest("GET", "/test/123", nil)
24-
w := httptest.NewRecorder()
24+
// Mount the subrouter onto a specific path of the main router
25+
r.Mount("/test", subRouter)
26+
27+
// Test request for the subrouter route
28+
reqSub := httptest.NewRequest("GET", "/test/sub/456", nil)
29+
wSub := httptest.NewRecorder()
30+
r.ServeHTTP(wSub, reqSub)
31+
assert.Equal(t, http.StatusOK, wSub.Code, "The expected status code for subrouter should be 200 OK.")
2532

26-
r.ServeHTTP(w, req)
33+
// Configure a test route on the main router
34+
mainRoutePattern := "/main/{mainParam}"
35+
r.Get(mainRoutePattern, func(w http.ResponseWriter, r *http.Request) {
36+
routePattern := GetRoutePattern(r)
37+
assert.Equal(t, "/main/{mainParam}", routePattern, "The returned route pattern should match the main router pattern.")
38+
})
2739

28-
assert.Equal(t, http.StatusOK, w.Code, "The expected status code should be 200 OK.")
40+
// Test request for the main router route
41+
reqMain := httptest.NewRequest("GET", "/main/123", nil)
42+
wMain := httptest.NewRecorder()
43+
r.ServeHTTP(wMain, reqMain)
44+
assert.Equal(t, http.StatusOK, wMain.Code, "The expected status code for main router should be 200 OK.")
2945
}
3046

3147
func TestGetRequestBody(t *testing.T) {

0 commit comments

Comments
 (0)