1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-28 08:38:39 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-07-20 19:24:33 -07:00
parent 6da918876f
commit 17c83a01a4
3 changed files with 21 additions and 14 deletions

View File

@ -112,7 +112,7 @@ func (c *Context) Render(code int, name string, data interface{}) (err error) {
if c.echo.renderer == nil {
return RendererNotRegistered
}
c.response.Header().Set(ContentType, TextHTML)
c.response.Header().Set(ContentType, TextHTMLUTF8)
c.response.WriteHeader(code)
if err = c.echo.renderer.Render(c.response, name, data); err != nil {
c.response.clear()
@ -123,7 +123,7 @@ func (c *Context) Render(code int, name string, data interface{}) (err error) {
// HTML formats according to a format specifier and sends text/html response with
// status code.
func (c *Context) HTML(code int, format string, a ...interface{}) (err error) {
c.response.Header().Set(ContentType, TextHTML)
c.response.Header().Set(ContentType, TextHTMLUTF8)
c.response.WriteHeader(code)
if _, err = fmt.Fprintf(c.response, format, a...); err != nil {
c.response.clear()
@ -144,7 +144,7 @@ func (c *Context) String(code int, format string, a ...interface{}) (err error)
// JSON sends an application/json response with status code.
func (c *Context) JSON(code int, i interface{}) (err error) {
c.response.Header().Set(ContentType, ApplicationJSON)
c.response.Header().Set(ContentType, ApplicationJSONUTF8)
c.response.WriteHeader(code)
if err = json.NewEncoder(c.response).Encode(i); err != nil {
c.response.clear()
@ -154,7 +154,7 @@ func (c *Context) JSON(code int, i interface{}) (err error) {
// XML sends an application/xml response with status code.
func (c *Context) XML(code int, i interface{}) (err error) {
c.response.Header().Set(ContentType, ApplicationXML)
c.response.Header().Set(ContentType, ApplicationXMLUTF8)
c.response.WriteHeader(code)
c.response.Write([]byte(xml.Header))
if err = xml.NewEncoder(c.response).Encode(i); err != nil {
@ -172,7 +172,7 @@ func (c *Context) NoContent(code int) error {
// Redirect redirects the request using http.Redirect with status code.
func (c *Context) Redirect(code int, url string) error {
http.Redirect(c.response, c.request, url, code)
return nil
return nil
}
// Error invokes the registered HTTP error handler. Generally used by middleware.

View File

@ -93,7 +93,7 @@ func TestContext(t *testing.T) {
err = c.JSON(http.StatusOK, user{"1", "Joe"})
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, ApplicationJSON, rec.Header().Get(ContentType))
assert.Equal(t, ApplicationJSONUTF8, rec.Header().Get(ContentType))
assert.Equal(t, userJSON, strings.TrimSpace(rec.Body.String()))
}
@ -104,7 +104,7 @@ func TestContext(t *testing.T) {
err = c.XML(http.StatusOK, user{"1", "Joe"})
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, ApplicationXML, rec.Header().Get(ContentType))
assert.Equal(t, ApplicationXMLUTF8, rec.Header().Get(ContentType))
assert.Equal(t, xml.Header, xml.Header, rec.Body.String())
}
@ -126,7 +126,7 @@ func TestContext(t *testing.T) {
err = c.HTML(http.StatusOK, "Hello, <strong>World!</strong>")
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, TextHTML, rec.Header().Get(ContentType))
assert.Equal(t, TextHTMLUTF8, rec.Header().Get(ContentType))
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
}

19
echo.go
View File

@ -91,15 +91,22 @@ const (
// Media types
//-------------
ApplicationJSON = "application/json; charset=utf-8"
ApplicationXML = "application/xml; charset=utf-8"
ApplicationJSON = "application/json"
ApplicationJSONUTF8 = "application/json; " + UTF8
ApplicationXML = "application/xml"
ApplicationXMLUTF8 = "application/xml; " + UTF8
ApplicationForm = "application/x-www-form-urlencoded"
ApplicationProtobuf = "application/protobuf"
ApplicationMsgpack = "application/msgpack"
TextHTML = "text/html; charset=utf-8"
TextPlain = "text/plain; charset=utf-8"
TextHTML = "text/html"
TextHTMLUTF8 = "text/html; " + UTF8
TextPlain = "text/plain"
TextPlainUTF8 = "text/plain; " + UTF8
MultipartForm = "multipart/form-data"
// Charset
UTF8 = "charset=utf-8"
//---------
// Headers
//---------
@ -180,9 +187,9 @@ func New() (e *Echo) {
e.SetBinder(func(r *http.Request, v interface{}) error {
ct := r.Header.Get(ContentType)
err := UnsupportedMediaType
if strings.HasPrefix(ApplicationJSON, ct) {
if strings.HasPrefix(ct, ApplicationJSON) {
err = json.NewDecoder(r.Body).Decode(v)
} else if strings.HasPrefix(ApplicationXML, ct) {
} else if strings.HasPrefix(ct, ApplicationXML) {
err = xml.NewDecoder(r.Body).Decode(v)
}
return err