From 1ff9950a551e1f96415cf918cd306b4eefb65c07 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sat, 3 Nov 2018 12:03:06 -0700 Subject: Remove charset=utf-8 from JSON responses See: https://www.iana.org/assignments/media-types/application/json --- http/response/json/json.go | 18 ++++++++++-------- http/response/json/json_test.go | 18 +++++++++--------- 2 files changed, 19 insertions(+), 17 deletions(-) (limited to 'http') diff --git a/http/response/json/json.go b/http/response/json/json.go index 7cd9fbc..dfd4978 100644 --- a/http/response/json/json.go +++ b/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() } diff --git a/http/response/json/json_test.go b/http/response/json/json_test.go index 9531615..ee5580b 100644 --- a/http/response/json/json_test.go +++ b/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) -- cgit v1.2.3