Skip to content

Commit

Permalink
Fix GetRoutePattern for subrouters (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslopezf committed Apr 12, 2024
1 parent c4ca66b commit 843d55e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
15 changes: 14 additions & 1 deletion pkg/zrouter/zmiddlewares/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,20 @@ func PathToRegexp(path string) *regexp.Regexp {
}

func GetRoutePattern(r *http.Request) string {
return chi.RouteContext(r.Context()).RoutePattern()
rctx := chi.RouteContext(r.Context())
if pattern := rctx.RoutePattern(); pattern != "" && !strings.HasSuffix(pattern, "*") {
return pattern
}

routePath := r.URL.Path
tctx := chi.NewRouteContext()
if !rctx.Routes.Match(tctx, r.Method, routePath) {
// No matching pattern, so just return the request path.
return routePath
}

// tctx has the updated pattern, since Match mutates it
return tctx.RoutePattern()
}

func getRequestBody(r *http.Request) ([]byte, error) {
Expand Down
34 changes: 25 additions & 9 deletions pkg/zrouter/zmiddlewares/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,38 @@ import (
"testing"
)

func TestGetRoutePattern(t *testing.T) {
func TestGetRoutePatternIncludingSubrouters(t *testing.T) {
r := chi.NewRouter()
subRouter := chi.NewRouter()

routePattern := "/test/{param}"
r.Get(routePattern, func(w http.ResponseWriter, r *http.Request) {
// Configure a test route on the subrouter
subRoutePattern := "/sub/{subParam}"
subRouter.Get(subRoutePattern, func(w http.ResponseWriter, r *http.Request) {
routePattern := GetRoutePattern(r)

assert.Equal(t, routePattern, "/test/{param}", "The returned route pattern should match the one configured in the router.")
assert.Equal(t, "/test/sub/{subParam}", routePattern, "The returned route pattern should match the subrouter pattern.")
})

req := httptest.NewRequest("GET", "/test/123", nil)
w := httptest.NewRecorder()
// Mount the subrouter onto a specific path of the main router
r.Mount("/test", subRouter)

// Test request for the subrouter route
reqSub := httptest.NewRequest("GET", "/test/sub/456", nil)
wSub := httptest.NewRecorder()
r.ServeHTTP(wSub, reqSub)
assert.Equal(t, http.StatusOK, wSub.Code, "The expected status code for subrouter should be 200 OK.")

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

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

func TestGetRequestBody(t *testing.T) {
Expand Down

0 comments on commit 843d55e

Please sign in to comment.