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

JSONIndent, XMLIndent, changes after review

This commit is contained in:
Marcin Tojek 2015-11-08 00:39:09 +01:00
parent 1bfd7762ca
commit 31b657e1d2
2 changed files with 41 additions and 27 deletions

View File

@ -156,19 +156,19 @@ func (c *Context) String(code int, format string, a ...interface{}) (err error)
// JSON sends a JSON response with status code.
func (c *Context) JSON(code int, i interface{}) (err error) {
return c.JSONPrettyPrint(code, i, false)
}
// JSON sends a pretty printed JSON response with status code.
func (c *Context) JSONPrettyPrint(code int, i interface{}, prettyPrint bool) (err error) {
var b []byte
if prettyPrint {
b, err = json.MarshalIndent(i, " ", " ")
} else {
b, err = json.Marshal(i)
b, err := json.Marshal(i)
if err != nil {
return err
}
c.response.Header().Set(ContentType, ApplicationJSONCharsetUTF8)
c.response.WriteHeader(code)
c.response.Write(b)
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
}
@ -206,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.

View File

@ -10,9 +10,9 @@ import (
"strings"
"encoding/xml"
"net/url"
"encoding/xml"
"github.com/stretchr/testify/assert"
)
@ -28,8 +28,9 @@ func (t *Template) Render(w io.Writer, name string, data interface{}) error {
func TestContext(t *testing.T) {
userJSON := `{"id":"1","name":"Joe"}`
userJSONPrettyPrint := "{\n \"id\": \"1\",\n \"name\": \"Joe\"\n }"
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>"
req, _ := http.NewRequest(POST, "/", strings.NewReader(userJSON))
rec := httptest.NewRecorder()
@ -98,24 +99,14 @@ func TestContext(t *testing.T) {
assert.Equal(t, userJSON, rec.Body.String())
}
// JSONPrettyPrint (pretty)
// JSONIndent
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec), New())
err = c.JSONPrettyPrint(http.StatusOK, user{"1", "Joe"}, true)
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, userJSONPrettyPrint, rec.Body.String())
}
// JSONPrettyPrint (not pretty)
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec), New())
err = c.JSONPrettyPrint(http.StatusOK, user{"1", "Joe"}, false)
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, ApplicationJSONCharsetUTF8, rec.Header().Get(ContentType))
assert.Equal(t, userJSON, rec.Body.String())
assert.Equal(t, userJSONIndent, rec.Body.String())
}
// JSONP
@ -136,7 +127,17 @@ 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())
}
// 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())
}
// String