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

Refactored contants for media types

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-07-22 04:44:29 -07:00
parent fd427bc943
commit 99f2868bcc
3 changed files with 20 additions and 20 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, TextHTMLUTF8)
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
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, TextHTMLUTF8)
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
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, ApplicationJSONUTF8)
c.response.Header().Set(ContentType, ApplicationJSONCharsetUTF8)
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, ApplicationXMLUTF8)
c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8)
c.response.WriteHeader(code)
c.response.Write([]byte(xml.Header))
if err = xml.NewEncoder(c.response).Encode(i); err != nil {

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, ApplicationJSONUTF8, rec.Header().Get(ContentType))
assert.Equal(t, ApplicationJSONCharsetUTF8, 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, ApplicationXMLUTF8, rec.Header().Get(ContentType))
assert.Equal(t, ApplicationXMLCharsetUTF8, 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, TextHTMLUTF8, rec.Header().Get(ContentType))
assert.Equal(t, TextHTMLCharsetUTF8, rec.Header().Get(ContentType))
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
}

26
echo.go
View File

@ -91,21 +91,21 @@ const (
// Media types
//-------------
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"
TextHTMLUTF8 = "text/html; " + UTF8
TextPlain = "text/plain"
TextPlainUTF8 = "text/plain; " + UTF8
MultipartForm = "multipart/form-data"
ApplicationJSON = "application/json"
ApplicationJSONCharsetUTF8 = ApplicationJSON + "; " + CharsetUTF8
ApplicationXML = "application/xml"
ApplicationXMLCharsetUTF8 = ApplicationXML + "; " + CharsetUTF8
ApplicationForm = "application/x-www-form-urlencoded"
ApplicationProtobuf = "application/protobuf"
ApplicationMsgpack = "application/msgpack"
TextHTML = "text/html"
TextHTMLCharsetUTF8 = TextHTML + "; " + CharsetUTF8
TextPlain = "text/plain"
TextPlainCharsetUTF8 = TextPlain + "; " + CharsetUTF8
MultipartForm = "multipart/form-data"
// Charset
UTF8 = "charset=utf-8"
CharsetUTF8 = "charset=utf-8"
//---------
// Headers