From 24c9dbd1299fcfcd2ed4cf54a0c59ac3d1cc5cd2 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 9 Feb 2022 12:06:27 -0800 Subject: [PATCH] tsweb: fix JSONHandlerFunc regression where HTTP status was lost on gzip Change-Id: Ia7add6cf7e8b46bb6dd45bd3c0371ea79402fb45 Signed-off-by: Brad Fitzpatrick --- tsweb/jsonhandler.go | 1 + tsweb/jsonhandler_test.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/tsweb/jsonhandler.go b/tsweb/jsonhandler.go index 89e96e89d..766570b7f 100644 --- a/tsweb/jsonhandler.go +++ b/tsweb/jsonhandler.go @@ -100,6 +100,7 @@ func (fn JSONHandlerFunc) ServeHTTPReturn(w http.ResponseWriter, r *http.Request } w.Header().Set("Content-Encoding", "gzip") w.Header().Set("Content-Length", strconv.Itoa(len(encb))) + w.WriteHeader(status) w.Write(encb) } else { w.Header().Set("Content-Length", strconv.Itoa(len(b))) diff --git a/tsweb/jsonhandler_test.go b/tsweb/jsonhandler_test.go index 8cf55c2ab..494c6494b 100644 --- a/tsweb/jsonhandler_test.go +++ b/tsweb/jsonhandler_test.go @@ -182,6 +182,23 @@ func TestNewJSONHandler(t *testing.T) { } }) + t.Run("gzipped_400", func(t *testing.T) { + w := httptest.NewRecorder() + r := httptest.NewRequest("POST", "/", strings.NewReader(`{"Price": 10}`)) + r.Header.Set("Accept-Encoding", "gzip") + value := []string{"foo", "foo", "foo"} + JSONHandlerFunc(func(r *http.Request) (int, interface{}, error) { + return 400, value, nil + }).ServeHTTPReturn(w, r) + res := w.Result() + if ct := res.Header.Get("Content-Encoding"); ct != "gzip" { + t.Fatalf("encoding = %q; want gzip", ct) + } + if res.StatusCode != 400 { + t.Errorf("Status = %v; want 400", res.StatusCode) + } + }) + t.Run("400 post data error", func(t *testing.T) { w := httptest.NewRecorder() r := httptest.NewRequest("POST", "/", strings.NewReader(`{}`))