Skip to content

Commit

Permalink
fixed json, xml pretty print
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <vr@labstack.com>
  • Loading branch information
vishr committed Dec 9, 2016
1 parent 133f7ac commit 20954af
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func (c *context) XMLPretty(code int, i interface{}, indent string) (err error)
if err != nil {
return
}
return c.JSONBlob(code, b)
return c.XMLBlob(code, b)
}

func (c *context) XMLBlob(code int, b []byte) (err error) {
Expand Down
20 changes: 20 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ func TestContext(t *testing.T) {
assert.Equal(t, userJSON, rec.Body.String())
}

// JSONPretty
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context)
err = c.JSONPretty(http.StatusOK, user{1, "Jon Snow"}, "\t")
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(t, userJSONPretty, rec.Body.String())
}

// JSON (error)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context)
Expand Down Expand Up @@ -106,6 +116,16 @@ func TestContext(t *testing.T) {
err = c.XML(http.StatusOK, make(chan bool))
assert.Error(t, err)

// XMLPretty
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context)
err = c.XMLPretty(http.StatusOK, user{1, "Jon Snow"}, "\t")
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(t, xml.Header+userXMLPretty, rec.Body.String())
}

// String
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context)
Expand Down
10 changes: 10 additions & 0 deletions echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ const (
invalidContent = "invalid content"
)

const userJSONPretty = `{
"id": 1,
"name": "Jon Snow"
}`

const userXMLPretty = `<user>
<id>1</id>
<name>Jon Snow</name>
</user>`

func TestEcho(t *testing.T) {
e := New()
req, _ := http.NewRequest(GET, "/", nil)
Expand Down

0 comments on commit 20954af

Please sign in to comment.