@@ -56,17 +56,20 @@ func TestRouter(t *testing.T) {
5656 recorder .Body = buff
5757
5858 type resultStruct struct {
59- method string
60- pathParams map [string ]string
61- handlerMarks []string
59+ method string
60+ pathParams map [string ]string
61+ handlerMarks []string
62+ chiRoutePattern * string
6263 }
6364
6465 var res resultStruct
6566 h := func (optMark ... string ) func (resp http.ResponseWriter , req * http.Request ) {
6667 mark := util .OptionalArg (optMark , "" )
6768 return func (resp http.ResponseWriter , req * http.Request ) {
69+ chiCtx := chi .RouteContext (req .Context ())
6870 res .method = req .Method
69- res .pathParams = chiURLParamsToMap (chi .RouteContext (req .Context ()))
71+ res .pathParams = chiURLParamsToMap (chiCtx )
72+ res .chiRoutePattern = util .ToPointer (chiCtx .RoutePattern ())
7073 if mark != "" {
7174 res .handlerMarks = append (res .handlerMarks , mark )
7275 }
@@ -125,21 +128,29 @@ func TestRouter(t *testing.T) {
125128 req , err := http .NewRequest (methodPathFields [0 ], methodPathFields [1 ], nil )
126129 assert .NoError (t , err )
127130 r .ServeHTTP (recorder , req )
131+ if expected .chiRoutePattern == nil {
132+ res .chiRoutePattern = nil
133+ }
128134 assert .Equal (t , expected , res )
129135 })
130136 }
131137
132138 t .Run ("RootRouter" , func (t * testing.T ) {
133- testRoute (t , "GET /the-user/the-repo/other" , resultStruct {method : "GET" , handlerMarks : []string {"not-found:/" }})
139+ testRoute (t , "GET /the-user/the-repo/other" , resultStruct {
140+ method : "GET" ,
141+ handlerMarks : []string {"not-found:/" },
142+ chiRoutePattern : util .ToPointer ("" ),
143+ })
134144 testRoute (t , "GET /the-user/the-repo/pulls" , resultStruct {
135145 method : "GET" ,
136146 pathParams : map [string ]string {"username" : "the-user" , "reponame" : "the-repo" , "type" : "pulls" },
137147 handlerMarks : []string {"list-issues-b" },
138148 })
139149 testRoute (t , "GET /the-user/the-repo/issues/123" , resultStruct {
140- method : "GET" ,
141- pathParams : map [string ]string {"username" : "the-user" , "reponame" : "the-repo" , "type" : "issues" , "index" : "123" },
142- handlerMarks : []string {"view-issue" },
150+ method : "GET" ,
151+ pathParams : map [string ]string {"username" : "the-user" , "reponame" : "the-repo" , "type" : "issues" , "index" : "123" },
152+ handlerMarks : []string {"view-issue" },
153+ chiRoutePattern : util .ToPointer ("/{username}/{reponame}/{type:issues|pulls}/{index}" ),
143154 })
144155 testRoute (t , "GET /the-user/the-repo/issues/123?stop=hijack" , resultStruct {
145156 method : "GET" ,
@@ -154,7 +165,10 @@ func TestRouter(t *testing.T) {
154165 })
155166
156167 t .Run ("Sub Router" , func (t * testing.T ) {
157- testRoute (t , "GET /api/v1/other" , resultStruct {method : "GET" , handlerMarks : []string {"not-found:/api/v1" }})
168+ testRoute (t , "GET /api/v1/other" , resultStruct {
169+ method : "GET" ,
170+ handlerMarks : []string {"not-found:/api/v1" },
171+ })
158172 testRoute (t , "GET /api/v1/repos/the-user/the-repo/branches" , resultStruct {
159173 method : "GET" ,
160174 pathParams : map [string ]string {"username" : "the-user" , "reponame" : "the-repo" },
@@ -211,9 +225,10 @@ func TestRouter(t *testing.T) {
211225 })
212226
213227 testRoute (t , "GET /api/v1/repos/the-user/the-repo/branches/d1/d2/fn?stop=s3" , resultStruct {
214- method : "GET" ,
215- pathParams : map [string ]string {"username" : "the-user" , "reponame" : "the-repo" , "*" : "d1/d2/fn" , "dir" : "d1/d2" , "file" : "fn" },
216- handlerMarks : []string {"s1" , "s2" , "s3" },
228+ method : "GET" ,
229+ pathParams : map [string ]string {"username" : "the-user" , "reponame" : "the-repo" , "*" : "d1/d2/fn" , "dir" : "d1/d2" , "file" : "fn" },
230+ handlerMarks : []string {"s1" , "s2" , "s3" },
231+ chiRoutePattern : util .ToPointer ("/api/v1/repos/{username}/{reponame}/branches/<dir:*>/<file:[a-z]{1,2}>" ),
217232 })
218233 })
219234}
0 commit comments