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

Coverage for handler

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-03-29 23:35:08 -07:00
parent c29ae7dd7d
commit e08b3f78e8
5 changed files with 170 additions and 89 deletions

View File

@ -5,14 +5,17 @@ Echo is a fast HTTP router (zero memory allocation) + micro web framework in Go.
- Zippy router. - Zippy router.
- Extensible middleware / handler, supports: - Extensible middleware / handler, supports:
- Middleware - Middleware
- `func(*echo.Context)`
- `func(echo.HandlerFunc) echo.HandlerFunc` - `func(echo.HandlerFunc) echo.HandlerFunc`
- `http.HandlerFunc`
- `http.Handler`
- `func(http.Handler) http.Handler` - `func(http.Handler) http.Handler`
- `http.Handler`
- `http.HandlerFunc`
- `func(http.ResponseWriter, *http.Request)`
- Handler - Handler
- `func(*echo.Context)` - `func(*echo.Context)`
- `http.HandlerFunc`
- `http.Handler` - `http.Handler`
- `http.HandlerFunc`
- `func(http.ResponseWriter, *http.Request)`
- Serve static files, including index. - Serve static files, including index.
### Installatioin ### Installatioin

View File

@ -47,6 +47,13 @@ func (c *Context) Bind(i interface{}) bool {
return true return true
} }
// String writes status and string to the response.
func (c *Context) String(n int, s string) {
c.Response.Header().Set(HeaderContentType, MIMEText+"; charset=utf-8")
c.Response.WriteHeader(n)
c.Response.Write([]byte(s))
}
// JSON writes status and JSON to the response. // JSON writes status and JSON to the response.
func (c *Context) JSON(n int, i interface{}) { func (c *Context) JSON(n int, i interface{}) {
enc := json.NewEncoder(c.Response) enc := json.NewEncoder(c.Response)

View File

@ -3,9 +3,7 @@ package echo
import "testing" import "testing"
func TestContextBind(t *testing.T) { func TestContextBind(t *testing.T) {
} }
func TestContextJSON(t *testing.T) { func TestContextJSON(t *testing.T) {
} }

125
echo.go
View File

@ -24,6 +24,7 @@ type (
const ( const (
MIMEJSON = "application/json" MIMEJSON = "application/json"
MIMEText = "text/plain"
HeaderAccept = "Accept" HeaderAccept = "Accept"
HeaderContentDisposition = "Content-Disposition" HeaderContentDisposition = "Content-Disposition"
@ -32,8 +33,8 @@ const (
) )
// New creates a echo instance. // New creates a echo instance.
func New() (b *Echo) { func New() (e *Echo) {
b = &Echo{ e = &Echo{
maxParam: 5, maxParam: 5,
notFoundHandler: func(c *Context) { notFoundHandler: func(c *Context) {
http.Error(c.Response, http.StatusText(http.StatusNotFound), http.StatusNotFound) http.Error(c.Response, http.StatusText(http.StatusNotFound), http.StatusNotFound)
@ -45,13 +46,13 @@ func New() (b *Echo) {
http.Error(c.Response, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(c.Response, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}, },
} }
b.Router = NewRouter(b) e.Router = NewRouter(e)
b.pool.New = func() interface{} { e.pool.New = func() interface{} {
return &Context{ return &Context{
Response: &response{}, Response: &response{},
params: make(Params, b.maxParam), params: make(Params, e.maxParam),
store: make(store), store: make(store),
echo: b, echo: e,
} }
} }
return return
@ -70,137 +71,151 @@ func (h HandlerFunc) ServeHTTP(r http.ResponseWriter, w *http.Request) {
// MaxParam sets the maximum allowed path parameters. Default is 5, good enough // MaxParam sets the maximum allowed path parameters. Default is 5, good enough
// for many users. // for many users.
func (b *Echo) MaxParam(n uint8) { func (e *Echo) MaxParam(n uint8) {
b.maxParam = n e.maxParam = n
} }
// NotFoundHandler sets a custom NotFound handler. // NotFoundHandler sets a custom NotFound handler.
func (b *Echo) NotFoundHandler(h Handler) { func (e *Echo) NotFoundHandler(h Handler) {
b.notFoundHandler = wrapH(h) e.notFoundHandler = wrapH(h)
} }
// MethodNotAllowedHandler sets a custom MethodNotAllowed handler. // MethodNotAllowedHandler sets a custom MethodNotAllowed handler.
func (b *Echo) MethodNotAllowedHandler(h Handler) { func (e *Echo) MethodNotAllowedHandler(h Handler) {
b.methodNotAllowedHandler = wrapH(h) e.methodNotAllowedHandler = wrapH(h)
} }
// InternalServerErrorHandler sets a custom InternalServerError handler. // InternalServerErrorHandler sets a custom InternalServerError handler.
func (b *Echo) InternalServerErrorHandler(h Handler) { func (e *Echo) InternalServerErrorHandler(h Handler) {
b.internalServerErrorHandler = wrapH(h) e.internalServerErrorHandler = wrapH(h)
} }
// Use adds handler to the middleware chain. // Use adds handler to the middleware chain.
func (b *Echo) Use(m ...Middleware) { func (e *Echo) Use(m ...Middleware) {
for _, h := range m { for _, h := range m {
b.middleware = append(b.middleware, wrapM(h)) e.middleware = append(e.middleware, wrapM(h))
} }
} }
// Connect adds a CONNECT route > handler to the router. // Connect adds a CONNECT route > handler to the router.
func (b *Echo) Connect(path string, h Handler) { func (e *Echo) Connect(path string, h Handler) {
b.Router.Add("CONNECT", path, wrapH(h)) e.Router.Add("CONNECT", path, wrapH(h))
} }
// Delete adds a DELETE route > handler to the router. // Delete adds a DELETE route > handler to the router.
func (b *Echo) Delete(path string, h Handler) { func (e *Echo) Delete(path string, h Handler) {
b.Router.Add("DELETE", path, wrapH(h)) e.Router.Add("DELETE", path, wrapH(h))
} }
// Get adds a GET route > handler to the router. // Get adds a GET route > handler to the router.
func (b *Echo) Get(path string, h Handler) { func (e *Echo) Get(path string, h Handler) {
b.Router.Add("GET", path, wrapH(h)) e.Router.Add("GET", path, wrapH(h))
} }
// Head adds a HEAD route > handler to the router. // Head adds a HEAD route > handler to the router.
func (b *Echo) Head(path string, h Handler) { func (e *Echo) Head(path string, h Handler) {
b.Router.Add("HEAD", path, wrapH(h)) e.Router.Add("HEAD", path, wrapH(h))
} }
// Options adds an OPTIONS route > handler to the router. // Options adds an OPTIONS route > handler to the router.
func (b *Echo) Options(path string, h Handler) { func (e *Echo) Options(path string, h Handler) {
b.Router.Add("OPTIONS", path, wrapH(h)) e.Router.Add("OPTIONS", path, wrapH(h))
} }
// Patch adds a PATCH route > handler to the router. // Patch adds a PATCH route > handler to the router.
func (b *Echo) Patch(path string, h Handler) { func (e *Echo) Patch(path string, h Handler) {
b.Router.Add("PATCH", path, wrapH(h)) e.Router.Add("PATCH", path, wrapH(h))
} }
// Post adds a POST route > handler to the router. // Post adds a POST route > handler to the router.
func (b *Echo) Post(path string, h Handler) { func (e *Echo) Post(path string, h Handler) {
b.Router.Add("POST", path, wrapH(h)) e.Router.Add("POST", path, wrapH(h))
} }
// Put adds a PUT route > handler to the router. // Put adds a PUT route > handler to the router.
func (b *Echo) Put(path string, h Handler) { func (e *Echo) Put(path string, h Handler) {
b.Router.Add("PUT", path, wrapH(h)) e.Router.Add("PUT", path, wrapH(h))
} }
// Trace adds a TRACE route > handler to the router. // Trace adds a TRACE route > handler to the router.
func (b *Echo) Trace(path string, h Handler) { func (e *Echo) Trace(path string, h Handler) {
b.Router.Add("TRACE", path, wrapH(h)) e.Router.Add("TRACE", path, wrapH(h))
} }
// Static serves static files. // Static serves static files.
func (b *Echo) Static(path, root string) { func (e *Echo) Static(path, root string) {
fs := http.StripPrefix(path, http.FileServer(http.Dir(root))) fs := http.StripPrefix(path, http.FileServer(http.Dir(root)))
b.Get(path+"/*", func(c *Context) { e.Get(path+"/*", func(c *Context) {
fs.ServeHTTP(c.Response, c.Request) fs.ServeHTTP(c.Response, c.Request)
}) })
} }
// ServeFile serves a file. // ServeFile serves a file.
func (b *Echo) ServeFile(path, file string) { func (e *Echo) ServeFile(path, file string) {
b.Get(path, func(c *Context) { e.Get(path, func(c *Context) {
http.ServeFile(c.Response, c.Request, file) http.ServeFile(c.Response, c.Request, file)
}) })
} }
// Index serves index file. // Index serves index file.
func (b *Echo) Index(file string) { func (e *Echo) Index(file string) {
b.ServeFile("/", file) e.ServeFile("/", file)
} }
func (b *Echo) ServeHTTP(rw http.ResponseWriter, r *http.Request) { func (e *Echo) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
h, c, s := b.Router.Find(r.Method, r.URL.Path) h, c, s := e.Router.Find(r.Method, r.URL.Path)
c.reset(rw, r) c.reset(rw, r)
if h != nil { if h != nil {
// Middleware // Middleware
for i := len(b.middleware) - 1; i >= 0; i-- { for i := len(e.middleware) - 1; i >= 0; i-- {
h = b.middleware[i](h) h = e.middleware[i](h)
} }
// Handler // Handler
h(c) h(c)
} else { } else {
if s == NotFound { if s == NotFound {
b.notFoundHandler(c) e.notFoundHandler(c)
} else if s == NotAllowed { } else if s == NotAllowed {
b.methodNotAllowedHandler(c) e.methodNotAllowedHandler(c)
} }
} }
b.pool.Put(c) e.pool.Put(c)
} }
func (b *Echo) Run(addr string) { func (e *Echo) Run(addr string) {
log.Fatal(http.ListenAndServe(addr, b)) log.Fatal(http.ListenAndServe(addr, e))
} }
// wraps Middleware // wraps Middleware
func wrapM(m Middleware) MiddlewareFunc { func wrapM(m Middleware) MiddlewareFunc {
switch m := m.(type) { switch m := m.(type) {
case func(*Context):
return func(h HandlerFunc) HandlerFunc {
return func(c *Context) {
m(c)
h(c)
}
}
case func(HandlerFunc) HandlerFunc: case func(HandlerFunc) HandlerFunc:
return MiddlewareFunc(m) return MiddlewareFunc(m)
case http.HandlerFunc, http.Handler: case func(http.Handler) http.Handler:
return func(h HandlerFunc) HandlerFunc {
return func(c *Context) {
m(h).ServeHTTP(c.Response, c.Request)
h(c)
}
}
case http.Handler, http.HandlerFunc:
return func(h HandlerFunc) HandlerFunc { return func(h HandlerFunc) HandlerFunc {
return func(c *Context) { return func(c *Context) {
m.(http.Handler).ServeHTTP(c.Response, c.Request) m.(http.Handler).ServeHTTP(c.Response, c.Request)
h(c) h(c)
} }
} }
case func(http.Handler) http.Handler: case func(http.ResponseWriter, *http.Request):
return func(h HandlerFunc) HandlerFunc { return func(h HandlerFunc) HandlerFunc {
return func(c *Context) { return func(c *Context) {
m(h).ServeHTTP(c.Response, c.Request) m(c.Response, c.Request)
h(c) h(c)
} }
} }
@ -214,10 +229,14 @@ func wrapH(h Handler) HandlerFunc {
switch h := h.(type) { switch h := h.(type) {
case func(*Context): case func(*Context):
return HandlerFunc(h) return HandlerFunc(h)
case http.HandlerFunc, func(http.ResponseWriter, *http.Request), http.Handler: case http.Handler, http.HandlerFunc:
return func(c *Context) { return func(c *Context) {
h.(http.Handler).ServeHTTP(c.Response, c.Request) h.(http.Handler).ServeHTTP(c.Response, c.Request)
} }
case func(http.ResponseWriter, *http.Request):
return func(c *Context) {
h(c.Response, c.Request)
}
default: default:
panic("echo: unknown handler") panic("echo: unknown handler")
} }

View File

@ -21,41 +21,47 @@ var u = user{
Name: "Joe", Name: "Joe",
} }
// TODO: Fix me
func TestEchoMaxParam(t *testing.T) { func TestEchoMaxParam(t *testing.T) {
b := New() e := New()
b.MaxParam(8) e.MaxParam(8)
if b.maxParam != 8 { if e.maxParam != 8 {
t.Errorf("max param should be 8, found %d", b.maxParam) t.Errorf("max param should be 8, found %d", e.maxParam)
} }
} }
func TestEchoIndex(t *testing.T) { func TestEchoIndex(t *testing.T) {
b := New() e := New()
b.Index("example/public/index.html") e.Index("example/public/index.html")
w := httptest.NewRecorder() w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil) r, _ := http.NewRequest("GET", "/", nil)
b.ServeHTTP(w, r) e.ServeHTTP(w, r)
if w.Code != 200 { if w.Code != 200 {
t.Errorf("status code should be 200, found %d", w.Code) t.Errorf("status code should be 200, found %d", w.Code)
} }
} }
func TestEchoStatic(t *testing.T) { func TestEchoStatic(t *testing.T) {
b := New() e := New()
b.Static("/js", "example/public/js") e.Static("/js", "example/public/js")
w := httptest.NewRecorder() w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/js/main.js", nil) r, _ := http.NewRequest("GET", "/js/main.js", nil)
b.ServeHTTP(w, r) e.ServeHTTP(w, r)
if w.Code != 200 { if w.Code != 200 {
t.Errorf("status code should be 200, found %d", w.Code) t.Errorf("status code should be 200, found %d", w.Code)
} }
} }
func TestEchoMiddleware(t *testing.T) { func TestEchoMiddleware(t *testing.T) {
b := New() e := New()
// func(HandlerFunc) HandlerFunc // func(*echo.Context)
b.Use(func(h HandlerFunc) HandlerFunc { e.Use(func(c *Context) {
c.Request.Header.Set("e", "5")
})
// func(echo.HandlerFunc) echo.HandlerFunc
e.Use(func(h HandlerFunc) HandlerFunc {
return HandlerFunc(func(c *Context) { return HandlerFunc(func(c *Context) {
c.Request.Header.Set("a", "1") c.Request.Header.Set("a", "1")
h(c) h(c)
@ -63,45 +69,93 @@ func TestEchoMiddleware(t *testing.T) {
}) })
// http.HandlerFunc // http.HandlerFunc
b.Use(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { e.Use(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Set("b", "2") r.Header.Set("b", "2")
})) }))
// http.Handler // http.Handler
b.Use(http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { e.Use(http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Set("c", "3") r.Header.Set("c", "3")
}))) })))
// func(http.Handler) http.Handler // func(http.Handler) http.Handler
b.Use(func(http.Handler) http.Handler { e.Use(func(http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Set("d", "4") r.Header.Set("d", "4")
}) })
}) })
// func(http.ResponseWriter, *http.Request)
e.Use(func(w http.ResponseWriter, r *http.Request) {
r.Header.Set("f", "6")
})
// Route // Route
b.Get("/users", func(c *Context) { e.Get("/hello", func(c *Context) {
h := c.Request.Header.Get("a") if c.Request.Header.Get("a") != "1" {
if h != "1" { t.Error("header a should be 1")
t.Errorf("header a should be 1, found %s", h)
} }
h = c.Request.Header.Get("b") if c.Request.Header.Get("b") != "2" {
if h != "2" { t.Error("header b should be 2")
t.Errorf("header b should be 2, found %s", h)
} }
h = c.Request.Header.Get("c") if c.Request.Header.Get("c") != "3" {
if h != "3" { t.Error("header c should be 3")
t.Errorf("header c should be 3, found %s", h)
} }
h = c.Request.Header.Get("d") if c.Request.Header.Get("d") != "4" {
if h != "4" { t.Error("header d should be 4")
t.Errorf("header d should be 4, found %s", h)
} }
if c.Request.Header.Get("e") != "5" {
t.Error("header e should be 5")
}
if c.Request.Header.Get("f") != "6" {
t.Error("header f should be 6")
}
c.String(200, "world")
}) })
w := httptest.NewRecorder() w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/users", nil) r, _ := http.NewRequest("GET", "/hello", nil)
b.ServeHTTP(w, r) e.ServeHTTP(w, r)
if w.Body.String() != "world" {
t.Errorf("body should be world")
}
}
func TestEchoHandler(t *testing.T) {
e := New()
// func(*echo.Context)
e.Get("/1", func(c *Context) {
c.String(http.StatusOK, "1")
})
w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/1", nil)
e.ServeHTTP(w, r)
if w.Body.String() != "1" {
t.Errorf("body should be 1")
}
// http.Handler / http.HandlerFunc
e.Get("/2", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("2"))
}))
w = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/2", nil)
e.ServeHTTP(w, r)
if w.Body.String() != "2" {
t.Errorf("body should be 2")
}
// func(http.ResponseWriter, *http.Request)
e.Get("/3", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("3"))
})
w = httptest.NewRecorder()
r, _ = http.NewRequest("GET", "/3", nil)
e.ServeHTTP(w, r)
if w.Body.String() != "3" {
t.Errorf("body should be 3")
}
} }
func verifyUser(rd io.Reader, t *testing.T) { func verifyUser(rd io.Reader, t *testing.T) {