Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PureJSON renderer #694

Merged
merged 19 commits into from
Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,12 @@ func (c *Context) JSON(code int, obj interface{}) {
c.Render(code, render.JSON{Data: obj})
}

// PureJSON serializes the given struct as JSON into the response body.
// PureJSON, unlike JSON, does not replace special html characters with their unicode entities.
func (c *Context) PureJSON(code int, obj interface{}) {
c.Render(code, render.PureJSON{Data: obj})
}

// XML serializes the given struct as XML into the response body.
// It also sets the Content-Type as "application/xml".
func (c *Context) XML(code int, obj interface{}) {
Expand Down
21 changes: 18 additions & 3 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,14 +570,29 @@ func TestContextRenderPanicIfErr(t *testing.T) {

// Tests that the response is serialized as JSON
// and Content-Type is set to application/json
// and special HTML characters are escaped
func TestContextRenderJSON(t *testing.T) {
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)

c.JSON(201, H{"foo": "bar"})
c.JSON(http.StatusCreated, H{"foo": "bar", "html": "<b>"})

assert.Equal(t, 201, w.Code)
assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())
assert.Equal(t, http.StatusCreated, w.Code)
assert.Equal(
t,
"{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}\n",
w.Body.String())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest to use one line

assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))
}

// Tests that the response is serialized as JSON
// and Content-Type is set to application/json
// and special HTML characters are preserved
func TestContextRenderPureJSON(t *testing.T) {
c, w, _ := CreateTestContext()
c.PureJSON(201, H{"foo": "bar", "html": "<b>"})
assert.Equal(t, w.Code, 201)
assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"<b>\"}\n", w.Body.String())
assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))
}

Expand Down
11 changes: 11 additions & 0 deletions render/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ type JSON struct {
Data interface{}
}

type PureJSON struct {
Data interface{}
}

type IndentedJSON struct {
Data interface{}
}
Expand Down Expand Up @@ -49,6 +53,13 @@ func WriteJSON(w http.ResponseWriter, obj interface{}) error {
return nil
}

func (r PureJSON) Render(w http.ResponseWriter) error {
writeContentType(w, jsonContentType)
encoder := json.NewEncoder(w)
encoder.SetEscapeHTML(false)
return encoder.Encode(r.Data)
}

func (r IndentedJSON) Render(w http.ResponseWriter) error {
r.WriteContentType(w)
jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
Expand Down
19 changes: 18 additions & 1 deletion render/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,30 @@ func TestRenderMsgPack(t *testing.T) {
func TestRenderJSON(t *testing.T) {
w := httptest.NewRecorder()
data := map[string]interface{}{
"foo": "bar",
"foo": "bar",
"html": "<b>",
}

err := (JSON{data}).Render(w)

assert.NoError(t, err)
assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())
assert.Equal(
t,
w.Body.String(),
"{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}\n")
assert.Equal(t, w.Header().Get("Content-Type"), "application/json; charset=utf-8")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be assert.Equal(t, expect, actual)

}

func TestRenderPureJSON(t *testing.T) {
w := httptest.NewRecorder()
data := map[string]interface{}{
"foo": "bar",
"html": "<b>",
}
err := (PureJSON{data}).Render(w)
assert.NoError(t, err)
assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"<b>\"}\n", w.Body.String())
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
}

Expand Down