1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-24 03:16:14 +02:00

Merge branch 'bugfix/1412-route-matchany-multilevel' of github.com:neotel-at/echo into bugfix/1412-route-matchany-multilevel

This commit is contained in:
Roland Lammel 2019-12-27 16:41:31 +01:00
commit 86b200fa0d
6 changed files with 164 additions and 65 deletions

View File

@ -227,7 +227,7 @@ const (
const (
// Version of Echo
Version = "4.1.10"
Version = "4.1.11"
website = "https://echo.labstack.com"
// http://patorjk.com/software/taag/#p=display&f=Small%20Slant&t=Echo
banner = `

View File

@ -92,15 +92,14 @@ func proxyRaw(t *ProxyTarget, c echo.Context) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
in, _, err := c.Response().Hijack()
if err != nil {
c.Error(fmt.Errorf("proxy raw, hijack error=%v, url=%s", t.URL, err))
c.Set("_error", fmt.Sprintf("proxy raw, hijack error=%v, url=%s", t.URL, err))
return
}
defer in.Close()
out, err := net.Dial("tcp", t.URL.Host)
if err != nil {
he := echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("proxy raw, dial error=%v, url=%s", t.URL, err))
c.Error(he)
c.Set("_error", echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("proxy raw, dial error=%v, url=%s", t.URL, err)))
return
}
defer out.Close()
@ -108,8 +107,7 @@ func proxyRaw(t *ProxyTarget, c echo.Context) http.Handler {
// Write header
err = r.Write(out)
if err != nil {
he := echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("proxy raw, request header copy error=%v, url=%s", t.URL, err))
c.Error(he)
c.Set("_error", echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("proxy raw, request header copy error=%v, url=%s", t.URL, err)))
return
}
@ -123,7 +121,7 @@ func proxyRaw(t *ProxyTarget, c echo.Context) http.Handler {
go cp(in, out)
err = <-errCh
if err != nil && err != io.EOF {
c.Logger().Errorf("proxy raw, copy body error=%v, url=%s", t.URL, err)
c.Set("_error", fmt.Errorf("proxy raw, copy body error=%v, url=%s", t.URL, err))
}
})
}
@ -251,6 +249,9 @@ func ProxyWithConfig(config ProxyConfig) echo.MiddlewareFunc {
default:
proxyHTTP(tgt, c, config).ServeHTTP(res, req)
}
if e, ok := c.Get("_error").(error); ok {
err = e
}
return
}

View File

@ -17,8 +17,7 @@ func proxyHTTP(tgt *ProxyTarget, c echo.Context, config ProxyConfig) http.Handle
if tgt.Name != "" {
desc = fmt.Sprintf("%s(%s)", tgt.Name, tgt.URL.String())
}
c.Logger().Errorf("remote %s unreachable, could not forward: %v", desc, err)
c.Error(echo.NewHTTPError(http.StatusServiceUnavailable))
c.Set("_error", echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("remote %s unreachable, could not forward: %v", desc, err)))
}
proxy.Transport = config.Transport
return proxy

View File

@ -49,5 +49,5 @@ func TestProxy_1_11(t *testing.T) {
req.URL.Path = "/api/users"
e.ServeHTTP(rec, req)
assert.Equal(t, "/api/users", req.URL.Path)
assert.Equal(t, http.StatusServiceUnavailable, rec.Code)
assert.Equal(t, http.StatusBadGateway, rec.Code)
}

View File

@ -136,6 +136,11 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
// Split node
n := newNode(cn.kind, cn.prefix[l:], cn, cn.children, cn.methodHandler, cn.ppath, cn.pnames)
// Update parent path for all children to new node
for _, child := range cn.children {
child.parent = n
}
// Reset parent node
cn.kind = skind
cn.label = cn.prefix[0]
@ -417,6 +422,10 @@ func (r *Router) Find(method, path string, c Context) {
if np == nil {
break // no further parent nodes in tree, abort
}
var str strings.Builder
str.WriteString(nn.prefix)
str.WriteString(search)
search = str.String()
nn = np
}
if cn != nil { // use the found "any" route and update path

View File

@ -499,6 +499,15 @@ var (
{"GET", "/people/:userId/moments/:collection", ""},
{"DELETE", "/moments/:id", ""},
}
// handlerHelper created a function that will set a context key for assertion
handlerHelper = func(key string, value int) func(c Context) error {
return func(c Context) error {
c.Set(key, value)
c.Set("path", c.Path())
return nil
}
}
)
func TestRouterStatic(t *testing.T) {
@ -631,6 +640,56 @@ func TestRouterMatchAnyMultiLevel(t *testing.T) {
assert.Equal(t, "/*", c.Get("path"))
assert.Equal(t, "noapi/users/jim", c.Param("*"))
}
func TestRouterMatchAnyMultiLevelWithPost(t *testing.T) {
e := New()
r := e.router
handler := func(c Context) error {
c.Set("path", c.Path())
return nil
}
// Routes
e.POST("/api/auth/login", handler)
e.POST("/api/auth/forgotPassword", handler)
e.Any("/api/*", handler)
e.Any("/*", handler)
// POST /api/auth/login shall choose login method
c := e.NewContext(nil, nil).(*context)
r.Find(http.MethodPost, "/api/auth/login", c)
c.handler(c)
assert.Equal(t, "/api/auth/login", c.Get("path"))
assert.Equal(t, "", c.Param("*"))
// GET /api/auth/login shall choose any route
// c = e.NewContext(nil, nil).(*context)
// r.Find(http.MethodGet, "/api/auth/login", c)
// c.handler(c)
// assert.Equal(t, "/api/*", c.Get("path"))
// assert.Equal(t, "auth/login", c.Param("*"))
// POST /api/auth/logout shall choose nearest any route
c = e.NewContext(nil, nil).(*context)
r.Find(http.MethodPost, "/api/auth/logout", c)
c.handler(c)
assert.Equal(t, "/api/*", c.Get("path"))
assert.Equal(t, "auth/logout", c.Param("*"))
// POST to /api/other/test shall choose nearest any route
c = e.NewContext(nil, nil).(*context)
r.Find(http.MethodPost, "/api/other/test", c)
c.handler(c)
assert.Equal(t, "/api/*", c.Get("path"))
assert.Equal(t, "other/test", c.Param("*"))
// GET to /api/other/test shall choose nearest any route
c = e.NewContext(nil, nil).(*context)
r.Find(http.MethodGet, "/api/other/test", c)
c.handler(c)
assert.Equal(t, "/api/*", c.Get("path"))
assert.Equal(t, "other/test", c.Param("*"))
}
func TestRouterMicroParam(t *testing.T) {
e := New()
@ -695,74 +754,69 @@ func TestRouterPriority(t *testing.T) {
r := e.router
// Routes
r.Add(http.MethodGet, "/users", func(c Context) error {
c.Set("a", 1)
return nil
})
r.Add(http.MethodGet, "/users/new", func(c Context) error {
c.Set("b", 2)
return nil
})
r.Add(http.MethodGet, "/users/:id", func(c Context) error {
c.Set("c", 3)
return nil
})
r.Add(http.MethodGet, "/users/dew", func(c Context) error {
c.Set("d", 4)
return nil
})
r.Add(http.MethodGet, "/users/:id/files", func(c Context) error {
c.Set("e", 5)
return nil
})
r.Add(http.MethodGet, "/users/newsee", func(c Context) error {
c.Set("f", 6)
return nil
})
r.Add(http.MethodGet, "/users/*", func(c Context) error {
c.Set("g", 7)
return nil
})
r.Add(http.MethodGet, "/users/new/*", func(c Context) error {
c.Set("h", 8)
return nil
})
r.Add(http.MethodGet, "/*", func(c Context) error {
c.Set("i", 9)
return nil
})
c := e.NewContext(nil, nil).(*context)
r.Add(http.MethodGet, "/users", handlerHelper("a", 1))
r.Add(http.MethodGet, "/users/new", handlerHelper("b", 2))
r.Add(http.MethodGet, "/users/:id", handlerHelper("c", 3))
r.Add(http.MethodGet, "/users/dew", handlerHelper("d", 4))
r.Add(http.MethodGet, "/users/:id/files", handlerHelper("e", 5))
r.Add(http.MethodGet, "/users/newsee", handlerHelper("f", 6))
r.Add(http.MethodGet, "/users/*", handlerHelper("g", 7))
r.Add(http.MethodGet, "/users/new/*", handlerHelper("h", 8))
r.Add(http.MethodGet, "/*", handlerHelper("i", 9))
// Route > /users
c := e.NewContext(nil, nil).(*context)
r.Find(http.MethodGet, "/users", c)
c.handler(c)
assert.Equal(t, 1, c.Get("a"))
assert.Equal(t, "/users", c.Get("path"))
// Route > /users/new
c = e.NewContext(nil, nil).(*context)
r.Find(http.MethodGet, "/users/new", c)
c.handler(c)
assert.Equal(t, 2, c.Get("b"))
assert.Equal(t, "/users/new", c.Get("path"))
// Route > /users/:id
c = e.NewContext(nil, nil).(*context)
r.Find(http.MethodGet, "/users/1", c)
c.handler(c)
assert.Equal(t, 3, c.Get("c"))
assert.Equal(t, "/users/:id", c.Get("path"))
// Route > /users/dew
c = e.NewContext(nil, nil).(*context)
r.Find(http.MethodGet, "/users/dew", c)
c.handler(c)
assert.Equal(t, 4, c.Get("d"))
assert.Equal(t, "/users/dew", c.Get("path"))
// Route > /users/:id/files
c = e.NewContext(nil, nil).(*context)
r.Find(http.MethodGet, "/users/1/files", c)
c.handler(c)
assert.Equal(t, 5, c.Get("e"))
assert.Equal(t, "/users/:id/files", c.Get("path"))
// Route > /users/:id
c = e.NewContext(nil, nil).(*context)
r.Find(http.MethodGet, "/users/news", c)
c.handler(c)
assert.Equal(t, 3, c.Get("c"))
assert.Equal(t, "/users/:id", c.Get("path"))
// Route > /users/newsee
c = e.NewContext(nil, nil).(*context)
r.Find(http.MethodGet, "/users/newsee", c)
c.handler(c)
assert.Equal(t, 6, c.Get("f"))
assert.Equal(t, "/users/newsee", c.Get("path"))
// Route > /users/newsee
r.Find(http.MethodGet, "/users/newsee", c)
c.handler(c)
assert.Equal(t, 6, c.Get("f"))
// Route > /users/newsee
r.Find(http.MethodGet, "/users/newsee", c)
@ -770,39 +824,52 @@ func TestRouterPriority(t *testing.T) {
assert.Equal(t, 6, c.Get("f"))
// Route > /users/*
c = e.NewContext(nil, nil).(*context)
r.Find(http.MethodGet, "/users/joe/books", c)
c.handler(c)
assert.Equal(t, 7, c.Get("g"))
assert.Equal(t, "/users/*", c.Get("path"))
assert.Equal(t, "joe/books", c.Param("*"))
// Route > /users/new/* should be matched
c = e.NewContext(nil, nil).(*context)
r.Find(http.MethodGet, "/users/new/someone", c)
c.handler(c)
assert.Equal(t, 8, c.Get("h"))
assert.Equal(t, "/users/new/*", c.Get("path"))
assert.Equal(t, "someone", c.Param("*"))
// Route > /users/* should be matched although /users/dew exists
c = e.NewContext(nil, nil).(*context)
r.Find(http.MethodGet, "/users/dew/someone", c)
c.handler(c)
assert.Equal(t, 7, c.Get("g"))
assert.Equal(t, "/users/*", c.Get("path"))
assert.Equal(t, "dew/someone", c.Param("*"))
// Route > /users/* should be matched although /users/dew exists
r.Find(http.MethodGet, "/users/notexists/someone/else", c)
c = e.NewContext(nil, nil).(*context)
r.Find(http.MethodGet, "/users/notexists/someone", c)
c.handler(c)
assert.Equal(t, 7, c.Get("g"))
assert.Equal(t, "notexists/someone/else", c.Param("*"))
assert.Equal(t, "/users/*", c.Get("path"))
assert.Equal(t, "notexists/someone", c.Param("*"))
// Route > *
c = e.NewContext(nil, nil).(*context)
r.Find(http.MethodGet, "/nousers", c)
c.handler(c)
assert.Equal(t, 9, c.Get("i"))
assert.Equal(t, "/*", c.Get("path"))
assert.Equal(t, "nousers", c.Param("*"))
// Route > *
c = e.NewContext(nil, nil).(*context)
r.Find(http.MethodGet, "/nousers/new", c)
c.handler(c)
assert.Equal(t, 9, c.Get("i"))
assert.Equal(t, "/*", c.Get("path"))
assert.Equal(t, "nousers/new", c.Param("*"))
}
@ -884,36 +951,59 @@ func TestRouterParamNames(t *testing.T) {
assert.Equal(t, "1", c.Param("fid"))
}
// Issue #623
// Issue #623 and #1406
func TestRouterStaticDynamicConflict(t *testing.T) {
e := New()
r := e.router
r.Add(http.MethodGet, "/dictionary/skills", handlerHelper("a", 1))
r.Add(http.MethodGet, "/dictionary/:name", handlerHelper("b", 2))
r.Add(http.MethodGet, "/users/new", handlerHelper("d", 4))
r.Add(http.MethodGet, "/users/:name", handlerHelper("e", 5))
r.Add(http.MethodGet, "/server", handlerHelper("c", 3))
r.Add(http.MethodGet, "/", handlerHelper("f", 6))
c := e.NewContext(nil, nil)
r.Add(http.MethodGet, "/dictionary/skills", func(c Context) error {
c.Set("a", 1)
return nil
})
r.Add(http.MethodGet, "/dictionary/:name", func(c Context) error {
c.Set("b", 2)
return nil
})
r.Add(http.MethodGet, "/server", func(c Context) error {
c.Set("c", 3)
return nil
})
r.Find(http.MethodGet, "/dictionary/skills", c)
c.Handler()(c)
assert.Equal(t, 1, c.Get("a"))
assert.Equal(t, "/dictionary/skills", c.Get("path"))
c = e.NewContext(nil, nil)
r.Find(http.MethodGet, "/dictionary/skillsnot", c)
c.Handler()(c)
assert.Equal(t, 2, c.Get("b"))
assert.Equal(t, "/dictionary/:name", c.Get("path"))
c = e.NewContext(nil, nil)
r.Find(http.MethodGet, "/dictionary/type", c)
c.Handler()(c)
assert.Equal(t, 2, c.Get("b"))
assert.Equal(t, "/dictionary/:name", c.Get("path"))
c = e.NewContext(nil, nil)
r.Find(http.MethodGet, "/server", c)
c.Handler()(c)
assert.Equal(t, 3, c.Get("c"))
assert.Equal(t, "/server", c.Get("path"))
c = e.NewContext(nil, nil)
r.Find(http.MethodGet, "/users/new", c)
c.Handler()(c)
assert.Equal(t, 4, c.Get("d"))
assert.Equal(t, "/users/new", c.Get("path"))
c = e.NewContext(nil, nil)
r.Find(http.MethodGet, "/users/new2", c)
c.Handler()(c)
assert.Equal(t, 5, c.Get("e"))
assert.Equal(t, "/users/:name", c.Get("path"))
c = e.NewContext(nil, nil)
r.Find(http.MethodGet, "/", c)
c.Handler()(c)
assert.Equal(t, 6, c.Get("f"))
assert.Equal(t, "/", c.Get("path"))
}
// Issue #1348