Skip to content

Commit

Permalink
- fix superfluous response.WriteHeader (setting a few times header wh…
Browse files Browse the repository at this point in the history
…en JsonResponse)
  • Loading branch information
exelban committed Nov 20, 2020
1 parent d884f2f commit bdd57c0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
16 changes: 6 additions & 10 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (e HttpError) Error() string {
}

// RenderJSON sends data as json.
func RenderJSON(w http.ResponseWriter, data interface{}) {
func RenderJSON(w http.ResponseWriter, code int, data interface{}) {
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(true)
Expand All @@ -32,21 +32,19 @@ func RenderJSON(w http.ResponseWriter, data interface{}) {
}
}

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
_, _ = w.Write(buf.Bytes())
}

// JsonResponse - write a response with application/json Content-Type header.
func JsonResponse(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
RenderJSON(w, data)
RenderJSON(w, http.StatusOK, data)
}

// OKResponse - write a OK response with application/json Content-Type header.
func OkResponse(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
RenderJSON(w, struct {
RenderJSON(w, http.StatusOK, struct {
OK bool `json:"ok"`
}{
OK: true,
Expand All @@ -71,9 +69,7 @@ func ErrorResponse(w http.ResponseWriter, r *http.Request, code int, error error

log.Printf("[DEBUG] %s - %s - %d (%s) - %s - %s", r.Method, uri, code, http.StatusText(code), err, msg)

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
RenderJSON(w, err)
RenderJSON(w, code, err)
}

// NotFound - return a error page for not found
Expand Down
31 changes: 31 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import (
"testing"
)

type custom struct {
test string
}

func (c *custom) MarshalJSON() ([]byte, error) {
return nil, errors.New("test")
}

func TestJsonError(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/empty_error" {
Expand Down Expand Up @@ -71,6 +79,11 @@ func TestJsonResponse(t *testing.T) {
} else if r.URL.Path == "/string" {
JsonResponse(w, []byte("OK"))
return
} else if r.URL.Path == "/multiply-header-set" {
JsonResponse(w, &custom{
test: "test",
})
return
}
OkResponse(w)
}
Expand All @@ -80,8 +93,10 @@ func TestJsonResponse(t *testing.T) {
w := httptest.NewRecorder()
handler(w, req)
resp := w.Result()

require.Equal(t, http.StatusOK, resp.StatusCode)
require.Equal(t, "application/json; charset=utf-8", resp.Header.Get("Content-Type"))

body, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
require.Empty(t, body)
Expand All @@ -92,8 +107,10 @@ func TestJsonResponse(t *testing.T) {
w := httptest.NewRecorder()
handler(w, req)
resp := w.Result()

require.Equal(t, http.StatusOK, resp.StatusCode)
require.Equal(t, "application/json; charset=utf-8", resp.Header.Get("Content-Type"))

body, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)

Expand All @@ -102,6 +119,20 @@ func TestJsonResponse(t *testing.T) {

require.Equal(t, append(b, '\n'), body)
})

t.Run("multiply header set", func(t *testing.T) {
req := httptest.NewRequest("GET", "http://test/multiply-header-set", nil)
w := httptest.NewRecorder()
handler(w, req)
resp := w.Result()

require.Equal(t, http.StatusInternalServerError, resp.StatusCode)
require.Equal(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))

body, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, "json: error calling MarshalJSON for type *rest.custom: test\n", string(body))
})
}

func TestNotFound(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Server struct {
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
w.Write([]byte("."))
_, _ = w.Write([]byte("."))
}

// Run - will initialize server and run it on provided port.
Expand Down

0 comments on commit bdd57c0

Please sign in to comment.