From 71819ac9c3cb76bd87383c5840cf8517a2f8f535 Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Tue, 30 Dec 2014 20:09:45 -0800 Subject: [PATCH] Use full json content type with charset parameter --- api_test.go | 4 ++-- app.go | 6 +++--- app_test.go | 4 ++-- helpers.go | 2 +- images.go | 2 +- tags.go | 2 ++ 6 files changed, 11 insertions(+), 9 deletions(-) diff --git a/api_test.go b/api_test.go index 76c3b348..3db3a4d4 100644 --- a/api_test.go +++ b/api_test.go @@ -53,7 +53,7 @@ func TestCheckAPI(t *testing.T) { checkResponse(t, "issuing api base check", resp, http.StatusOK) checkHeaders(t, resp, http.Header{ - "Content-Type": []string{"application/json"}, + "Content-Type": []string{"application/json; charset=utf-8"}, "Content-Length": []string{"2"}, }) @@ -221,7 +221,7 @@ func TestManifestAPI(t *testing.T) { // TODO(stevvooe): Shoot. The error setup is not working out. The content- // type headers are being set after writing the status code. - // if resp.Header.Get("Content-Type") != "application/json" { + // if resp.Header.Get("Content-Type") != "application/json; charset=utf-8" { // t.Fatalf("unexpected content type: %v != 'application/json'", // resp.Header.Get("Content-Type")) // } diff --git a/app.go b/app.go index d8276cec..13781b78 100644 --- a/app.go +++ b/app.go @@ -215,7 +215,7 @@ func (app *App) authorized(w http.ResponseWriter, r *http.Request, context *Cont // under which name is not set and we still allow access is when the // base route is accessed. This section prevents us from making that // mistake elsewhere in the code, allowing any operation to proceed. - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(http.StatusForbidden) var errs v2.Errors @@ -227,7 +227,7 @@ func (app *App) authorized(w http.ResponseWriter, r *http.Request, context *Cont if err := app.accessController.Authorized(r, accessRecords...); err != nil { switch err := err.(type) { case auth.Challenge: - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") err.ServeHTTP(w, r) var errs v2.Errors @@ -253,7 +253,7 @@ func (app *App) authorized(w http.ResponseWriter, r *http.Request, context *Cont func apiBase(w http.ResponseWriter, r *http.Request) { const emptyJSON = "{}" // Provide a simple /v2/ 200 OK response with empty json response. - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Header().Set("Content-Length", fmt.Sprint(len(emptyJSON))) fmt.Fprint(w, emptyJSON) diff --git a/app_test.go b/app_test.go index d3d6d979..4d9535f7 100644 --- a/app_test.go +++ b/app_test.go @@ -173,8 +173,8 @@ func TestNewApp(t *testing.T) { t.Fatalf("unexpected status code during request: %v", err) } - if req.Header.Get("Content-Type") != "application/json" { - t.Fatalf("unexpected content-type: %v != %v", req.Header.Get("Content-Type"), "application/json") + if req.Header.Get("Content-Type") != "application/json; charset=utf-8" { + t.Fatalf("unexpected content-type: %v != %v", req.Header.Get("Content-Type"), "application/json; charset=utf-8") } expectedAuthHeader := "Bearer realm=\"realm-test\",service=\"service-test\"" diff --git a/helpers.go b/helpers.go index 6fce84a2..6bcb4ae8 100644 --- a/helpers.go +++ b/helpers.go @@ -10,7 +10,7 @@ import ( // 'application/json'. If a different status code is required, call // ResponseWriter.WriteHeader before this function. func serveJSON(w http.ResponseWriter, v interface{}) error { - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") enc := json.NewEncoder(w) if err := enc.Encode(v); err != nil { diff --git a/images.go b/images.go index 6a972109..dbe1c3ae 100644 --- a/images.go +++ b/images.go @@ -46,7 +46,7 @@ func (imh *imageManifestHandler) GetImageManifest(w http.ResponseWriter, r *http return } - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Header().Set("Content-Length", fmt.Sprint(len(manifest.Raw))) w.Write(manifest.Raw) } diff --git a/tags.go b/tags.go index 458e6d1a..18f6add2 100644 --- a/tags.go +++ b/tags.go @@ -47,6 +47,8 @@ func (th *tagsHandler) GetTags(w http.ResponseWriter, r *http.Request) { return } + w.Header().Set("Content-Type", "application/json; charset=utf-8") + enc := json.NewEncoder(w) if err := enc.Encode(tagsAPIResponse{ Name: th.Name,