@@ -34,7 +34,7 @@ func TestPathProcessor(t *testing.T) {
34
34
testProcess := func (pattern , uri string , expectedPathParams map [string ]string ) {
35
35
chiCtx := chi .NewRouteContext ()
36
36
chiCtx .RouteMethod = "GET"
37
- p := newRouterPathMatcher ("GET" , pattern , http .NotFound )
37
+ p := newRouterPathMatcher ("GET" , patternRegexp ( pattern ) , http .NotFound )
38
38
assert .True (t , p .matchPath (chiCtx , uri ), "use pattern %s to process uri %s" , pattern , uri )
39
39
assert .Equal (t , expectedPathParams , chiURLParamsToMap (chiCtx ), "use pattern %s to process uri %s" , pattern , uri )
40
40
}
@@ -56,18 +56,20 @@ func TestRouter(t *testing.T) {
56
56
recorder .Body = buff
57
57
58
58
type resultStruct struct {
59
- method string
60
- pathParams map [string ]string
61
- handlerMark string
59
+ method string
60
+ pathParams map [string ]string
61
+ handlerMarks [] string
62
62
}
63
- var res resultStruct
64
63
64
+ var res resultStruct
65
65
h := func (optMark ... string ) func (resp http.ResponseWriter , req * http.Request ) {
66
66
mark := util .OptionalArg (optMark , "" )
67
67
return func (resp http.ResponseWriter , req * http.Request ) {
68
68
res .method = req .Method
69
69
res .pathParams = chiURLParamsToMap (chi .RouteContext (req .Context ()))
70
- res .handlerMark = mark
70
+ if mark != "" {
71
+ res .handlerMarks = append (res .handlerMarks , mark )
72
+ }
71
73
}
72
74
}
73
75
@@ -77,6 +79,8 @@ func TestRouter(t *testing.T) {
77
79
if stop := req .FormValue ("stop" ); stop != "" && (mark == "" || mark == stop ) {
78
80
h (stop )(resp , req )
79
81
resp .WriteHeader (http .StatusOK )
82
+ } else if mark != "" {
83
+ res .handlerMarks = append (res .handlerMarks , mark )
80
84
}
81
85
}
82
86
}
@@ -108,7 +112,7 @@ func TestRouter(t *testing.T) {
108
112
m .Delete ("" , h ())
109
113
})
110
114
m .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" ))
112
116
}, stopMark ("s1" ))
113
117
})
114
118
})
@@ -126,31 +130,31 @@ func TestRouter(t *testing.T) {
126
130
}
127
131
128
132
t .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:/" } })
130
134
testRoute (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" } ,
134
138
})
135
139
testRoute (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" } ,
139
143
})
140
144
testRoute (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" } ,
144
148
})
145
149
testRoute (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" } ,
149
153
})
150
154
})
151
155
152
156
t .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" } })
154
158
testRoute (t , "GET /api/v1/repos/the-user/the-repo/branches" , resultStruct {
155
159
method : "GET" ,
156
160
pathParams : map [string ]string {"username" : "the-user" , "reponame" : "the-repo" },
@@ -179,31 +183,37 @@ func TestRouter(t *testing.T) {
179
183
180
184
t .Run ("MatchPath" , func (t * testing.T ) {
181
185
testRoute (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"} ,
185
189
})
186
190
testRoute (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"} ,
190
194
})
191
195
testRoute (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"} ,
195
199
})
196
200
197
201
testRoute (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" } ,
201
205
})
202
206
203
207
testRoute (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" },
207
217
})
208
218
})
209
219
}
0 commit comments