mirror of
https://github.com/labstack/echo.git
synced 2025-01-12 01:22:21 +02:00
commit
0097dfafcd
25
context.go
25
context.go
@ -166,6 +166,18 @@ func (c *Context) JSON(code int, i interface{}) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// JSON sends a JSON response with status code, but it applies prefix and indent to format the output.
|
||||
func (c *Context) JSONIndent(code int, i interface{}, prefix string, indent string) (err error) {
|
||||
b, err := json.MarshalIndent(i, prefix, indent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.response.Header().Set(ContentType, ApplicationJSONCharsetUTF8)
|
||||
c.response.WriteHeader(code)
|
||||
c.response.Write(b)
|
||||
return
|
||||
}
|
||||
|
||||
// JSONP sends a JSONP response with status code. It uses `callback` to construct
|
||||
// the JSONP payload.
|
||||
func (c *Context) JSONP(code int, callback string, i interface{}) (err error) {
|
||||
@ -194,6 +206,19 @@ func (c *Context) XML(code int, i interface{}) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// XML sends an XML response with status code, but it applies prefix and indent to format the output.
|
||||
func (c *Context) XMLIndent(code int, i interface{}, prefix string, indent string) (err error) {
|
||||
b, err := xml.MarshalIndent(i, prefix, indent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8)
|
||||
c.response.WriteHeader(code)
|
||||
c.response.Write([]byte(xml.Header))
|
||||
c.response.Write(b)
|
||||
return
|
||||
}
|
||||
|
||||
// File sends a response with the content of the file. If `attachment` is set
|
||||
// to true, the client is prompted to save the file with provided `name`,
|
||||
// name can be empty, in that case name of the file is used.
|
||||
|
@ -10,9 +10,9 @@ import (
|
||||
|
||||
"strings"
|
||||
|
||||
"encoding/xml"
|
||||
"net/url"
|
||||
|
||||
"encoding/xml"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@ -28,7 +28,11 @@ func (t *Template) Render(w io.Writer, name string, data interface{}) error {
|
||||
|
||||
func TestContext(t *testing.T) {
|
||||
userJSON := `{"id":"1","name":"Joe"}`
|
||||
userJSONIndent := "{\n_?\"id\": \"1\",\n_?\"name\": \"Joe\"\n_}"
|
||||
userXML := `<user><id>1</id><name>Joe</name></user>`
|
||||
userXMLIndent := "_<user>\n_?<id>1</id>\n_?<name>Joe</name>\n_</user>"
|
||||
|
||||
var nonMarshallableChannel chan bool
|
||||
|
||||
req, _ := http.NewRequest(POST, "/", strings.NewReader(userJSON))
|
||||
rec := httptest.NewRecorder()
|
||||
@ -97,6 +101,29 @@ func TestContext(t *testing.T) {
|
||||
assert.Equal(t, userJSON, rec.Body.String())
|
||||
}
|
||||
|
||||
// JSON (error)
|
||||
rec = httptest.NewRecorder()
|
||||
c = NewContext(req, NewResponse(rec), New())
|
||||
val := make(chan bool)
|
||||
err = c.JSON(http.StatusOK, val)
|
||||
assert.Error(t, err)
|
||||
|
||||
// JSONIndent
|
||||
rec = httptest.NewRecorder()
|
||||
c = NewContext(req, NewResponse(rec), New())
|
||||
err = c.JSONIndent(http.StatusOK, user{"1", "Joe"}, "_", "?")
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
assert.Equal(t, ApplicationJSONCharsetUTF8, rec.Header().Get(ContentType))
|
||||
assert.Equal(t, userJSONIndent, rec.Body.String())
|
||||
}
|
||||
|
||||
// JSONIndent (error)
|
||||
rec = httptest.NewRecorder()
|
||||
c = NewContext(req, NewResponse(rec), New())
|
||||
err = c.JSONIndent(http.StatusOK, nonMarshallableChannel, "_", "?")
|
||||
assert.Error(t, err)
|
||||
|
||||
// JSONP
|
||||
rec = httptest.NewRecorder()
|
||||
c = NewContext(req, NewResponse(rec), New())
|
||||
@ -115,9 +142,31 @@ func TestContext(t *testing.T) {
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
assert.Equal(t, ApplicationXMLCharsetUTF8, rec.Header().Get(ContentType))
|
||||
assert.Equal(t, xml.Header, xml.Header, rec.Body.String())
|
||||
assert.Equal(t, xml.Header+userXML, rec.Body.String())
|
||||
}
|
||||
|
||||
// XML (error)
|
||||
rec = httptest.NewRecorder()
|
||||
c = NewContext(req, NewResponse(rec), New())
|
||||
err = c.XML(http.StatusOK, nonMarshallableChannel)
|
||||
assert.Error(t, err)
|
||||
|
||||
// XMLIndent
|
||||
rec = httptest.NewRecorder()
|
||||
c = NewContext(req, NewResponse(rec), New())
|
||||
err = c.XMLIndent(http.StatusOK, user{"1", "Joe"}, "_", "?")
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
assert.Equal(t, ApplicationXMLCharsetUTF8, rec.Header().Get(ContentType))
|
||||
assert.Equal(t, xml.Header+userXMLIndent, rec.Body.String())
|
||||
}
|
||||
|
||||
// XMLIndent (error)
|
||||
rec = httptest.NewRecorder()
|
||||
c = NewContext(req, NewResponse(rec), New())
|
||||
err = c.XMLIndent(http.StatusOK, nonMarshallableChannel, "_", "?")
|
||||
assert.Error(t, err)
|
||||
|
||||
// String
|
||||
rec = httptest.NewRecorder()
|
||||
c = NewContext(req, NewResponse(rec), New())
|
||||
|
Loading…
Reference in New Issue
Block a user