Procházet zdrojové kódy

Remove charset=utf-8 from JSON responses

See: https://www.iana.org/assignments/media-types/application/json
Frédéric Guillot před 7 roky
rodič
revize
1ff9950a55
2 změnil soubory, kde provedl 19 přidání a 17 odebrání
  1. 10 8
      http/response/json/json.go
  2. 9 9
      http/response/json/json_test.go

+ 10 - 8
http/response/json/json.go

@@ -13,10 +13,12 @@ import (
 	"miniflux.app/logger"
 )
 
+const contentTypeHeader = `application/json`
+
 // OK creates a new JSON response with a 200 status code.
 func OK(w http.ResponseWriter, r *http.Request, body interface{}) {
 	builder := response.New(w, r)
-	builder.WithHeader("Content-Type", "application/json; charset=utf-8")
+	builder.WithHeader("Content-Type", contentTypeHeader)
 	builder.WithBody(toJSON(body))
 	builder.Write()
 }
@@ -25,7 +27,7 @@ func OK(w http.ResponseWriter, r *http.Request, body interface{}) {
 func Created(w http.ResponseWriter, r *http.Request, body interface{}) {
 	builder := response.New(w, r)
 	builder.WithStatus(http.StatusCreated)
-	builder.WithHeader("Content-Type", "application/json; charset=utf-8")
+	builder.WithHeader("Content-Type", contentTypeHeader)
 	builder.WithBody(toJSON(body))
 	builder.Write()
 }
@@ -34,7 +36,7 @@ func Created(w http.ResponseWriter, r *http.Request, body interface{}) {
 func NoContent(w http.ResponseWriter, r *http.Request) {
 	builder := response.New(w, r)
 	builder.WithStatus(http.StatusNoContent)
-	builder.WithHeader("Content-Type", "application/json; charset=utf-8")
+	builder.WithHeader("Content-Type", contentTypeHeader)
 	builder.Write()
 }
 
@@ -44,7 +46,7 @@ func ServerError(w http.ResponseWriter, r *http.Request, err error) {
 
 	builder := response.New(w, r)
 	builder.WithStatus(http.StatusInternalServerError)
-	builder.WithHeader("Content-Type", "application/json; charset=utf-8")
+	builder.WithHeader("Content-Type", contentTypeHeader)
 	builder.WithBody(toJSONError(err))
 	builder.Write()
 }
@@ -55,7 +57,7 @@ func BadRequest(w http.ResponseWriter, r *http.Request, err error) {
 
 	builder := response.New(w, r)
 	builder.WithStatus(http.StatusBadRequest)
-	builder.WithHeader("Content-Type", "application/json; charset=utf-8")
+	builder.WithHeader("Content-Type", contentTypeHeader)
 	builder.WithBody(toJSONError(err))
 	builder.Write()
 }
@@ -66,7 +68,7 @@ func Unauthorized(w http.ResponseWriter, r *http.Request) {
 
 	builder := response.New(w, r)
 	builder.WithStatus(http.StatusUnauthorized)
-	builder.WithHeader("Content-Type", "application/json; charset=utf-8")
+	builder.WithHeader("Content-Type", contentTypeHeader)
 	builder.WithBody(toJSONError(errors.New("Access Unauthorized")))
 	builder.Write()
 }
@@ -77,7 +79,7 @@ func Forbidden(w http.ResponseWriter, r *http.Request) {
 
 	builder := response.New(w, r)
 	builder.WithStatus(http.StatusForbidden)
-	builder.WithHeader("Content-Type", "application/json; charset=utf-8")
+	builder.WithHeader("Content-Type", contentTypeHeader)
 	builder.WithBody(toJSONError(errors.New("Access Forbidden")))
 	builder.Write()
 }
@@ -88,7 +90,7 @@ func NotFound(w http.ResponseWriter, r *http.Request) {
 
 	builder := response.New(w, r)
 	builder.WithStatus(http.StatusNotFound)
-	builder.WithHeader("Content-Type", "application/json; charset=utf-8")
+	builder.WithHeader("Content-Type", contentTypeHeader)
 	builder.WithBody(toJSONError(errors.New("Resource Not Found")))
 	builder.Write()
 }

+ 9 - 9
http/response/json/json_test.go

@@ -39,7 +39,7 @@ func TestOKResponse(t *testing.T) {
 		t.Fatalf(`Unexpected body, got %q instead of %q`, actualBody, expectedBody)
 	}
 
-	expectedContentType := "application/json; charset=utf-8"
+	expectedContentType := contentTypeHeader
 	actualContentType := resp.Header.Get("Content-Type")
 	if actualContentType != expectedContentType {
 		t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
@@ -72,7 +72,7 @@ func TestCreatedResponse(t *testing.T) {
 		t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
 	}
 
-	expectedContentType := "application/json; charset=utf-8"
+	expectedContentType := contentTypeHeader
 	actualContentType := resp.Header.Get("Content-Type")
 	if actualContentType != expectedContentType {
 		t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
@@ -105,7 +105,7 @@ func TestNoContentResponse(t *testing.T) {
 		t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
 	}
 
-	expectedContentType := "application/json; charset=utf-8"
+	expectedContentType := contentTypeHeader
 	actualContentType := resp.Header.Get("Content-Type")
 	if actualContentType != expectedContentType {
 		t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
@@ -140,7 +140,7 @@ func TestServerErrorResponse(t *testing.T) {
 		t.Fatalf(`Unexpected body, got %q instead of %q`, actualBody, expectedBody)
 	}
 
-	expectedContentType := "application/json; charset=utf-8"
+	expectedContentType := contentTypeHeader
 	actualContentType := resp.Header.Get("Content-Type")
 	if actualContentType != expectedContentType {
 		t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
@@ -173,7 +173,7 @@ func TestBadRequestResponse(t *testing.T) {
 		t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
 	}
 
-	expectedContentType := "application/json; charset=utf-8"
+	expectedContentType := contentTypeHeader
 	actualContentType := resp.Header.Get("Content-Type")
 	if actualContentType != expectedContentType {
 		t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
@@ -206,7 +206,7 @@ func TestUnauthorizedResponse(t *testing.T) {
 		t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
 	}
 
-	expectedContentType := "application/json; charset=utf-8"
+	expectedContentType := contentTypeHeader
 	actualContentType := resp.Header.Get("Content-Type")
 	if actualContentType != expectedContentType {
 		t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
@@ -239,7 +239,7 @@ func TestForbiddenResponse(t *testing.T) {
 		t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
 	}
 
-	expectedContentType := "application/json; charset=utf-8"
+	expectedContentType := contentTypeHeader
 	actualContentType := resp.Header.Get("Content-Type")
 	if actualContentType != expectedContentType {
 		t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
@@ -272,7 +272,7 @@ func TestNotFoundResponse(t *testing.T) {
 		t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
 	}
 
-	expectedContentType := "application/json; charset=utf-8"
+	expectedContentType := contentTypeHeader
 	actualContentType := resp.Header.Get("Content-Type")
 	if actualContentType != expectedContentType {
 		t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)
@@ -305,7 +305,7 @@ func TestBuildInvalidJSONResponse(t *testing.T) {
 		t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
 	}
 
-	expectedContentType := "application/json; charset=utf-8"
+	expectedContentType := contentTypeHeader
 	actualContentType := resp.Header.Get("Content-Type")
 	if actualContentType != expectedContentType {
 		t.Fatalf(`Unexpected content type, got %q instead of %q`, actualContentType, expectedContentType)