mirror of https://github.com/gin-gonic/gin.git
Adds supports for custom JSON Content-type
This commit is contained in:
parent
22f118f3b6
commit
a7c957af7d
|
@ -219,6 +219,18 @@ 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
|
||||||
|
// we change the content-type before
|
||||||
|
func TestContextRenderAPIJSON(t *testing.T) {
|
||||||
|
c, w, _ := createTestContext()
|
||||||
|
c.Header("Content-Type", "application/vnd.api+json")
|
||||||
|
c.JSON(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) {
|
||||||
|
|
|
@ -26,7 +26,7 @@ func (r JSON) Render(w http.ResponseWriter) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r IndentedJSON) Render(w http.ResponseWriter) error {
|
func (r IndentedJSON) Render(w http.ResponseWriter) error {
|
||||||
w.Header()["Content-Type"] = jsonContentType
|
writeContentType(w, jsonContentType)
|
||||||
jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
|
jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -36,6 +36,6 @@ func (r IndentedJSON) Render(w http.ResponseWriter) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteJSON(w http.ResponseWriter, obj interface{}) error {
|
func WriteJSON(w http.ResponseWriter, obj interface{}) error {
|
||||||
w.Header()["Content-Type"] = jsonContentType
|
writeContentType(w, jsonContentType)
|
||||||
return json.NewEncoder(w).Encode(obj)
|
return json.NewEncoder(w).Encode(obj)
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,3 +21,10 @@ var (
|
||||||
_ HTMLRender = HTMLDebug{}
|
_ HTMLRender = HTMLDebug{}
|
||||||
_ HTMLRender = HTMLProduction{}
|
_ HTMLRender = HTMLProduction{}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func writeContentType(w http.ResponseWriter, value []string) {
|
||||||
|
header := w.Header()
|
||||||
|
if val := header["Content-Type"]; len(val) == 0 {
|
||||||
|
header["Content-Type"] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -23,10 +23,8 @@ func (r String) Render(w http.ResponseWriter) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteString(w http.ResponseWriter, format string, data []interface{}) {
|
func WriteString(w http.ResponseWriter, format string, data []interface{}) {
|
||||||
header := w.Header()
|
writeContentType(w, plainContentType)
|
||||||
if _, exist := header["Content-Type"]; !exist {
|
|
||||||
header["Content-Type"] = plainContentType
|
|
||||||
}
|
|
||||||
if len(data) > 0 {
|
if len(data) > 0 {
|
||||||
fmt.Fprintf(w, format, data...)
|
fmt.Fprintf(w, format, data...)
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue