mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 21:06:39 +08:00
Merge 7d753188a7c06af5fa9fe8fb120bdb4d180cf659 into 22f118f3b604734b4782f0dd767171687ffd7774
This commit is contained in:
commit
29d219126f
@ -331,6 +331,15 @@ func (c *Context) JSON(code int, obj interface{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Serializes the given struct as JSON into the response body.
|
||||||
|
// It also sets the Content-Type as "application/vnd.api+json".
|
||||||
|
func (c *Context) JSONAPI(code int, obj interface{}) {
|
||||||
|
c.writermem.WriteHeader(code)
|
||||||
|
if err := render.WriteJSONAPI(c.Writer, obj); err != nil {
|
||||||
|
c.renderError(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Serializes the given struct as XML into the response body.
|
// Serializes the given struct as XML into the response body.
|
||||||
// It also sets the Content-Type as "application/xml".
|
// It also sets the Content-Type as "application/xml".
|
||||||
func (c *Context) XML(code int, obj interface{}) {
|
func (c *Context) XML(code int, obj interface{}) {
|
||||||
|
@ -219,6 +219,17 @@ func TestContextRenderJSON(t *testing.T) {
|
|||||||
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8")
|
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/vnd.api+json
|
||||||
|
func TestContextRenderJSON(t *testing.T) {
|
||||||
|
c, w, _ := createTestContext()
|
||||||
|
c.JSONAPI(201, H{"foo": "bar"})
|
||||||
|
|
||||||
|
assert.Equal(t, w.Code, 201)
|
||||||
|
assert.Equal(t, w.Body.String(), "{\"foo\":\"bar\"}\n")
|
||||||
|
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/vnd.api+json")
|
||||||
|
}
|
||||||
|
|
||||||
// Tests that the response is serialized as JSON
|
// Tests that the response is serialized as JSON
|
||||||
// and Content-Type is set to application/json
|
// and Content-Type is set to application/json
|
||||||
func TestContextRenderIndentedJSON(t *testing.T) {
|
func TestContextRenderIndentedJSON(t *testing.T) {
|
||||||
|
@ -13,18 +13,29 @@ type (
|
|||||||
JSON struct {
|
JSON struct {
|
||||||
Data interface{}
|
Data interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JSONAPI struct {
|
||||||
|
Data interface{}
|
||||||
|
}
|
||||||
|
|
||||||
IndentedJSON struct {
|
IndentedJSON struct {
|
||||||
Data interface{}
|
Data interface{}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
var jsonContentType = []string{"application/json; charset=utf-8"}
|
var (
|
||||||
|
jsonContentType = []string{"application/json; charset=utf-8"}
|
||||||
|
jsonAPIContentType = []string{"application/vnd.api+json"}
|
||||||
|
)
|
||||||
|
|
||||||
func (r JSON) Render(w http.ResponseWriter) error {
|
func (r JSON) Render(w http.ResponseWriter) error {
|
||||||
return WriteJSON(w, r.Data)
|
return WriteJSON(w, r.Data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r JSONAPI) Render(w http.ResponseWriter) error {
|
||||||
|
return WriteJSONAPI(w, r.Data)
|
||||||
|
}
|
||||||
|
|
||||||
func (r IndentedJSON) Render(w http.ResponseWriter) error {
|
func (r IndentedJSON) Render(w http.ResponseWriter) error {
|
||||||
w.Header()["Content-Type"] = jsonContentType
|
w.Header()["Content-Type"] = jsonContentType
|
||||||
jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
|
jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
|
||||||
@ -39,3 +50,8 @@ func WriteJSON(w http.ResponseWriter, obj interface{}) error {
|
|||||||
w.Header()["Content-Type"] = jsonContentType
|
w.Header()["Content-Type"] = jsonContentType
|
||||||
return json.NewEncoder(w).Encode(obj)
|
return json.NewEncoder(w).Encode(obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WriteJSONAPI(w http.ResponseWriter, obj interface{}) error {
|
||||||
|
w.Header()["Content-Type"] = jsonAPIContentType
|
||||||
|
return json.NewEncoder(w).Encode(obj)
|
||||||
|
}
|
||||||
|
@ -29,6 +29,19 @@ func TestRenderJSON(t *testing.T) {
|
|||||||
assert.Equal(t, w.Header().Get("Content-Type"), "application/json; charset=utf-8")
|
assert.Equal(t, w.Header().Get("Content-Type"), "application/json; charset=utf-8")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRenderJSONAPI(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"foo": "bar",
|
||||||
|
}
|
||||||
|
|
||||||
|
err := (JSONAPI{data}).Render(w)
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, w.Body.String(), "{\"foo\":\"bar\"}\n")
|
||||||
|
assert.Equal(t, w.Header().Get("Content-Type"), "application/vnd.api+json")
|
||||||
|
}
|
||||||
|
|
||||||
func TestRenderIndentedJSON(t *testing.T) {
|
func TestRenderIndentedJSON(t *testing.T) {
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
data := map[string]interface{}{
|
data := map[string]interface{}{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user