tsweb: fix JSONHandlerFunc regression where HTTP status was lost on gzip

Change-Id: Ia7add6cf7e8b46bb6dd45bd3c0371ea79402fb45
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/3861/merge
Brad Fitzpatrick 3 years ago committed by Brad Fitzpatrick
parent 62db629227
commit 24c9dbd129

@ -100,6 +100,7 @@ func (fn JSONHandlerFunc) ServeHTTPReturn(w http.ResponseWriter, r *http.Request
} }
w.Header().Set("Content-Encoding", "gzip") w.Header().Set("Content-Encoding", "gzip")
w.Header().Set("Content-Length", strconv.Itoa(len(encb))) w.Header().Set("Content-Length", strconv.Itoa(len(encb)))
w.WriteHeader(status)
w.Write(encb) w.Write(encb)
} else { } else {
w.Header().Set("Content-Length", strconv.Itoa(len(b))) w.Header().Set("Content-Length", strconv.Itoa(len(b)))

@ -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) { t.Run("400 post data error", func(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", strings.NewReader(`{}`)) r := httptest.NewRequest("POST", "/", strings.NewReader(`{}`))

Loading…
Cancel
Save