From c2d29f38955fad857900f17f1be49adb8e537719 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Sun, 5 Apr 2015 15:30:03 -0700 Subject: [PATCH] more coverage Signed-off-by: Vishal Rana --- context.go | 11 +++++------ context_test.go | 19 ++++++++++++++++++- echo.go | 30 ++++++------------------------ echo_test.go | 29 ++++++++++++----------------- example/main.go | 4 ++-- response_test.go | 2 +- 6 files changed, 44 insertions(+), 51 deletions(-) diff --git a/context.go b/context.go index 258ac9a8..8c11372d 100644 --- a/context.go +++ b/context.go @@ -42,17 +42,16 @@ func (c *Context) Bind(i interface{}) error { } // 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 { a := c.Request.Header.Get(HeaderAccept) if strings.HasPrefix(a, MIMEJSON) { return c.JSON(code, i) } else if strings.HasPrefix(a, MIMEText) { - return c.String(code, i.(string)) + return c.Text(code, i.(string)) } 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. @@ -62,8 +61,8 @@ func (c *Context) JSON(code int, i interface{}) error { return json.NewEncoder(c.Response).Encode(i) } -// String sends a text/plain response with status code. -func (c *Context) String(code int, s string) (err error) { +// Text sends a text/plain response with status code. +func (c *Context) Text(code int, s string) (err error) { c.Response.Header().Set(HeaderContentType, MIMEText+"; charset=utf-8") c.Response.WriteHeader(code) _, err = c.Response.Write([]byte(s)) diff --git a/context_test.go b/context_test.go index b3677a4b..9c9def66 100644 --- a/context_test.go +++ b/context_test.go @@ -22,13 +22,30 @@ func TestContext(t *testing.T) { //**********// // Bind // //**********// - r.Header.Add(HeaderContentType, MIMEJSON) + // JSON + r.Header.Set(HeaderContentType, MIMEJSON) u2 := new(user) if err := c.Bind(u2); err != nil { t.Error(err) } 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 // //***********// diff --git a/echo.go b/echo.go index 44af01e9..bd781f74 100644 --- a/echo.go +++ b/echo.go @@ -9,14 +9,12 @@ import ( type ( Echo struct { - Router *router - prefix string - middleware []MiddlewareFunc - maxParam byte - notFoundHandler HandlerFunc - methodNotAllowedHandler HandlerFunc - internalServerErrorHandler HandlerFunc - pool sync.Pool + Router *router + prefix string + middleware []MiddlewareFunc + maxParam byte + notFoundHandler HandlerFunc + pool sync.Pool } Middleware interface{} MiddlewareFunc func(HandlerFunc) HandlerFunc @@ -71,12 +69,6 @@ func New() (e *Echo) { notFoundHandler: func(c *Context) { 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.pool.New = func() interface{} { @@ -122,16 +114,6 @@ func (e *Echo) NotFoundHandler(h Handler) { 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. func (e *Echo) Use(m ...Middleware) { for _, h := range m { diff --git a/echo_test.go b/echo_test.go index 58483c0a..de056ad4 100644 --- a/echo_test.go +++ b/echo_test.go @@ -92,7 +92,7 @@ func TestEchoMiddleware(t *testing.T) { // Route e.Get("/hello", func(c *Context) { - c.String(200, "world") + c.Text(200, "world") }) w := httptest.NewRecorder() @@ -111,7 +111,7 @@ func TestEchoHandler(t *testing.T) { // func(*echo.Context) e.Get("/1", func(c *Context) { - c.String(http.StatusOK, "1") + c.Text(http.StatusOK, "1") }) w := httptest.NewRecorder() r, _ := http.NewRequest(MethodGET, "/1", nil) @@ -189,13 +189,16 @@ func TestEchoSubGroup(t *testing.T) { } func TestEchoMethod(t *testing.T) { - // e := New() - // // GET - // e.Get("/users", func(c *Context) {}) - // h, _, _ := e.Router.Find("GET", "/users") - // if h == nil { - // t.Error("should find route for GET") - // } + e := New() + e.Connect("/", func(*Context) {}) + e.Delete("/", func(*Context) {}) + e.Get("/", func(*Context) {}) + e.Head("/", func(*Context) {}) + 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) { @@ -218,14 +221,6 @@ func TestEchoServeHTTP(t *testing.T) { if w.Code != http.StatusNotFound { 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) { diff --git a/example/main.go b/example/main.go index 1ab99b92..8540dd51 100644 --- a/example/main.go +++ b/example/main.go @@ -88,7 +88,7 @@ func main() { sub.Use(func(c *echo.Context) { // Middleware }) 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 @@ -96,7 +96,7 @@ func main() { grp.Use(func(c *echo.Context) { // Middleware }) grp.Get("/home", func(c *echo.Context) { - c.String(200, "Group route /group/welcome") + c.Text(http.StatusOK, "Group route /group/welcome") }) // Start server diff --git a/response_test.go b/response_test.go index 3bbae717..fe49a28f 100644 --- a/response_test.go +++ b/response_test.go @@ -9,7 +9,7 @@ import ( func TestResponse(t *testing.T) { e := New() e.Get("/hello", func(c *Context) { - c.String(http.StatusOK, "world") + c.Text(http.StatusOK, "world") // Status if c.Response.Status() != http.StatusOK {