Skip to content

Commit

Permalink
Merge bcf1052 into f931d1e
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip committed Aug 31, 2016
2 parents f931d1e + bcf1052 commit 66d8608
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
6 changes: 6 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ func (c *Context) JSON(code int, obj interface{}) {
}
}

// 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
23 changes: 21 additions & 2 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,31 @@ func TestContextGetCookie(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) {
c, w, _ := CreateTestContext()
c.JSON(201, H{"foo": "bar"})
c.JSON(201, H{"foo": "bar", "html": "<b>"})

assert.Equal(t, w.Code, 201)
assert.Equal(t, w.Body.String(), "{\"foo\":\"bar\"}\n")
assert.Equal(
t,
w.Body.String(),
"{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}\n")
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8")
}

// 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,
w.Body.String(),
"{\"foo\":\"bar\",\"html\":\"<b>\"}\n")
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8")
}

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

PureJSON struct {
Data interface{}
}

IndentedJSON struct {
Data interface{}
}
Expand All @@ -25,6 +29,13 @@ func (r JSON) Render(w http.ResponseWriter) error {
return WriteJSON(w, r.Data)
}

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 {
writeContentType(w, jsonContentType)
jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
Expand Down
25 changes: 23 additions & 2 deletions render/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,34 @@ import (
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, w.Body.String(), "{\"foo\":\"bar\"}\n")
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")
}

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,
w.Body.String(),
"{\"foo\":\"bar\",\"html\":\"<b>\"}\n")
assert.Equal(t, w.Header().Get("Content-Type"), "application/json; charset=utf-8")
}

Expand Down

0 comments on commit 66d8608

Please sign in to comment.