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

more coverage

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-04-05 15:30:03 -07:00
parent 550cd94dbe
commit c2d29f3895
6 changed files with 44 additions and 51 deletions

View File

@ -42,17 +42,16 @@ func (c *Context) Bind(i interface{}) error {
} }
// Render encodes the provided type and sends a response with status code // Render encodes the provided type and sends a response with status code
// based on Accept header. // based on Accept header. If Accept header not set, it defaults to html/plain.
func (c *Context) Render(code int, i interface{}) error { func (c *Context) Render(code int, i interface{}) error {
a := c.Request.Header.Get(HeaderAccept) a := c.Request.Header.Get(HeaderAccept)
if strings.HasPrefix(a, MIMEJSON) { if strings.HasPrefix(a, MIMEJSON) {
return c.JSON(code, i) return c.JSON(code, i)
} else if strings.HasPrefix(a, MIMEText) { } else if strings.HasPrefix(a, MIMEText) {
return c.String(code, i.(string)) return c.Text(code, i.(string))
} else if strings.HasPrefix(a, MIMEHTML) { } else if strings.HasPrefix(a, MIMEHTML) {
return c.HTML(code, i.(string))
} }
return ErrUnsupportedMediaType return c.HTML(code, i.(string))
} }
// JSON sends an application/json response with status code. // JSON sends an application/json response with status code.
@ -62,8 +61,8 @@ func (c *Context) JSON(code int, i interface{}) error {
return json.NewEncoder(c.Response).Encode(i) return json.NewEncoder(c.Response).Encode(i)
} }
// String sends a text/plain response with status code. // Text sends a text/plain response with status code.
func (c *Context) String(code int, s string) (err error) { func (c *Context) Text(code int, s string) (err error) {
c.Response.Header().Set(HeaderContentType, MIMEText+"; charset=utf-8") c.Response.Header().Set(HeaderContentType, MIMEText+"; charset=utf-8")
c.Response.WriteHeader(code) c.Response.WriteHeader(code)
_, err = c.Response.Write([]byte(s)) _, err = c.Response.Write([]byte(s))

View File

@ -22,13 +22,30 @@ func TestContext(t *testing.T) {
//**********// //**********//
// Bind // // Bind //
//**********// //**********//
r.Header.Add(HeaderContentType, MIMEJSON) // JSON
r.Header.Set(HeaderContentType, MIMEJSON)
u2 := new(user) u2 := new(user)
if err := c.Bind(u2); err != nil { if err := c.Bind(u2); err != nil {
t.Error(err) t.Error(err)
} }
verifyUser(u2, t) verifyUser(u2, t)
// FORM
r.Header.Set(HeaderContentType, MIMEForm)
u2 = new(user)
if err := c.Bind(u2); err != nil {
t.Error(err)
}
// TODO: add verification
// Unsupported
r.Header.Set(HeaderContentType, "")
u2 = new(user)
if err := c.Bind(u2); err == nil {
t.Error(err)
}
// TODO: add verification
//***********// //***********//
// Param // // Param //
//***********// //***********//

18
echo.go
View File

@ -14,8 +14,6 @@ type (
middleware []MiddlewareFunc middleware []MiddlewareFunc
maxParam byte maxParam byte
notFoundHandler HandlerFunc notFoundHandler HandlerFunc
methodNotAllowedHandler HandlerFunc
internalServerErrorHandler HandlerFunc
pool sync.Pool pool sync.Pool
} }
Middleware interface{} Middleware interface{}
@ -71,12 +69,6 @@ func New() (e *Echo) {
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)
}, },
methodNotAllowedHandler: func(c *Context) {
http.Error(c.Response, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
},
internalServerErrorHandler: func(c *Context) {
http.Error(c.Response, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
},
} }
e.Router = NewRouter(e) e.Router = NewRouter(e)
e.pool.New = func() interface{} { e.pool.New = func() interface{} {
@ -122,16 +114,6 @@ func (e *Echo) NotFoundHandler(h Handler) {
e.notFoundHandler = wrapH(h) e.notFoundHandler = wrapH(h)
} }
// MethodNotAllowedHandler sets a custom MethodNotAllowed handler.
func (e *Echo) MethodNotAllowedHandler(h Handler) {
e.methodNotAllowedHandler = wrapH(h)
}
// InternalServerErrorHandler sets a custom InternalServerError handler.
func (e *Echo) InternalServerErrorHandler(h Handler) {
e.internalServerErrorHandler = wrapH(h)
}
// Use adds handler to the middleware chain. // Use adds handler to the middleware chain.
func (e *Echo) Use(m ...Middleware) { func (e *Echo) Use(m ...Middleware) {
for _, h := range m { for _, h := range m {

View File

@ -92,7 +92,7 @@ func TestEchoMiddleware(t *testing.T) {
// Route // Route
e.Get("/hello", func(c *Context) { e.Get("/hello", func(c *Context) {
c.String(200, "world") c.Text(200, "world")
}) })
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -111,7 +111,7 @@ func TestEchoHandler(t *testing.T) {
// func(*echo.Context) // func(*echo.Context)
e.Get("/1", func(c *Context) { e.Get("/1", func(c *Context) {
c.String(http.StatusOK, "1") c.Text(http.StatusOK, "1")
}) })
w := httptest.NewRecorder() w := httptest.NewRecorder()
r, _ := http.NewRequest(MethodGET, "/1", nil) r, _ := http.NewRequest(MethodGET, "/1", nil)
@ -189,13 +189,16 @@ func TestEchoSubGroup(t *testing.T) {
} }
func TestEchoMethod(t *testing.T) { func TestEchoMethod(t *testing.T) {
// e := New() e := New()
// // GET e.Connect("/", func(*Context) {})
// e.Get("/users", func(c *Context) {}) e.Delete("/", func(*Context) {})
// h, _, _ := e.Router.Find("GET", "/users") e.Get("/", func(*Context) {})
// if h == nil { e.Head("/", func(*Context) {})
// t.Error("should find route for GET") e.Options("/", func(*Context) {})
// } e.Patch("/", func(*Context) {})
e.Post("/", func(*Context) {})
e.Put("/", func(*Context) {})
e.Trace("/", func(*Context) {})
} }
func TestEchoServeHTTP(t *testing.T) { func TestEchoServeHTTP(t *testing.T) {
@ -218,14 +221,6 @@ func TestEchoServeHTTP(t *testing.T) {
if w.Code != http.StatusNotFound { if w.Code != http.StatusNotFound {
t.Errorf("status code should be 404, found %d", w.Code) t.Errorf("status code should be 404, found %d", w.Code)
} }
// NotAllowed
// r, _ = http.NewRequest("POST", "/users", nil)
// w = httptest.NewRecorder()
// e.ServeHTTP(w, r)
// if w.Code != http.StatusMethodNotAllowed {
// t.Errorf("status code should be 405, found %d", w.Code)
// }
} }
func verifyUser(u2 *user, t *testing.T) { func verifyUser(u2 *user, t *testing.T) {

View File

@ -88,7 +88,7 @@ func main() {
sub.Use(func(c *echo.Context) { // Middleware sub.Use(func(c *echo.Context) { // Middleware
}) })
sub.Get("/home", func(c *echo.Context) { sub.Get("/home", func(c *echo.Context) {
c.String(200, "Sub route /sub/welcome") c.Text(http.StatusOK, "Sub route /sub/welcome")
}) })
// Group - doesn't inherit parent middleware // Group - doesn't inherit parent middleware
@ -96,7 +96,7 @@ func main() {
grp.Use(func(c *echo.Context) { // Middleware grp.Use(func(c *echo.Context) { // Middleware
}) })
grp.Get("/home", func(c *echo.Context) { grp.Get("/home", func(c *echo.Context) {
c.String(200, "Group route /group/welcome") c.Text(http.StatusOK, "Group route /group/welcome")
}) })
// Start server // Start server

View File

@ -9,7 +9,7 @@ import (
func TestResponse(t *testing.T) { func TestResponse(t *testing.T) {
e := New() e := New()
e.Get("/hello", func(c *Context) { e.Get("/hello", func(c *Context) {
c.String(http.StatusOK, "world") c.Text(http.StatusOK, "world")
// Status // Status
if c.Response.Status() != http.StatusOK { if c.Response.Status() != http.StatusOK {