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:
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-Length", strconv.Itoa(len(encb)))
|
||||
w.WriteHeader(status)
|
||||
w.Write(encb)
|
||||
} else {
|
||||
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) {
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest("POST", "/", strings.NewReader(`{}`))
|
||||
|
|
Loading…
Reference in New Issue