Procházet zdrojové kódy

Adds supports for custom JSON Content-type

Manu Mtz-Almeida před 10 roky
rodič
revize
a7c957af7d
4 změnil soubory, kde provedl 23 přidání a 6 odebrání
  1. 12 0
      context_test.go
  2. 2 2
      render/json.go
  3. 7 0
      render/render.go
  4. 2 4
      render/text.go

+ 12 - 0
context_test.go

@@ -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) {

+ 2 - 2
render/json.go

@@ -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)
 }
 }

+ 7 - 0
render/render.go

@@ -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
+	}
+}

+ 2 - 4
render/text.go

@@ -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()
-	if _, exist := header["Content-Type"]; !exist {
-		header["Content-Type"] = plainContentType
-	}
+	writeContentType(w, plainContentType)
+
 	if len(data) > 0 {
 	if len(data) > 0 {
 		fmt.Fprintf(w, format, data...)
 		fmt.Fprintf(w, format, data...)
 	} else {
 	} else {