1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-12 01:22:21 +02:00

Added echo.HandlerFunc to handlers

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-04-26 22:41:41 -07:00
parent ff75c9c907
commit dd9bc2de9c
4 changed files with 33 additions and 18 deletions

View File

@ -16,8 +16,9 @@ Echo is a fast HTTP router (zero memory allocation) and micro web framework in G
- `func(http.ResponseWriter, *http.Request)`
- `func(http.ResponseWriter, *http.Request) error`
- Handler
- `func(*echo.Context)`
- `echo.HandlerFunc`
- `func(*echo.Context) error`
- `func(*echo.Context)`
- `http.Handler`
- `http.HandlerFunc`
- `func(http.ResponseWriter, *http.Request)`
@ -110,7 +111,7 @@ func main() {
## Contribute
**Use issues for everything**
- Report problems
- Discuss before sending pull request
- Suggest new features

View File

@ -398,13 +398,15 @@ func wrapM(m Middleware) MiddlewareFunc {
// wraps Handler
func wrapH(h Handler) HandlerFunc {
switch h := h.(type) {
case HandlerFunc:
return h
case func(*Context) error:
return h
case func(*Context):
return func(c *Context) error {
h(c)
return nil
}
case func(*Context) error:
return h
case http.Handler, http.HandlerFunc:
return func(c *Context) error {
h.(http.Handler).ServeHTTP(c.Response, c.Request)

View File

@ -121,7 +121,7 @@ func TestEchoMiddleware(t *testing.T) {
func TestEchoHandler(t *testing.T) {
e := New()
// func(*echo.Context)
// func(*echo.Context) error
e.Get("/1", func(c *Context) {
c.String(http.StatusOK, "1")
})
@ -132,7 +132,7 @@ func TestEchoHandler(t *testing.T) {
t.Error("body should be 1")
}
// func(*echo.Context) error
// HandlerFunc
e.Get("/2", func(c *Context) {
c.String(http.StatusOK, "2")
})
@ -143,10 +143,10 @@ func TestEchoHandler(t *testing.T) {
t.Error("body should be 2")
}
// http.Handler/http.HandlerFunc
e.Get("/3", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("3"))
}))
// func(*echo.Context)
e.Get("/3", func(c *Context) {
c.String(http.StatusOK, "3")
})
w = httptest.NewRecorder()
r, _ = http.NewRequest(GET, "/3", nil)
e.ServeHTTP(w, r)
@ -154,10 +154,10 @@ func TestEchoHandler(t *testing.T) {
t.Error("body should be 3")
}
// func(http.ResponseWriter, *http.Request)
e.Get("/4", func(w http.ResponseWriter, r *http.Request) {
// http.Handler/http.HandlerFunc
e.Get("/4", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("4"))
})
}))
w = httptest.NewRecorder()
r, _ = http.NewRequest(GET, "/4", nil)
e.ServeHTTP(w, r)
@ -165,10 +165,9 @@ func TestEchoHandler(t *testing.T) {
t.Error("body should be 4")
}
// func(http.ResponseWriter, *http.Request) error
e.Get("/5", func(w http.ResponseWriter, r *http.Request) error {
// func(http.ResponseWriter, *http.Request)
e.Get("/5", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("5"))
return nil
})
w = httptest.NewRecorder()
r, _ = http.NewRequest(GET, "/5", nil)
@ -176,6 +175,18 @@ func TestEchoHandler(t *testing.T) {
if w.Body.String() != "5" {
t.Error("body should be 5")
}
// func(http.ResponseWriter, *http.Request) error
e.Get("/6", func(w http.ResponseWriter, r *http.Request) error {
w.Write([]byte("6"))
return nil
})
w = httptest.NewRecorder()
r, _ = http.NewRequest(GET, "/6", nil)
e.ServeHTTP(w, r)
if w.Body.String() != "6" {
t.Error("body should be 6")
}
}
func TestEchoGroup(t *testing.T) {

View File

@ -22,8 +22,9 @@ Echo is a fast HTTP router (zero memory allocation) and micro web framework in G
- `func(http.ResponseWriter, *http.Request)`
- `func(http.ResponseWriter, *http.Request) error`
- Handler
- `func(*echo.Context)`
- `echo.HandlerFunc`
- `func(*echo.Context) error`
- `func(*echo.Context)`
- `http.Handler`
- `http.HandlerFunc`
- `func(http.ResponseWriter, *http.Request)`
@ -112,7 +113,7 @@ Hello, World! on the page.
## Contribute
**Use issues for everything**
- Report problems
- Discuss before sending pull request
- Suggest new features