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

Change-Id: Ia7add6cf7e8b46bb6dd45bd3c0371ea79402fb45
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2022-02-09 12:06:27 -08:00 committed by Brad Fitzpatrick
parent 62db629227
commit 24c9dbd129
2 changed files with 18 additions and 0 deletions

View File

@ -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)))

View File

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