export pathvar for user-defined routers (#911)

* refactor

* export pathvar for user-defined routers
This commit is contained in:
Kevin Wan
2021-08-14 22:57:28 +08:00
committed by GitHub
parent fbf2eebc42
commit fc04ad7854
6 changed files with 23 additions and 23 deletions

View File

@@ -8,7 +8,7 @@ import (
"github.com/tal-tech/go-zero/core/search"
"github.com/tal-tech/go-zero/rest/httpx"
"github.com/tal-tech/go-zero/rest/internal/context"
"github.com/tal-tech/go-zero/rest/pathvar"
)
const (
@@ -61,7 +61,7 @@ func (pr *patRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if tree, ok := pr.trees[r.Method]; ok {
if result, ok := tree.Search(reqPath); ok {
if len(result.Params) > 0 {
r = context.WithPathVars(r, result.Params)
r = pathvar.WithVars(r, result.Params)
}
result.Item.(http.Handler).ServeHTTP(w, r)
return

View File

@@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/rest/httpx"
"github.com/tal-tech/go-zero/rest/internal/context"
"github.com/tal-tech/go-zero/rest/pathvar"
)
const (
@@ -107,12 +107,12 @@ func TestPatRouter(t *testing.T) {
router := NewRouter()
err := router.Handle(test.method, "/a/:b", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
routed = true
assert.Equal(t, 1, len(context.Vars(r)))
assert.Equal(t, 1, len(pathvar.Vars(r)))
}))
assert.Nil(t, err)
err = router.Handle(test.method, "/a/b/c", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
routed = true
assert.Nil(t, context.Vars(r))
assert.Nil(t, pathvar.Vars(r))
}))
assert.Nil(t, err)
err = router.Handle(test.method, "/b/c", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {