Skip to content

net/http: discrepancy in contentLength given invalid "Content-Encoding":"foo" between http1 and http2 #13543

Closed
@odeke-em

Description

@odeke-em

Setting an invalid "Content-Encoding" as "foo" then querying for resp.ContentLength in http1 returns -1 while in http2 it returns the entire length of the content served.
The test's CL that found this is at https://go-review.googlesource.com/#/c/17598/2

diff --git a/src/net/http/fs_test.go b/src/net/http/fs_test.go
index 7550c55..1d52678 100644
--- a/src/net/http/fs_test.go
+++ b/src/net/http/fs_test.go
@@ -477,14 +477,22 @@ func TestServeFileFromCWD(t *testing.T) {
    }
 }

-func TestServeFileWithContentEncoding(t *testing.T) {
+func TestServeFileWithContentEncoding_h1(t *testing.T) {
+   testServeFileWithContentEncoding(t, false)
+}
+
+func TestServeFileWithContentEncoding_h2(t *testing.T) {
+   testServeFileWithContentEncoding(t, true)
+}
+
+func testServeFileWithContentEncoding(t *testing.T, h2 bool) {
    defer afterTest(t)
-   ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
+   cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
        w.Header().Set("Content-Encoding", "foo")
        ServeFile(w, r, "testdata/file")
    }))
-   defer ts.Close()
-   resp, err := Get(ts.URL)
+   defer cst.close()
+   resp, err := cst.c.Get(cst.ts.URL)
    if err != nil {
        t.Fatal(err)
    }

Giving

$ go test -cover
--- FAIL: TestServeFileWithContentEncoding_h2 (0.00s)
    fs_test.go:501: Content-Length mismatch: got 11, want -1
FAIL
coverage: 78.2% of statements
exit status 1
FAIL    net/http    22.996s

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions