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(`{}`))