-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #693
- Loading branch information
Showing
8 changed files
with
133 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2018 Gin Core Team. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build go1.7 | ||
|
||
package gin | ||
|
||
import ( | ||
"github.com/gin-gonic/gin/render" | ||
) | ||
|
||
// 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}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2018 Gin Core Team. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build go1.7 | ||
|
||
package gin | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// 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) { | ||
w := httptest.NewRecorder() | ||
c, _ := CreateTestContext(w) | ||
c.PureJSON(http.StatusCreated, H{"foo": "bar", "html": "<b>"}) | ||
assert.Equal(t, http.StatusCreated, w.Code) | ||
assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"<b>\"}\n", w.Body.String()) | ||
assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2018 Gin Core Team. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build go1.7 | ||
|
||
package render | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin/json" | ||
) | ||
|
||
type PureJSON struct { | ||
Data interface{} | ||
} | ||
|
||
func (r PureJSON) Render(w http.ResponseWriter) error { | ||
r.WriteContentType(w) | ||
encoder := json.NewEncoder(w) | ||
encoder.SetEscapeHTML(false) | ||
return encoder.Encode(r.Data) | ||
} | ||
|
||
func (r PureJSON) WriteContentType(w http.ResponseWriter) { | ||
writeContentType(w, jsonContentType) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2018 Gin Core Team. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build go1.7 | ||
|
||
package render | ||
|
||
import ( | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters