Use full json content type with charset parameter

This commit is contained in:
Stephen J Day 2014-12-30 20:09:45 -08:00
parent 83b899dab6
commit 71819ac9c3
6 changed files with 11 additions and 9 deletions

View file

@ -53,7 +53,7 @@ func TestCheckAPI(t *testing.T) {
checkResponse(t, "issuing api base check", resp, http.StatusOK) checkResponse(t, "issuing api base check", resp, http.StatusOK)
checkHeaders(t, resp, http.Header{ checkHeaders(t, resp, http.Header{
"Content-Type": []string{"application/json"}, "Content-Type": []string{"application/json; charset=utf-8"},
"Content-Length": []string{"2"}, "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- // TODO(stevvooe): Shoot. The error setup is not working out. The content-
// type headers are being set after writing the status code. // 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'", // t.Fatalf("unexpected content type: %v != 'application/json'",
// resp.Header.Get("Content-Type")) // resp.Header.Get("Content-Type"))
// } // }

6
app.go
View file

@ -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 // 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 // base route is accessed. This section prevents us from making that
// mistake elsewhere in the code, allowing any operation to proceed. // 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) w.WriteHeader(http.StatusForbidden)
var errs v2.Errors 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 { if err := app.accessController.Authorized(r, accessRecords...); err != nil {
switch err := err.(type) { switch err := err.(type) {
case auth.Challenge: case auth.Challenge:
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json; charset=utf-8")
err.ServeHTTP(w, r) err.ServeHTTP(w, r)
var errs v2.Errors 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) { func apiBase(w http.ResponseWriter, r *http.Request) {
const emptyJSON = "{}" const emptyJSON = "{}"
// Provide a simple /v2/ 200 OK response with empty json response. // 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))) w.Header().Set("Content-Length", fmt.Sprint(len(emptyJSON)))
fmt.Fprint(w, emptyJSON) fmt.Fprint(w, emptyJSON)

View file

@ -173,8 +173,8 @@ func TestNewApp(t *testing.T) {
t.Fatalf("unexpected status code during request: %v", err) t.Fatalf("unexpected status code during request: %v", err)
} }
if 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") 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\"" expectedAuthHeader := "Bearer realm=\"realm-test\",service=\"service-test\""

View file

@ -10,7 +10,7 @@ import (
// 'application/json'. If a different status code is required, call // 'application/json'. If a different status code is required, call
// ResponseWriter.WriteHeader before this function. // ResponseWriter.WriteHeader before this function.
func serveJSON(w http.ResponseWriter, v interface{}) error { 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) enc := json.NewEncoder(w)
if err := enc.Encode(v); err != nil { if err := enc.Encode(v); err != nil {

View file

@ -46,7 +46,7 @@ func (imh *imageManifestHandler) GetImageManifest(w http.ResponseWriter, r *http
return 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.Header().Set("Content-Length", fmt.Sprint(len(manifest.Raw)))
w.Write(manifest.Raw) w.Write(manifest.Raw)
} }

View file

@ -47,6 +47,8 @@ func (th *tagsHandler) GetTags(w http.ResponseWriter, r *http.Request) {
return return
} }
w.Header().Set("Content-Type", "application/json; charset=utf-8")
enc := json.NewEncoder(w) enc := json.NewEncoder(w)
if err := enc.Encode(tagsAPIResponse{ if err := enc.Encode(tagsAPIResponse{
Name: th.Name, Name: th.Name,