From dd9bc2de9c19a6cc0a2448b19c205afb616d07e9 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Sun, 26 Apr 2015 22:41:41 -0700 Subject: [PATCH] Added echo.HandlerFunc to handlers Signed-off-by: Vishal Rana --- README.md | 5 +++-- echo.go | 6 ++++-- echo_test.go | 35 +++++++++++++++++++++++------------ website/docs/index.md | 5 +++-- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index bcb69d7f..e7da65e1 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/echo.go b/echo.go index f2dbf717..f9e5258d 100644 --- a/echo.go +++ b/echo.go @@ -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) diff --git a/echo_test.go b/echo_test.go index c2a3fc32..4756a2b4 100644 --- a/echo_test.go +++ b/echo_test.go @@ -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) { diff --git a/website/docs/index.md b/website/docs/index.md index feee0148..914019c8 100644 --- a/website/docs/index.md +++ b/website/docs/index.md @@ -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