@@ -34,7 +34,7 @@ func TestPathProcessor(t *testing.T) {
3434testProcess := func (pattern ,uri string ,expectedPathParams map [string ]string ) {
3535chiCtx := chi .NewRouteContext ()
3636chiCtx .RouteMethod = "GET"
37- p := newRouterPathMatcher ("GET" ,pattern ,http .NotFound )
37+ p := newRouterPathMatcher ("GET" ,patternRegexp ( pattern ) ,http .NotFound )
3838assert .True (t ,p .matchPath (chiCtx ,uri ),"use pattern %s to process uri %s" ,pattern ,uri )
3939assert .Equal (t ,expectedPathParams ,chiURLParamsToMap (chiCtx ),"use pattern %s to process uri %s" ,pattern ,uri )
4040}
@@ -56,18 +56,20 @@ func TestRouter(t *testing.T) {
5656recorder .Body = buff
5757
5858type resultStruct struct {
59- method string
60- pathParams map [string ]string
61- handlerMark string
59+ method string
60+ pathParams map [string ]string
61+ handlerMarks [] string
6262}
63- var res resultStruct
6463
64+ var res resultStruct
6565h := func (optMark ... string )func (resp http.ResponseWriter ,req * http.Request ) {
6666mark := util .OptionalArg (optMark ,"" )
6767return func (resp http.ResponseWriter ,req * http.Request ) {
6868res .method = req .Method
6969res .pathParams = chiURLParamsToMap (chi .RouteContext (req .Context ()))
70- res .handlerMark = mark
70+ if mark != "" {
71+ res .handlerMarks = append (res .handlerMarks ,mark )
72+ }
7173}
7274}
7375
@@ -77,6 +79,8 @@ func TestRouter(t *testing.T) {
7779if stop := req .FormValue ("stop" );stop != "" && (mark == "" || mark == stop ) {
7880h (stop )(resp ,req )
7981resp .WriteHeader (http .StatusOK )
82+ }else if mark != "" {
83+ res .handlerMarks = append (res .handlerMarks ,mark )
8084}
8185}
8286}
@@ -108,7 +112,7 @@ func TestRouter(t *testing.T) {
108112m .Delete ("" ,h ())
109113})
110114m .PathGroup ("/*" ,func (g * RouterPathGroup ) {
111- g .MatchPath ("GET" ,`/<dir:*>/<file:[a-z]{1,2}>` ,stopMark ("s2" ),h ("match-path" ))
115+ g .MatchPattern ("GET" ,g . PatternRegexp ( `/<dir:*>/<file:[a-z]{1,2}>` ,stopMark ("s2" )), stopMark ( "s3 " ),h ("match-path" ))
112116},stopMark ("s1" ))
113117})
114118})
@@ -126,31 +130,31 @@ func TestRouter(t *testing.T) {
126130}
127131
128132t .Run ("RootRouter" ,func (t * testing.T ) {
129- testRoute (t ,"GET /the-user/the-repo/other" ,resultStruct {method :"GET" ,handlerMark : "not-found:/" })
133+ testRoute (t ,"GET /the-user/the-repo/other" ,resultStruct {method :"GET" ,handlerMarks : [] string { "not-found:/" } })
130134testRoute (t ,"GET /the-user/the-repo/pulls" ,resultStruct {
131- method :"GET" ,
132- pathParams :map [string ]string {"username" :"the-user" ,"reponame" :"the-repo" ,"type" :"pulls" },
133- handlerMark : "list-issues-b" ,
135+ method :"GET" ,
136+ pathParams :map [string ]string {"username" :"the-user" ,"reponame" :"the-repo" ,"type" :"pulls" },
137+ handlerMarks : [] string { "list-issues-b" } ,
134138})
135139testRoute (t ,"GET /the-user/the-repo/issues/123" ,resultStruct {
136- method :"GET" ,
137- pathParams :map [string ]string {"username" :"the-user" ,"reponame" :"the-repo" ,"type" :"issues" ,"index" :"123" },
138- handlerMark : "view-issue" ,
140+ method :"GET" ,
141+ pathParams :map [string ]string {"username" :"the-user" ,"reponame" :"the-repo" ,"type" :"issues" ,"index" :"123" },
142+ handlerMarks : [] string { "view-issue" } ,
139143})
140144testRoute (t ,"GET /the-user/the-repo/issues/123?stop=hijack" ,resultStruct {
141- method :"GET" ,
142- pathParams :map [string ]string {"username" :"the-user" ,"reponame" :"the-repo" ,"type" :"issues" ,"index" :"123" },
143- handlerMark : "hijack" ,
145+ method :"GET" ,
146+ pathParams :map [string ]string {"username" :"the-user" ,"reponame" :"the-repo" ,"type" :"issues" ,"index" :"123" },
147+ handlerMarks : [] string { "hijack" } ,
144148})
145149testRoute (t ,"POST /the-user/the-repo/issues/123/update" ,resultStruct {
146- method :"POST" ,
147- pathParams :map [string ]string {"username" :"the-user" ,"reponame" :"the-repo" ,"index" :"123" },
148- handlerMark : "update-issue" ,
150+ method :"POST" ,
151+ pathParams :map [string ]string {"username" :"the-user" ,"reponame" :"the-repo" ,"index" :"123" },
152+ handlerMarks : [] string { "update-issue" } ,
149153})
150154})
151155
152156t .Run ("Sub Router" ,func (t * testing.T ) {
153- testRoute (t ,"GET /api/v1/other" ,resultStruct {method :"GET" ,handlerMark : "not-found:/api/v1" })
157+ testRoute (t ,"GET /api/v1/other" ,resultStruct {method :"GET" ,handlerMarks : [] string { "not-found:/api/v1" } })
154158testRoute (t ,"GET /api/v1/repos/the-user/the-repo/branches" ,resultStruct {
155159method :"GET" ,
156160pathParams :map [string ]string {"username" :"the-user" ,"reponame" :"the-repo" },
@@ -179,31 +183,37 @@ func TestRouter(t *testing.T) {
179183
180184t .Run ("MatchPath" ,func (t * testing.T ) {
181185testRoute (t ,"GET /api/v1/repos/the-user/the-repo/branches/d1/d2/fn" ,resultStruct {
182- method :"GET" ,
183- pathParams :map [string ]string {"username" :"the-user" ,"reponame" :"the-repo" ,"*" :"d1/d2/fn" ,"dir" :"d1/d2" ,"file" :"fn" },
184- handlerMark : " match-path" ,
186+ method :"GET" ,
187+ pathParams :map [string ]string {"username" :"the-user" ,"reponame" :"the-repo" ,"*" :"d1/d2/fn" ,"dir" :"d1/d2" ,"file" :"fn" },
188+ handlerMarks : [] string { "s1" , "s2" , "s3" , " match-path"} ,
185189})
186190testRoute (t ,"GET /api/v1/repos/the-user/the-repo/branches/d1%2fd2/fn" ,resultStruct {
187- method :"GET" ,
188- pathParams :map [string ]string {"username" :"the-user" ,"reponame" :"the-repo" ,"*" :"d1%2fd2/fn" ,"dir" :"d1%2fd2" ,"file" :"fn" },
189- handlerMark : " match-path" ,
191+ method :"GET" ,
192+ pathParams :map [string ]string {"username" :"the-user" ,"reponame" :"the-repo" ,"*" :"d1%2fd2/fn" ,"dir" :"d1%2fd2" ,"file" :"fn" },
193+ handlerMarks : [] string { "s1" , "s2" , "s3" , " match-path"} ,
190194})
191195testRoute (t ,"GET /api/v1/repos/the-user/the-repo/branches/d1/d2/000" ,resultStruct {
192- method :"GET" ,
193- pathParams :map [string ]string {"reponame" :"the-repo" ,"username" :"the-user" ,"*" :"d1/d2/000" },
194- handlerMark : " not-found:/api/v1" ,
196+ method :"GET" ,
197+ pathParams :map [string ]string {"reponame" :"the-repo" ,"username" :"the-user" ,"*" :"d1/d2/000" },
198+ handlerMarks : [] string { "s1" , " not-found:/api/v1"} ,
195199})
196200
197201testRoute (t ,"GET /api/v1/repos/the-user/the-repo/branches/d1/d2/fn?stop=s1" ,resultStruct {
198- method :"GET" ,
199- pathParams :map [string ]string {"username" :"the-user" ,"reponame" :"the-repo" ,"*" :"d1/d2/fn" },
200- handlerMark : "s1" ,
202+ method :"GET" ,
203+ pathParams :map [string ]string {"username" :"the-user" ,"reponame" :"the-repo" ,"*" :"d1/d2/fn" },
204+ handlerMarks : [] string { "s1" } ,
201205})
202206
203207testRoute (t ,"GET /api/v1/repos/the-user/the-repo/branches/d1/d2/fn?stop=s2" ,resultStruct {
204- method :"GET" ,
205- pathParams :map [string ]string {"username" :"the-user" ,"reponame" :"the-repo" ,"*" :"d1/d2/fn" ,"dir" :"d1/d2" ,"file" :"fn" },
206- handlerMark :"s2" ,
208+ method :"GET" ,
209+ pathParams :map [string ]string {"username" :"the-user" ,"reponame" :"the-repo" ,"*" :"d1/d2/fn" ,"dir" :"d1/d2" ,"file" :"fn" },
210+ handlerMarks : []string {"s1" ,"s2" },
211+ })
212+
213+ 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" },
207217})
208218})
209219}