diff --git a/transport/http/server.go b/transport/http/server.go index e909e6b07..4ae519bf0 100644 --- a/transport/http/server.go +++ b/transport/http/server.go @@ -151,6 +151,9 @@ func EncodeJSONResponse(_ context.Context, w http.ResponseWriter, response inter code = sc.StatusCode() } w.WriteHeader(code) + if code == http.StatusNoContent { + return nil + } return json.NewEncoder(w).Encode(response) } diff --git a/transport/http/server_test.go b/transport/http/server_test.go index 8ea025f65..faf20c6f6 100644 --- a/transport/http/server_test.go +++ b/transport/http/server_test.go @@ -157,6 +157,34 @@ func TestEncodeJSONResponse(t *testing.T) { } } +type noContentResponse struct{} + +func (e noContentResponse) StatusCode() int { return http.StatusNoContent } + +func TestEncodeNoContent(t *testing.T) { + handler := httptransport.NewServer( + context.Background(), + func(context.Context, interface{}) (interface{}, error) { return noContentResponse{}, nil }, + func(context.Context, *http.Request) (interface{}, error) { return struct{}{}, nil }, + httptransport.EncodeJSONResponse, + ) + + server := httptest.NewServer(handler) + defer server.Close() + + resp, err := http.Get(server.URL) + if err != nil { + t.Fatal(err) + } + if want, have := http.StatusNoContent, resp.StatusCode; want != have { + t.Errorf("StatusCode: want %d, have %d", want, have) + } + buf, _ := ioutil.ReadAll(resp.Body) + if want, have := 0, len(buf); want != have { + t.Errorf("Body: want no content, have %d bytes", have) + } +} + type enhancedError struct{} func (e enhancedError) Error() string { return "enhanced error" }