diff --git a/httpbin/handlers.go b/httpbin/handlers.go index 5bfaf93d7df1c58e7da5ad677d54c5533fbf65fe..2d856e61d1acfb174065a78cfa615122ae3fa00e 100644 --- a/httpbin/handlers.go +++ b/httpbin/handlers.go @@ -138,7 +138,7 @@ func (h *HTTPBin) Status(w http.ResponseWriter, r *http.Request) { 406: &statusCase{ body: notAcceptableBody, headers: map[string]string{ - "Content-Type": "application/json; encoding=utf-8", + "Content-Type": jsonContentType, }, }, 407: &statusCase{ @@ -179,7 +179,7 @@ func (h *HTTPBin) ResponseHeaders(w http.ResponseWriter, r *http.Request) { } body, _ := json.Marshal(args) if contentType := w.Header().Get("Content-Type"); contentType == "" { - w.Header().Set("Content-Type", "application/json; encoding=utf-8") + w.Header().Set("Content-Type", jsonContentType) } w.Write(body) } diff --git a/httpbin/handlers_test.go b/httpbin/handlers_test.go index 36919ceb70093ed7acbb7db7b98c0cdc4ce2967a..3037a280861af339b8b30e0a71508dfed07add63 100644 --- a/httpbin/handlers_test.go +++ b/httpbin/handlers_test.go @@ -92,7 +92,7 @@ func TestGet__Basic(t *testing.T) { handler.ServeHTTP(w, r) assertStatusCode(t, w, http.StatusOK) - assertContentType(t, w, "application/json; encoding=utf-8") + assertContentType(t, w, jsonContentType) var resp *bodyResponse err := json.Unmarshal(w.Body.Bytes(), &resp) @@ -135,7 +135,7 @@ func TestGet__WithParams(t *testing.T) { handler.ServeHTTP(w, r) assertStatusCode(t, w, http.StatusOK) - assertContentType(t, w, "application/json; encoding=utf-8") + assertContentType(t, w, jsonContentType) var resp *bodyResponse err := json.Unmarshal(w.Body.Bytes(), &resp) @@ -246,7 +246,7 @@ func TestIP(t *testing.T) { handler.ServeHTTP(w, r) assertStatusCode(t, w, http.StatusOK) - assertContentType(t, w, "application/json; encoding=utf-8") + assertContentType(t, w, jsonContentType) var resp *ipResponse err := json.Unmarshal(w.Body.Bytes(), &resp) @@ -266,7 +266,7 @@ func TestUserAgent(t *testing.T) { handler.ServeHTTP(w, r) assertStatusCode(t, w, http.StatusOK) - assertContentType(t, w, "application/json; encoding=utf-8") + assertContentType(t, w, jsonContentType) var resp *userAgentResponse err := json.Unmarshal(w.Body.Bytes(), &resp) @@ -289,7 +289,7 @@ func TestHeaders(t *testing.T) { handler.ServeHTTP(w, r) assertStatusCode(t, w, http.StatusOK) - assertContentType(t, w, "application/json; encoding=utf-8") + assertContentType(t, w, jsonContentType) var resp *headersResponse err := json.Unmarshal(w.Body.Bytes(), &resp) @@ -314,7 +314,7 @@ func TestPost__EmptyBody(t *testing.T) { handler.ServeHTTP(w, r) assertStatusCode(t, w, http.StatusOK) - assertContentType(t, w, "application/json; encoding=utf-8") + assertContentType(t, w, jsonContentType) var resp *bodyResponse err := json.Unmarshal(w.Body.Bytes(), &resp) @@ -342,7 +342,7 @@ func TestPost__FormEncodedBody(t *testing.T) { handler.ServeHTTP(w, r) assertStatusCode(t, w, http.StatusOK) - assertContentType(t, w, "application/json; encoding=utf-8") + assertContentType(t, w, jsonContentType) var resp *bodyResponse err := json.Unmarshal(w.Body.Bytes(), &resp) @@ -378,7 +378,7 @@ func TestPost__FormEncodedBodyNoContentType(t *testing.T) { handler.ServeHTTP(w, r) assertStatusCode(t, w, http.StatusOK) - assertContentType(t, w, "application/json; encoding=utf-8") + assertContentType(t, w, jsonContentType) var resp *bodyResponse err := json.Unmarshal(w.Body.Bytes(), &resp) @@ -426,7 +426,7 @@ func TestPost__MultiPartBody(t *testing.T) { handler.ServeHTTP(w, r) assertStatusCode(t, w, http.StatusOK) - assertContentType(t, w, "application/json; encoding=utf-8") + assertContentType(t, w, jsonContentType) var resp *bodyResponse err := json.Unmarshal(w.Body.Bytes(), &resp) @@ -488,7 +488,7 @@ func TestPost__JSON(t *testing.T) { handler.ServeHTTP(w, r) assertStatusCode(t, w, http.StatusOK) - assertContentType(t, w, "application/json; encoding=utf-8") + assertContentType(t, w, jsonContentType) var resp *bodyResponse err := json.Unmarshal(w.Body.Bytes(), &resp) @@ -536,7 +536,7 @@ func TestPost__BodyTooBig(t *testing.T) { handler.ServeHTTP(w, r) assertStatusCode(t, w, http.StatusBadRequest) - assertContentType(t, w, "application/json; encoding=utf-8") + assertContentType(t, w, jsonContentType) } func TestPost__QueryParams(t *testing.T) { @@ -550,7 +550,7 @@ func TestPost__QueryParams(t *testing.T) { handler.ServeHTTP(w, r) assertStatusCode(t, w, http.StatusOK) - assertContentType(t, w, "application/json; encoding=utf-8") + assertContentType(t, w, jsonContentType) var resp *bodyResponse err := json.Unmarshal(w.Body.Bytes(), &resp) @@ -587,7 +587,7 @@ func TestPost__QueryParamsAndBody(t *testing.T) { handler.ServeHTTP(w, r) assertStatusCode(t, w, http.StatusOK) - assertContentType(t, w, "application/json; encoding=utf-8") + assertContentType(t, w, jsonContentType) var resp *bodyResponse err := json.Unmarshal(w.Body.Bytes(), &resp) @@ -692,7 +692,7 @@ func TestResponseHeaders__OK(t *testing.T) { handler.ServeHTTP(w, r) assertStatusCode(t, w, http.StatusOK) - assertContentType(t, w, "application/json; encoding=utf-8") + assertContentType(t, w, jsonContentType) for k, expectedValues := range headers { values, ok := w.HeaderMap[k] diff --git a/httpbin/helpers.go b/httpbin/helpers.go index 9ac8f4230d022d4536d5d25bed165c249369cc94..ccc08cc194a8a39d7d33783ebf763c64e9d15064 100644 --- a/httpbin/helpers.go +++ b/httpbin/helpers.go @@ -47,7 +47,7 @@ func getURL(r *http.Request) *url.URL { } func writeJSON(w http.ResponseWriter, body []byte, status int) { - w.Header().Set("Content-Type", "application/json; encoding=utf-8") + w.Header().Set("Content-Type", jsonContentType) w.WriteHeader(status) w.Write(body) } diff --git a/httpbin/httpbin.go b/httpbin/httpbin.go index cac976f1e19b73b40aa80e62daffd9a3d0246cd5..cd1a1aadae027e7fafcc61b9d56dd99feeef5fd6 100644 --- a/httpbin/httpbin.go +++ b/httpbin/httpbin.go @@ -5,6 +5,8 @@ import ( "net/url" ) +const jsonContentType = "application/json; encoding=utf-8" + type headersResponse struct { Headers http.Header `json:"headers"` }