Skip to content
Snippets Groups Projects
Commit c0546840 authored by Will McCutchen's avatar Will McCutchen
Browse files

Constant json content type

parent c4135f95
No related branches found
No related tags found
No related merge requests found
...@@ -138,7 +138,7 @@ func (h *HTTPBin) Status(w http.ResponseWriter, r *http.Request) { ...@@ -138,7 +138,7 @@ func (h *HTTPBin) Status(w http.ResponseWriter, r *http.Request) {
406: &statusCase{ 406: &statusCase{
body: notAcceptableBody, body: notAcceptableBody,
headers: map[string]string{ headers: map[string]string{
"Content-Type": "application/json; encoding=utf-8", "Content-Type": jsonContentType,
}, },
}, },
407: &statusCase{ 407: &statusCase{
...@@ -179,7 +179,7 @@ func (h *HTTPBin) ResponseHeaders(w http.ResponseWriter, r *http.Request) { ...@@ -179,7 +179,7 @@ func (h *HTTPBin) ResponseHeaders(w http.ResponseWriter, r *http.Request) {
} }
body, _ := json.Marshal(args) body, _ := json.Marshal(args)
if contentType := w.Header().Get("Content-Type"); contentType == "" { 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) w.Write(body)
} }
......
...@@ -92,7 +92,7 @@ func TestGet__Basic(t *testing.T) { ...@@ -92,7 +92,7 @@ func TestGet__Basic(t *testing.T) {
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK) assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8") assertContentType(t, w, jsonContentType)
var resp *bodyResponse var resp *bodyResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
...@@ -135,7 +135,7 @@ func TestGet__WithParams(t *testing.T) { ...@@ -135,7 +135,7 @@ func TestGet__WithParams(t *testing.T) {
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK) assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8") assertContentType(t, w, jsonContentType)
var resp *bodyResponse var resp *bodyResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
...@@ -246,7 +246,7 @@ func TestIP(t *testing.T) { ...@@ -246,7 +246,7 @@ func TestIP(t *testing.T) {
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK) assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8") assertContentType(t, w, jsonContentType)
var resp *ipResponse var resp *ipResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
...@@ -266,7 +266,7 @@ func TestUserAgent(t *testing.T) { ...@@ -266,7 +266,7 @@ func TestUserAgent(t *testing.T) {
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK) assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8") assertContentType(t, w, jsonContentType)
var resp *userAgentResponse var resp *userAgentResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
...@@ -289,7 +289,7 @@ func TestHeaders(t *testing.T) { ...@@ -289,7 +289,7 @@ func TestHeaders(t *testing.T) {
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK) assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8") assertContentType(t, w, jsonContentType)
var resp *headersResponse var resp *headersResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
...@@ -314,7 +314,7 @@ func TestPost__EmptyBody(t *testing.T) { ...@@ -314,7 +314,7 @@ func TestPost__EmptyBody(t *testing.T) {
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK) assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8") assertContentType(t, w, jsonContentType)
var resp *bodyResponse var resp *bodyResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
...@@ -342,7 +342,7 @@ func TestPost__FormEncodedBody(t *testing.T) { ...@@ -342,7 +342,7 @@ func TestPost__FormEncodedBody(t *testing.T) {
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK) assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8") assertContentType(t, w, jsonContentType)
var resp *bodyResponse var resp *bodyResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
...@@ -378,7 +378,7 @@ func TestPost__FormEncodedBodyNoContentType(t *testing.T) { ...@@ -378,7 +378,7 @@ func TestPost__FormEncodedBodyNoContentType(t *testing.T) {
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK) assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8") assertContentType(t, w, jsonContentType)
var resp *bodyResponse var resp *bodyResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
...@@ -426,7 +426,7 @@ func TestPost__MultiPartBody(t *testing.T) { ...@@ -426,7 +426,7 @@ func TestPost__MultiPartBody(t *testing.T) {
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK) assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8") assertContentType(t, w, jsonContentType)
var resp *bodyResponse var resp *bodyResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
...@@ -488,7 +488,7 @@ func TestPost__JSON(t *testing.T) { ...@@ -488,7 +488,7 @@ func TestPost__JSON(t *testing.T) {
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK) assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8") assertContentType(t, w, jsonContentType)
var resp *bodyResponse var resp *bodyResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
...@@ -536,7 +536,7 @@ func TestPost__BodyTooBig(t *testing.T) { ...@@ -536,7 +536,7 @@ func TestPost__BodyTooBig(t *testing.T) {
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusBadRequest) assertStatusCode(t, w, http.StatusBadRequest)
assertContentType(t, w, "application/json; encoding=utf-8") assertContentType(t, w, jsonContentType)
} }
func TestPost__QueryParams(t *testing.T) { func TestPost__QueryParams(t *testing.T) {
...@@ -550,7 +550,7 @@ func TestPost__QueryParams(t *testing.T) { ...@@ -550,7 +550,7 @@ func TestPost__QueryParams(t *testing.T) {
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK) assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8") assertContentType(t, w, jsonContentType)
var resp *bodyResponse var resp *bodyResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
...@@ -587,7 +587,7 @@ func TestPost__QueryParamsAndBody(t *testing.T) { ...@@ -587,7 +587,7 @@ func TestPost__QueryParamsAndBody(t *testing.T) {
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK) assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8") assertContentType(t, w, jsonContentType)
var resp *bodyResponse var resp *bodyResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
...@@ -692,7 +692,7 @@ func TestResponseHeaders__OK(t *testing.T) { ...@@ -692,7 +692,7 @@ func TestResponseHeaders__OK(t *testing.T) {
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK) assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8") assertContentType(t, w, jsonContentType)
for k, expectedValues := range headers { for k, expectedValues := range headers {
values, ok := w.HeaderMap[k] values, ok := w.HeaderMap[k]
......
...@@ -47,7 +47,7 @@ func getURL(r *http.Request) *url.URL { ...@@ -47,7 +47,7 @@ func getURL(r *http.Request) *url.URL {
} }
func writeJSON(w http.ResponseWriter, body []byte, status int) { 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.WriteHeader(status)
w.Write(body) w.Write(body)
} }
......
...@@ -5,6 +5,8 @@ import ( ...@@ -5,6 +5,8 @@ import (
"net/url" "net/url"
) )
const jsonContentType = "application/json; encoding=utf-8"
type headersResponse struct { type headersResponse struct {
Headers http.Header `json:"headers"` Headers http.Header `json:"headers"`
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment