From c0546840d06e2530b3b5292d20eeaa8121bb0844 Mon Sep 17 00:00:00 2001
From: Will McCutchen <will@mccutch.org>
Date: Fri, 14 Oct 2016 22:06:10 -0700
Subject: [PATCH] Constant json content type

---
 httpbin/handlers.go      |  4 ++--
 httpbin/handlers_test.go | 28 ++++++++++++++--------------
 httpbin/helpers.go       |  2 +-
 httpbin/httpbin.go       |  2 ++
 4 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/httpbin/handlers.go b/httpbin/handlers.go
index 5bfaf93..2d856e6 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 36919ce..3037a28 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 9ac8f42..ccc08cc 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 cac976f..cd1a1aa 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"`
 }
-- 
GitLab