mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
Refactored media types and headers
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
3b87f90bfd
commit
8ace8e2143
@ -63,14 +63,14 @@ func (c *Context) Render(code int, name string, data interface{}) *HTTPError {
|
||||
if c.echo.renderer == nil {
|
||||
return &HTTPError{Error: RendererNotRegistered}
|
||||
}
|
||||
c.Response.Header().Set(HeaderContentType, MIMEHTML+"; charset=utf-8")
|
||||
c.Response.Header().Set(ContentType, TextHTML+"; charset=utf-8")
|
||||
c.Response.WriteHeader(code)
|
||||
return c.echo.renderer.Render(c.Response, name, data)
|
||||
}
|
||||
|
||||
// JSON sends an application/json response with status code.
|
||||
func (c *Context) JSON(code int, v interface{}) *HTTPError {
|
||||
c.Response.Header().Set(HeaderContentType, MIMEJSON+"; charset=utf-8")
|
||||
c.Response.Header().Set(ContentType, ApplicationJSON+"; charset=utf-8")
|
||||
c.Response.WriteHeader(code)
|
||||
if err := json.NewEncoder(c.Response).Encode(v); err != nil {
|
||||
return &HTTPError{Error: err}
|
||||
@ -80,7 +80,7 @@ func (c *Context) JSON(code int, v interface{}) *HTTPError {
|
||||
|
||||
// String sends a text/plain response with status code.
|
||||
func (c *Context) String(code int, s string) *HTTPError {
|
||||
c.Response.Header().Set(HeaderContentType, MIMEText+"; charset=utf-8")
|
||||
c.Response.Header().Set(ContentType, TextPlain+"; charset=utf-8")
|
||||
c.Response.WriteHeader(code)
|
||||
if _, err := c.Response.Write([]byte(s)); err != nil {
|
||||
return &HTTPError{Error: err}
|
||||
@ -90,7 +90,7 @@ func (c *Context) String(code int, s string) *HTTPError {
|
||||
|
||||
// HTML sends a text/html response with status code.
|
||||
func (c *Context) HTML(code int, html string) *HTTPError {
|
||||
c.Response.Header().Set(HeaderContentType, MIMEHTML+"; charset=utf-8")
|
||||
c.Response.Header().Set(ContentType, TextHTML+"; charset=utf-8")
|
||||
c.Response.WriteHeader(code)
|
||||
if _, err := c.Response.Write([]byte(html)); err != nil {
|
||||
return &HTTPError{Error: err}
|
||||
|
@ -33,7 +33,7 @@ func TestContext(t *testing.T) {
|
||||
//------
|
||||
|
||||
// JSON
|
||||
r.Header.Set(HeaderContentType, MIMEJSON)
|
||||
r.Header.Set(ContentType, ApplicationJSON)
|
||||
u2 := new(user)
|
||||
if he := c.Bind(u2); he != nil {
|
||||
t.Errorf("bind %#v", he)
|
||||
@ -41,7 +41,7 @@ func TestContext(t *testing.T) {
|
||||
verifyUser(u2, t)
|
||||
|
||||
// FORM
|
||||
r.Header.Set(HeaderContentType, MIMEForm)
|
||||
r.Header.Set(ContentType, ApplicationForm)
|
||||
u2 = new(user)
|
||||
if he := c.Bind(u2); he != nil {
|
||||
t.Errorf("bind %#v", he)
|
||||
@ -49,7 +49,7 @@ func TestContext(t *testing.T) {
|
||||
// TODO: add verification
|
||||
|
||||
// Unsupported
|
||||
r.Header.Set(HeaderContentType, "")
|
||||
r.Header.Set(ContentType, "")
|
||||
u2 = new(user)
|
||||
if he := c.Bind(u2); he == nil {
|
||||
t.Errorf("bind %#v", he)
|
||||
@ -93,21 +93,21 @@ func TestContext(t *testing.T) {
|
||||
}
|
||||
|
||||
// JSON
|
||||
r.Header.Set(HeaderAccept, MIMEJSON)
|
||||
r.Header.Set(Accept, ApplicationJSON)
|
||||
c.Response.committed = false
|
||||
if he := c.JSON(http.StatusOK, u1); he != nil {
|
||||
t.Errorf("json %#v", he)
|
||||
}
|
||||
|
||||
// String
|
||||
r.Header.Set(HeaderAccept, MIMEText)
|
||||
r.Header.Set(Accept, TextPlain)
|
||||
c.Response.committed = false
|
||||
if he := c.String(http.StatusOK, "Hello, World!"); he != nil {
|
||||
t.Errorf("string %#v", he.Error)
|
||||
}
|
||||
|
||||
// HTML
|
||||
r.Header.Set(HeaderAccept, MIMEHTML)
|
||||
r.Header.Set(Accept, TextHTML)
|
||||
c.Response.committed = false
|
||||
if he := c.HTML(http.StatusOK, "Hello, <strong>World!</strong>"); he != nil {
|
||||
t.Errorf("html %v", he.Error)
|
||||
|
33
echo.go
33
echo.go
@ -73,16 +73,25 @@ const (
|
||||
// TRACE HTTP method
|
||||
TRACE = "TRACE"
|
||||
|
||||
MIMEJSON = "application/json"
|
||||
MIMEText = "text/plain"
|
||||
MIMEHTML = "text/html"
|
||||
MIMEForm = "application/x-www-form-urlencoded"
|
||||
MIMEMultipartForm = "multipart/form-data"
|
||||
//-------------
|
||||
// Media types
|
||||
//-------------
|
||||
|
||||
HeaderAccept = "Accept"
|
||||
HeaderContentDisposition = "Content-Disposition"
|
||||
HeaderContentLength = "Content-Length"
|
||||
HeaderContentType = "Content-Type"
|
||||
ApplicationJSON = "application/json"
|
||||
ApplicationProtobuf = "application/protobuf"
|
||||
TextPlain = "text/plain"
|
||||
TextHTML = "text/html"
|
||||
ApplicationForm = "application/x-www-form-urlencoded"
|
||||
MultipartForm = "multipart/form-data"
|
||||
|
||||
//---------
|
||||
// Headers
|
||||
//---------
|
||||
|
||||
Accept = "Accept"
|
||||
ContentDisposition = "Content-Disposition"
|
||||
ContentLength = "Content-Length"
|
||||
ContentType = "Content-Type"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -138,11 +147,11 @@ func New() (e *Echo) {
|
||||
http.Error(c.Response, he.Message, he.Code)
|
||||
})
|
||||
e.Binder(func(r *http.Request, v interface{}) *HTTPError {
|
||||
ct := r.Header.Get(HeaderContentType)
|
||||
ct := r.Header.Get(ContentType)
|
||||
err := UnsupportedMediaType
|
||||
if strings.HasPrefix(ct, MIMEJSON) {
|
||||
if strings.HasPrefix(ct, ApplicationJSON) {
|
||||
err = json.NewDecoder(r.Body).Decode(v)
|
||||
} else if strings.HasPrefix(ct, MIMEForm) {
|
||||
} else if strings.HasPrefix(ct, ApplicationForm) {
|
||||
err = nil
|
||||
}
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user