Skip to content

Commit

Permalink
Changes related to #1199
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Jan 14, 2019
1 parent 62145fa commit 7867fce
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
32 changes: 16 additions & 16 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,9 @@ type (
const (
defaultMemory = 32 << 20 // 32 MB
indexPage = "index.html"
defaultIndent = " "
)

var defaultIndent = " "

func (c *context) writeContentType(value string) {
header := c.Response().Header()
if header.Get(HeaderContentType) == "" {
Expand Down Expand Up @@ -425,26 +424,26 @@ func (c *context) jsonPBlob(code int, callback string, i interface{}) (err error
return
}

func (c *context) jsonBlob(code int, i interface{}, indent *string) error {
func (c *context) json(code int, i interface{}, indent string) error {
enc := json.NewEncoder(c.response)
if indent != nil {
enc.SetIndent("", *indent)
if indent != "" {
enc.SetIndent("", indent)
}
c.writeContentType(MIMEApplicationJSONCharsetUTF8)
c.response.WriteHeader(code)
return enc.Encode(i)
}

func (c *context) JSON(code int, i interface{}) (err error) {
var indent *string
indent := ""
if _, pretty := c.QueryParams()["pretty"]; c.echo.Debug || pretty {
indent = &defaultIndent
indent = defaultIndent
}
return c.jsonBlob(code, i, indent)
return c.json(code, i, indent)
}

func (c *context) JSONPretty(code int, i interface{}, indent string) (err error) {
return c.jsonBlob(code, i, &indent)
return c.json(code, i, indent)
}

func (c *context) JSONBlob(code int, b []byte) (err error) {
Expand All @@ -468,12 +467,12 @@ func (c *context) JSONPBlob(code int, callback string, b []byte) (err error) {
return
}

func (c *context) xmlBlob(code int, i interface{}, indent *string) (err error) {
func (c *context) xml(code int, i interface{}, indent string) (err error) {
c.writeContentType(MIMEApplicationXMLCharsetUTF8)
c.response.WriteHeader(code)
enc := xml.NewEncoder(c.response)
if indent != nil {
enc.Indent("", *indent)
if indent != "" {
enc.Indent("", indent)
}
if _, err = c.response.Write([]byte(xml.Header)); err != nil {
return
Expand All @@ -482,15 +481,15 @@ func (c *context) xmlBlob(code int, i interface{}, indent *string) (err error) {
}

func (c *context) XML(code int, i interface{}) (err error) {
var indent *string
indent := ""
if _, pretty := c.QueryParams()["pretty"]; c.echo.Debug || pretty {
indent = &defaultIndent
indent = defaultIndent
}
return c.xmlBlob(code, i, indent)
return c.xml(code, i, indent)
}

func (c *context) XMLPretty(code int, i interface{}, indent string) (err error) {
return c.xmlBlob(code, i, &indent)
return c.xml(code, i, indent)
}

func (c *context) XMLBlob(code int, b []byte) (err error) {
Expand Down Expand Up @@ -598,3 +597,4 @@ func (c *context) Reset(r *http.Request, w http.ResponseWriter) {
// NOTE: Don't reset because it has to have length c.echo.maxParam at all times
// c.pvalues = nil
}

5 changes: 3 additions & 2 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func TestContext(t *testing.T) {
enc := json.NewEncoder(buf)
enc.SetIndent(emptyIndent, emptyIndent)
err = enc.Encode(u)
err = c.jsonBlob(http.StatusOK, user{1, "Jon Snow"}, &emptyIndent)
err = c.json(http.StatusOK, user{1, "Jon Snow"}, emptyIndent)
if assert.NoError(err) {
assert.Equal(http.StatusOK, rec.Code)
assert.Equal(MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
Expand All @@ -229,7 +229,7 @@ func TestContext(t *testing.T) {
enc := xml.NewEncoder(buf)
enc.Indent(emptyIndent, emptyIndent)
err = enc.Encode(u)
err = c.xmlBlob(http.StatusOK, user{1, "Jon Snow"}, &emptyIndent)
err = c.xml(http.StatusOK, user{1, "Jon Snow"}, emptyIndent)
if assert.NoError(err) {
assert.Equal(http.StatusOK, rec.Code)
assert.Equal(MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
Expand Down Expand Up @@ -543,3 +543,4 @@ func TestContextHandler(t *testing.T) {
c.Handler()(c)
testify.Equal(t, "handler", b.String())
}

0 comments on commit 7867fce

Please sign in to comment.