aboutsummaryrefslogtreecommitdiff
path: root/src/net/http/serve_test.go
diff options
context:
space:
mode:
authorDamien Neil <dneil@google.com>2024-01-05 16:10:33 -0800
committerDamien Neil <dneil@google.com>2024-02-29 19:48:56 +0000
commit4e7bd20f8fdccdb2f0f30b051e3ea3fffb449367 (patch)
treece2ac02a231da90aed6007e0e209d64e9fa43b21 /src/net/http/serve_test.go
parentadc575e64c8a49c0a14a8a6b0480c5f9815bdb1a (diff)
downloadgo-4e7bd20f8fdccdb2f0f30b051e3ea3fffb449367.tar.xz
net/http: remove Content-Length header in http.Error
Error replies to a request with an error message and HTTP code. Delete any preexisting Content-Length header before writing the header; if a Content-Length is present, it's probably for content that the caller has given up on writing. For #50905 Change-Id: Ia3d4ca008be46fa5d41afadf29ca5cacb1c47660 Reviewed-on: https://go-review.googlesource.com/c/go/+/554216 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Jonathan Amsterdam <jba@google.com>
Diffstat (limited to 'src/net/http/serve_test.go')
-rw-r--r--src/net/http/serve_test.go21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go
index 69d105ec63..9df6ab426c 100644
--- a/src/net/http/serve_test.go
+++ b/src/net/http/serve_test.go
@@ -7052,3 +7052,24 @@ func testDisableContentLength(t *testing.T, mode testMode) {
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)
+ }
+}