mirror of
https://github.com/labstack/echo.git
synced 2025-07-17 01:43:02 +02:00
10
context.go
10
context.go
@ -112,7 +112,7 @@ func (c *Context) Render(code int, name string, data interface{}) (err error) {
|
|||||||
if c.echo.renderer == nil {
|
if c.echo.renderer == nil {
|
||||||
return RendererNotRegistered
|
return RendererNotRegistered
|
||||||
}
|
}
|
||||||
c.response.Header().Set(ContentType, TextHTML)
|
c.response.Header().Set(ContentType, TextHTMLUTF8)
|
||||||
c.response.WriteHeader(code)
|
c.response.WriteHeader(code)
|
||||||
if err = c.echo.renderer.Render(c.response, name, data); err != nil {
|
if err = c.echo.renderer.Render(c.response, name, data); err != nil {
|
||||||
c.response.clear()
|
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
|
// HTML formats according to a format specifier and sends text/html response with
|
||||||
// status code.
|
// status code.
|
||||||
func (c *Context) HTML(code int, format string, a ...interface{}) (err error) {
|
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)
|
c.response.WriteHeader(code)
|
||||||
if _, err = fmt.Fprintf(c.response, format, a...); err != nil {
|
if _, err = fmt.Fprintf(c.response, format, a...); err != nil {
|
||||||
c.response.clear()
|
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.
|
// JSON sends an application/json response with status code.
|
||||||
func (c *Context) JSON(code int, i interface{}) (err error) {
|
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)
|
c.response.WriteHeader(code)
|
||||||
if err = json.NewEncoder(c.response).Encode(i); err != nil {
|
if err = json.NewEncoder(c.response).Encode(i); err != nil {
|
||||||
c.response.clear()
|
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.
|
// XML sends an application/xml response with status code.
|
||||||
func (c *Context) XML(code int, i interface{}) (err error) {
|
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.WriteHeader(code)
|
||||||
c.response.Write([]byte(xml.Header))
|
c.response.Write([]byte(xml.Header))
|
||||||
if err = xml.NewEncoder(c.response).Encode(i); err != nil {
|
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.
|
// Redirect redirects the request using http.Redirect with status code.
|
||||||
func (c *Context) Redirect(code int, url string) error {
|
func (c *Context) Redirect(code int, url string) error {
|
||||||
http.Redirect(c.response, c.request, url, code)
|
http.Redirect(c.response, c.request, url, code)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error invokes the registered HTTP error handler. Generally used by middleware.
|
// Error invokes the registered HTTP error handler. Generally used by middleware.
|
||||||
|
@ -93,7 +93,7 @@ func TestContext(t *testing.T) {
|
|||||||
err = c.JSON(http.StatusOK, user{"1", "Joe"})
|
err = c.JSON(http.StatusOK, user{"1", "Joe"})
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
assert.Equal(t, http.StatusOK, rec.Code)
|
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()))
|
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"})
|
err = c.XML(http.StatusOK, user{"1", "Joe"})
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
assert.Equal(t, http.StatusOK, rec.Code)
|
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())
|
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>")
|
err = c.HTML(http.StatusOK, "Hello, <strong>World!</strong>")
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
assert.Equal(t, http.StatusOK, rec.Code)
|
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())
|
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
19
echo.go
19
echo.go
@ -91,15 +91,22 @@ const (
|
|||||||
// Media types
|
// Media types
|
||||||
//-------------
|
//-------------
|
||||||
|
|
||||||
ApplicationJSON = "application/json; charset=utf-8"
|
ApplicationJSON = "application/json"
|
||||||
ApplicationXML = "application/xml; charset=utf-8"
|
ApplicationJSONUTF8 = "application/json; " + UTF8
|
||||||
|
ApplicationXML = "application/xml"
|
||||||
|
ApplicationXMLUTF8 = "application/xml; " + UTF8
|
||||||
ApplicationForm = "application/x-www-form-urlencoded"
|
ApplicationForm = "application/x-www-form-urlencoded"
|
||||||
ApplicationProtobuf = "application/protobuf"
|
ApplicationProtobuf = "application/protobuf"
|
||||||
ApplicationMsgpack = "application/msgpack"
|
ApplicationMsgpack = "application/msgpack"
|
||||||
TextHTML = "text/html; charset=utf-8"
|
TextHTML = "text/html"
|
||||||
TextPlain = "text/plain; charset=utf-8"
|
TextHTMLUTF8 = "text/html; " + UTF8
|
||||||
|
TextPlain = "text/plain"
|
||||||
|
TextPlainUTF8 = "text/plain; " + UTF8
|
||||||
MultipartForm = "multipart/form-data"
|
MultipartForm = "multipart/form-data"
|
||||||
|
|
||||||
|
// Charset
|
||||||
|
UTF8 = "charset=utf-8"
|
||||||
|
|
||||||
//---------
|
//---------
|
||||||
// Headers
|
// Headers
|
||||||
//---------
|
//---------
|
||||||
@ -180,9 +187,9 @@ func New() (e *Echo) {
|
|||||||
e.SetBinder(func(r *http.Request, v interface{}) error {
|
e.SetBinder(func(r *http.Request, v interface{}) error {
|
||||||
ct := r.Header.Get(ContentType)
|
ct := r.Header.Get(ContentType)
|
||||||
err := UnsupportedMediaType
|
err := UnsupportedMediaType
|
||||||
if strings.HasPrefix(ApplicationJSON, ct) {
|
if strings.HasPrefix(ct, ApplicationJSON) {
|
||||||
err = json.NewDecoder(r.Body).Decode(v)
|
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)
|
err = xml.NewDecoder(r.Body).Decode(v)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
|
Reference in New Issue
Block a user