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

Fix content type for JSON responses

parent 93f147d6
No related branches found
No related tags found
No related merge requests found
...@@ -19,14 +19,31 @@ var app = NewHTTPBin(&Options{ ...@@ -19,14 +19,31 @@ var app = NewHTTPBin(&Options{
var handler = app.Handler() var handler = app.Handler()
func assertStatusCode(t *testing.T, w *httptest.ResponseRecorder, code int) {
if w.Code != code {
t.Fatalf("expected status code %d, got %d", code, w.Code)
}
}
func assertContentType(t *testing.T, w *httptest.ResponseRecorder, contentType string) {
if w.Header().Get("Content-Type") != contentType {
t.Fatalf("expected content type %s, got %s", contentType, w.Header().Get("Content-Type"))
}
}
func assertBodyContains(t *testing.T, w *httptest.ResponseRecorder, needle string) {
if !strings.Contains(w.Body.String(), needle) {
t.Fatalf("expected string %v in body", needle)
}
}
func TestIndex(t *testing.T) { func TestIndex(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil) r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if !strings.Contains(w.Body.String(), "go-httpbin") { assertContentType(t, w, "text/html; charset=utf-8")
t.Fatalf("expected go-httpbin in index body") assertBodyContains(t, w, "go-httpbin")
}
} }
func TestFormsPost(t *testing.T) { func TestFormsPost(t *testing.T) {
...@@ -34,9 +51,8 @@ func TestFormsPost(t *testing.T) { ...@@ -34,9 +51,8 @@ func TestFormsPost(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if !strings.Contains(w.Body.String(), `<form method="post" action="/post">`) { assertContentType(t, w, "text/html; charset=utf-8")
t.Fatalf("expected <form> in body") assertBodyContains(t, w, `<form method="post" action="/post">`)
}
} }
func TestUTF8(t *testing.T) { func TestUTF8(t *testing.T) {
...@@ -44,12 +60,8 @@ func TestUTF8(t *testing.T) { ...@@ -44,12 +60,8 @@ func TestUTF8(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Header().Get("Content-Type") != "text/html; charset=utf-8" { assertContentType(t, w, "text/html; charset=utf-8")
t.Fatalf("expected 'text/html; charset=utf-8' content type") assertBodyContains(t, w, `Hello world, Καλημέρα κόσμε, コンニチハ`)
}
if !strings.Contains(w.Body.String(), `Hello world, Καλημέρα κόσμε, コンニチハ`) {
t.Fatalf("expected utf8 text in body")
}
} }
func TestGet__Basic(t *testing.T) { func TestGet__Basic(t *testing.T) {
...@@ -59,9 +71,8 @@ func TestGet__Basic(t *testing.T) { ...@@ -59,9 +71,8 @@ func TestGet__Basic(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Code != 200 { assertStatusCode(t, w, http.StatusOK)
t.Fatalf("expected status code 200, got %d", w.Code) assertContentType(t, w, "application/json; encoding=utf-8")
}
var resp *bodyResponse var resp *bodyResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
...@@ -98,9 +109,8 @@ func TestGet__OnlyAllowsGets(t *testing.T) { ...@@ -98,9 +109,8 @@ func TestGet__OnlyAllowsGets(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Code != http.StatusMethodNotAllowed { assertStatusCode(t, w, http.StatusMethodNotAllowed)
t.Fatalf("expected HTTP 405, got %d", w.Code) assertContentType(t, w, "text/plain; charset=utf-8")
}
} }
func TestGet__CORSHeadersWithoutRequestOrigin(t *testing.T) { func TestGet__CORSHeadersWithoutRequestOrigin(t *testing.T) {
...@@ -199,6 +209,9 @@ func TestIP(t *testing.T) { ...@@ -199,6 +209,9 @@ func TestIP(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8")
var resp *ipResponse var resp *ipResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil { if err != nil {
...@@ -216,6 +229,9 @@ func TestUserAgent(t *testing.T) { ...@@ -216,6 +229,9 @@ func TestUserAgent(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8")
var resp *userAgentResponse var resp *userAgentResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil { if err != nil {
...@@ -236,6 +252,9 @@ func TestHeaders(t *testing.T) { ...@@ -236,6 +252,9 @@ func TestHeaders(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8")
var resp *headersResponse var resp *headersResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil { if err != nil {
...@@ -258,6 +277,9 @@ func TestPost__EmptyBody(t *testing.T) { ...@@ -258,6 +277,9 @@ func TestPost__EmptyBody(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8")
var resp *bodyResponse var resp *bodyResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil { if err != nil {
...@@ -283,6 +305,9 @@ func TestPost__FormEncodedBody(t *testing.T) { ...@@ -283,6 +305,9 @@ func TestPost__FormEncodedBody(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8")
var resp *bodyResponse var resp *bodyResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil { if err != nil {
...@@ -316,6 +341,9 @@ func TestPost__FormEncodedBodyNoContentType(t *testing.T) { ...@@ -316,6 +341,9 @@ func TestPost__FormEncodedBodyNoContentType(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8")
var resp *bodyResponse var resp *bodyResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil { if err != nil {
...@@ -353,6 +381,9 @@ func TestPost__JSON(t *testing.T) { ...@@ -353,6 +381,9 @@ func TestPost__JSON(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, "application/json; encoding=utf-8")
var resp *bodyResponse var resp *bodyResponse
err := json.Unmarshal(w.Body.Bytes(), &resp) err := json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil { if err != nil {
...@@ -390,7 +421,6 @@ func TestPost__BodyTooBig(t *testing.T) { ...@@ -390,7 +421,6 @@ func TestPost__BodyTooBig(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Code != http.StatusBadRequest { assertStatusCode(t, w, http.StatusBadRequest)
t.Fatalf("expected code %d, got %d", http.StatusBadRequest, w.Code) assertContentType(t, w, "application/json; encoding=utf-8")
}
} }
...@@ -48,8 +48,8 @@ func getURL(r *http.Request) string { ...@@ -48,8 +48,8 @@ func getURL(r *http.Request) string {
} }
func writeJSON(w http.ResponseWriter, body []byte, status int) { func writeJSON(w http.ResponseWriter, body []byte, status int) {
w.WriteHeader(status)
w.Header().Set("Content-Type", "application/json; encoding=utf-8") w.Header().Set("Content-Type", "application/json; encoding=utf-8")
w.WriteHeader(status)
w.Write(body) w.Write(body)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment