t.Fatal(err)
}
}
+
+func TestErrorContentLength(t *testing.T) { run(t, testErrorContentLength) }
+func testErrorContentLength(t *testing.T, mode testMode) {
+ const errorBody = "an error occurred"
+ cst := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {
+ w.Header().Set("Content-Length", "1000")
+ Error(w, errorBody, 400)
+ }))
+ res, err := cst.c.Get(cst.ts.URL)
+ if err != nil {
+ t.Fatalf("Get(%q) = %v", cst.ts.URL, err)
+ }
+ defer res.Body.Close()
+ body, err := io.ReadAll(res.Body)
+ if err != nil {
+ t.Fatalf("io.ReadAll(res.Body) = %v", err)
+ }
+ if string(body) != errorBody+"\n" {
+ t.Fatalf("read body: %q, want %q", string(body), errorBody)
+ }
+}
// writes are done to w.
// The error message should be plain text.
func Error(w ResponseWriter, error string, code int) {
+ w.Header().Del("Content-Length")
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(code)