diff --git a/httpbin/handlers.go b/httpbin/handlers.go index 17f0cdfe17d50401e89ad8791f01e3da9ffe5662..6dba5ee80dc18b6e416081d1b5ebe3288af10b00 100644 --- a/httpbin/handlers.go +++ b/httpbin/handlers.go @@ -26,18 +26,17 @@ func (h *HTTPBin) Index(w http.ResponseWriter, r *http.Request) { http.Error(w, "Not Found", http.StatusNotFound) return } - w.Write(MustAsset("index.html")) + writeHTML(w, MustAsset("index.html"), http.StatusOK) } // FormsPost renders an HTML form that submits a request to the /post endpoint func (h *HTTPBin) FormsPost(w http.ResponseWriter, r *http.Request) { - w.Write(MustAsset("forms-post.html")) + writeHTML(w, MustAsset("forms-post.html"), http.StatusOK) } // UTF8 renders an HTML encoding stress test func (h *HTTPBin) UTF8(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/html; charset=utf-8") - w.Write(MustAsset("utf8.html")) + writeHTML(w, MustAsset("utf8.html"), http.StatusOK) } // Get handles HTTP GET requests diff --git a/httpbin/handlers_test.go b/httpbin/handlers_test.go index f3cbef82b074ddf8e3b3a0bc5ead71c360266993..8744a0a47dd95b8ada8befbd491bddd968acba33 100644 --- a/httpbin/handlers_test.go +++ b/httpbin/handlers_test.go @@ -60,7 +60,7 @@ func TestIndex(t *testing.T) { w := httptest.NewRecorder() handler.ServeHTTP(w, r) - assertContentType(t, w, "text/html; charset=utf-8") + assertContentType(t, w, htmlContentType) assertBodyContains(t, w, "go-httpbin") } @@ -76,7 +76,7 @@ func TestFormsPost(t *testing.T) { w := httptest.NewRecorder() handler.ServeHTTP(w, r) - assertContentType(t, w, "text/html; charset=utf-8") + assertContentType(t, w, htmlContentType) assertBodyContains(t, w, `<form method="post" action="/post">`) } @@ -85,7 +85,7 @@ func TestUTF8(t *testing.T) { w := httptest.NewRecorder() handler.ServeHTTP(w, r) - assertContentType(t, w, "text/html; charset=utf-8") + assertContentType(t, w, htmlContentType) assertBodyContains(t, w, `Hello world, Καλημέρα κόσμε, コンニチハ`) } diff --git a/httpbin/helpers.go b/httpbin/helpers.go index b857c8591fe3a3ef5ce00a31324859a4dc9b1ef6..e5f9e6ad4cf71f7b12ecd41a06ff5989ce358820 100644 --- a/httpbin/helpers.go +++ b/httpbin/helpers.go @@ -47,13 +47,21 @@ func getURL(r *http.Request) *url.URL { } } -func writeJSON(w http.ResponseWriter, body []byte, status int) { - w.Header().Set("Content-Type", jsonContentType) +func writeResponse(w http.ResponseWriter, status int, contentType string, body []byte) { + w.Header().Set("Content-Type", contentType) w.Header().Set("Content-Length", fmt.Sprintf("%d", len(body))) w.WriteHeader(status) w.Write(body) } +func writeJSON(w http.ResponseWriter, body []byte, status int) { + writeResponse(w, status, jsonContentType, body) +} + +func writeHTML(w http.ResponseWriter, body []byte, status int) { + writeResponse(w, status, htmlContentType, body) +} + // parseBody handles parsing a request body into our standard API response, // taking care to only consume the request body once based on the Content-Type // of the request. The given Resp will be updated. diff --git a/httpbin/httpbin.go b/httpbin/httpbin.go index 41ff3dac51d4d83bac560f5b9b767a9d751df746..ebd84dd92cb2ae3afc722cdee19102cddc7a1058 100644 --- a/httpbin/httpbin.go +++ b/httpbin/httpbin.go @@ -6,6 +6,7 @@ import ( ) const jsonContentType = "application/json; encoding=utf-8" +const htmlContentType = "text/html; charset=utf-8" type headersResponse struct { Headers http.Header `json:"headers"`