Skip to content

Commit 561c8c4

Browse files
committed
fix
1 parent 7b414f4 commit 561c8c4

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

modules/web/router_path.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@ type RouterPathGroup struct {
2424
func (g *RouterPathGroup) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
2525
chiCtx := chi.RouteContext(req.Context())
2626
path := chiCtx.URLParam(g.pathParam)
27-
28-
// FIXME: update chi route info and the handler func info?
29-
3027
for _, m := range g.matchers {
3128
if m.matchPath(chiCtx, path) {
29+
chiCtx.RoutePatterns = append(chiCtx.RoutePatterns, m.pattern)
3230
handler := m.handlerFunc
3331
for i := len(m.middlewares) - 1; i >= 0; i-- {
3432
handler = m.middlewares[i](handler).ServeHTTP
@@ -41,6 +39,7 @@ func (g *RouterPathGroup) ServeHTTP(resp http.ResponseWriter, req *http.Request)
4139
}
4240

4341
type RouterPathGroupPattern struct {
42+
pattern string
4443
re *regexp.Regexp
4544
params []routerPathParam
4645
middlewares []any
@@ -65,6 +64,7 @@ type routerPathParam struct {
6564

6665
type routerPathMatcher struct {
6766
methods container.Set[string]
67+
pattern string
6868
re *regexp.Regexp
6969
params []routerPathParam
7070
middlewares []func(http.Handler) http.Handler
@@ -120,7 +120,7 @@ func newRouterPathMatcher(methods string, patternRegexp *RouterPathGroupPattern,
120120
}
121121
p.methods.Add(method)
122122
}
123-
p.re, p.params = patternRegexp.re, patternRegexp.params
123+
p.pattern, p.re, p.params = patternRegexp.pattern, patternRegexp.re, patternRegexp.params
124124
return p
125125
}
126126

@@ -160,7 +160,7 @@ func patternRegexp(pattern string, h ...any) *RouterPathGroupPattern {
160160
p.params = append(p.params, param)
161161
}
162162
re = append(re, '$')
163-
p.re = regexp.MustCompile(string(re))
163+
p.pattern, p.re = pattern, regexp.MustCompile(string(re))
164164
return p
165165
}
166166

modules/web/router_test.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

routers/api/packages/conda/conda.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func ListOrGetPackages(ctx *context.Context) {
5151
DownloadPackageFile(ctx)
5252
return
5353
}
54-
ctx.NotFound(nil)
54+
http.NotFound(ctx.Resp, ctx.Req)
5555
}
5656

5757
func EnumeratePackages(ctx *context.Context) {

0 commit comments

Comments
 (0)