1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Added coverage for middleware

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-03-29 20:44:55 -07:00
parent bbafeb9abd
commit c29ae7dd7d
4 changed files with 59 additions and 11 deletions

View File

@ -7,6 +7,6 @@ before_install:
- go get golang.org/x/tools/cmd/cover - go get golang.org/x/tools/cmd/cover
script: script:
- go test -coverprofile=echo.coverprofile - go test -coverprofile=echo.coverprofile
- go test -coverprofile=mw.coverprofile ./middleware - go test -coverprofile=middleware.coverprofile ./middleware
- $HOME/gopath/bin/gover - $HOME/gopath/bin/gover
- $HOME/gopath/bin/goveralls -coverprofile=gover.coverprofile -service=travis-ci - $HOME/gopath/bin/goveralls -coverprofile=gover.coverprofile -service=travis-ci

View File

@ -37,15 +37,12 @@ func New() (b *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)
// c.Halt()
}, },
methodNotAllowedHandler: func(c *Context) { methodNotAllowedHandler: func(c *Context) {
http.Error(c.Response, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) http.Error(c.Response, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
// c.Halt()
}, },
internalServerErrorHandler: func(c *Context) { internalServerErrorHandler: func(c *Context) {
http.Error(c.Response, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(c.Response, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
// c.Halt()
}, },
} }
b.Router = NewRouter(b) b.Router = NewRouter(b)
@ -54,7 +51,6 @@ func New() (b *Echo) {
Response: &response{}, Response: &response{},
params: make(Params, b.maxParam), params: make(Params, b.maxParam),
store: make(store), store: make(store),
// i: -1,
echo: b, echo: b,
} }
} }
@ -194,7 +190,7 @@ func wrapM(m Middleware) MiddlewareFunc {
switch m := m.(type) { switch m := m.(type) {
case func(HandlerFunc) HandlerFunc: case func(HandlerFunc) HandlerFunc:
return MiddlewareFunc(m) return MiddlewareFunc(m)
case http.HandlerFunc, func(http.ResponseWriter, *http.Request), http.Handler: case http.HandlerFunc, http.Handler:
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)

View File

@ -32,8 +32,8 @@ func TestEchoMaxParam(t *testing.T) {
func TestEchoIndex(t *testing.T) { func TestEchoIndex(t *testing.T) {
b := New() b := New()
b.Index("example/public/index.html") b.Index("example/public/index.html")
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
b.ServeHTTP(w, r) b.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)
@ -43,14 +43,67 @@ func TestEchoIndex(t *testing.T) {
func TestEchoStatic(t *testing.T) { func TestEchoStatic(t *testing.T) {
b := New() b := New()
b.Static("/js", "example/public/js") b.Static("/js", "example/public/js")
r, _ := http.NewRequest("GET", "/js/main.js", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/js/main.js", nil)
b.ServeHTTP(w, r) b.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) {
b := New()
// func(HandlerFunc) HandlerFunc
b.Use(func(h HandlerFunc) HandlerFunc {
return HandlerFunc(func(c *Context) {
c.Request.Header.Set("a", "1")
h(c)
})
})
// http.HandlerFunc
b.Use(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Set("b", "2")
}))
// http.Handler
b.Use(http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Set("c", "3")
})))
// func(http.Handler) http.Handler
b.Use(func(http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Set("d", "4")
})
})
// Route
b.Get("/users", func(c *Context) {
h := c.Request.Header.Get("a")
if h != "1" {
t.Errorf("header a should be 1, found %s", h)
}
h = c.Request.Header.Get("b")
if h != "2" {
t.Errorf("header b should be 2, found %s", h)
}
h = c.Request.Header.Get("c")
if h != "3" {
t.Errorf("header c should be 3, found %s", h)
}
h = c.Request.Header.Get("d")
if h != "4" {
t.Errorf("header d should be 4, found %s", h)
}
})
w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/users", nil)
b.ServeHTTP(w, r)
}
func verifyUser(rd io.Reader, t *testing.T) { func verifyUser(rd io.Reader, t *testing.T) {
var l int64 var l int64
err := binary.Read(rd, binary.BigEndian, &l) // Body length err := binary.Read(rd, binary.BigEndian, &l) // Body length

View File

@ -17,7 +17,6 @@ type (
} }
) )
func (r *response) WriteHeader(n int) { func (r *response) WriteHeader(n int) {
// TODO: fix when halted. // TODO: fix when halted.
if r.committed { if r.committed {
@ -53,7 +52,7 @@ func (r *response) Flusher() {
func (r *response) Hijack() (net.Conn, *bufio.ReadWriter, error) { func (r *response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
h, ok := r.ResponseWriter.(http.Hijacker) h, ok := r.ResponseWriter.(http.Hijacker)
if !ok { if !ok {
return nil, nil, errors.New("hijacker interface not supported") return nil, nil, errors.New("bolt: hijacker interface not supported")
} }
return h.Hijack() return h.Hijack()
} }