From 20954afd66197fecb91083726385a99a674ab11c Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Fri, 9 Dec 2016 11:13:55 -0800 Subject: [PATCH] fixed json, xml pretty print Signed-off-by: Vishal Rana --- context.go | 2 +- context_test.go | 20 ++++++++++++++++++++ echo_test.go | 10 ++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/context.go b/context.go index ef83c3c04..bcbef034e 100644 --- a/context.go +++ b/context.go @@ -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) { diff --git a/context_test.go b/context_test.go index 4d4e62f47..c00686512 100644 --- a/context_test.go +++ b/context_test.go @@ -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) @@ -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) diff --git a/echo_test.go b/echo_test.go index e04cf1ff8..88a1deb63 100644 --- a/echo_test.go +++ b/echo_test.go @@ -29,6 +29,16 @@ const ( invalidContent = "invalid content" ) +const userJSONPretty = `{ + "id": 1, + "name": "Jon Snow" +}` + +const userXMLPretty = ` + 1 + Jon Snow +` + func TestEcho(t *testing.T) { e := New() req, _ := http.NewRequest(GET, "/", nil)