Skip to content

Commit

Permalink
fix(group): 修正 Group 下的路由同一中间件多次调用的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed May 20, 2024
1 parent 9042fc3 commit a80f1e8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
10 changes: 6 additions & 4 deletions group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ type (
GroupOf[T any] struct {
routers []*routerOf[T]

call mux.CallOf[T]
notFound T
call mux.CallOf[T]
notFound T // 所有路由都找不差时调用的方法,该方法应用的中间件中 router 参数是为空的。
originNotFound T // 这是应用中间件的 notFound
methodNotAllowedBuilder,
optionsBuilder types.BuildNodeHandleOf[T]
options []options.Option
Expand All @@ -45,6 +46,7 @@ func NewOf[T any](call mux.CallOf[T], notFound T, methodNotAllowedBuilder, optio

call: call,
notFound: notFound,
originNotFound: notFound,
methodNotAllowedBuilder: methodNotAllowedBuilder,
optionsBuilder: optionsBuilder,
options: o,
Expand Down Expand Up @@ -73,7 +75,7 @@ func (g *GroupOf[T]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// 新路由会继承 [NewOf] 中指定的参数,其中的 o 可以覆盖由 [NewOf] 中指定的相关参数;
func (g *GroupOf[T]) New(name string, matcher Matcher, o ...mux.Option) *mux.RouterOf[T] {
o = g.mergeOption(o...)
r := mux.NewRouterOf(name, g.call, g.notFound, g.methodNotAllowedBuilder, g.optionsBuilder, o...)
r := mux.NewRouterOf(name, g.call, g.originNotFound, g.methodNotAllowedBuilder, g.optionsBuilder, o...)
g.Add(matcher, r)
return r
}
Expand Down Expand Up @@ -130,8 +132,8 @@ func (g *GroupOf[T]) Use(m ...types.MiddlewareOf[T]) {
for _, r := range g.routers {
r.r.Use(m...)
}
g.notFound = tree.ApplyMiddleware(g.notFound, "", "", "", m...)

g.notFound = tree.ApplyMiddleware(g.notFound, "", "", "", m...)
g.middleware = append(g.middleware, m...)
}

Expand Down
27 changes: 25 additions & 2 deletions group/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,54 @@ func TestGroupOf_Use(t *testing.T) {

h1 := g.New("h1", NewHosts(false, "h1.example.com"))
h1.Get("/posts/5.html", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("h1")) }))

g.Use(tree.BuildTestMiddleware(a, "m1"))
h2 := g.New("h2", NewHosts(false, "h2.example.com"))
h2.Get("/posts/5.html", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("h2")) }))

rest.NewRequest(a, http.MethodGet, "https://h1.example.com/posts/5.html").
Do(g).
Status(http.StatusOK).
StringBody("h1m1")
rest.NewRequest(a, http.MethodGet, "https://h2.example.com/posts/5.html").
Do(g).
Status(http.StatusOK).
StringBody("h2m1")

// h2 中的 notFound
rest.NewRequest(a, http.MethodGet, "https://h2.example.com/not-exists").
Do(g).
Status(http.StatusNotFound).
StringBody("404 page not found\nm1")
// group.notFound
rest.NewRequest(a, http.MethodGet, "https://not-match.example.com/posts/5.html").
Do(g).
BodyFunc(func(a *assert.Assertion, body []byte) { a.Contains(body, "m1") })
Status(http.StatusNotFound).
StringBody("404 page not found\nm1")

// 添加了新的中间件

g.Use(tree.BuildTestMiddleware(a, "m2"))

rest.NewRequest(a, http.MethodGet, "https://h1.example.com/posts/5.html").
Do(g).
Status(http.StatusOK).
StringBody("h1m1m2")
rest.NewRequest(a, http.MethodGet, "https://h2.example.com/posts/5.html").
Do(g).
Status(http.StatusOK).
StringBody("h2m1m2")

// h2 中的 notFound
rest.NewRequest(a, http.MethodGet, "https://h2.example.com/not-exists").
Do(g).
Status(http.StatusNotFound).
StringBody("404 page not found\nm1m2")
// group.notFound
rest.NewRequest(a, http.MethodGet, "https://not-match.example.com/posts/5.html").
Do(g).
BodyFunc(func(a *assert.Assertion, body []byte) { a.Contains(body, "m1m2") })
Status(http.StatusNotFound).
StringBody("404 page not found\nm1m2")
}

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

0 comments on commit a80f1e8

Please sign in to comment.