net/http: adjust several tests to work for HTTP/3

Several tests currently fail for HTTP/3 due to assuming various things
that do not apply for HTTP/3. For example, assuming that Content-Length
header are automatically set for responses, or doing a raw TCP dial.

Adjust these tests to work for HTTP/3 too.

For #70914

Change-Id: Ia151496c984bd0e77c727464f872fa106a6a6964
Reviewed-on: https://go-review.googlesource.com/c/go/+/775461
Reviewed-by: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Nicholas Husin <husin@google.com>
This commit is contained in:
Nicholas S. Husin 2026-05-07 12:44:23 -04:00 committed by Nicholas Husin
parent 4d7ac7ff23
commit 70634e7d67
3 changed files with 18 additions and 18 deletions

View file

@ -709,27 +709,24 @@ func testServeIndexHtmlFS(t *testing.T, mode testMode) {
}
}
func TestFileServerZeroByte(t *testing.T) { run(t, testFileServerZeroByte, http3SkippedMode) }
func TestFileServerZeroByte(t *testing.T) { run(t, testFileServerZeroByte) }
func testFileServerZeroByte(t *testing.T, mode testMode) {
ts := newClientServerTest(t, mode, FileServer(Dir("."))).ts
cst := newClientServerTest(t, mode, FileServer(Dir(".")))
c, err := net.Dial("tcp", ts.Listener.Addr().String())
req, err := NewRequest("GET", cst.ts.URL, nil)
if err != nil {
t.Fatal(err)
}
defer c.Close()
_, err = fmt.Fprintf(c, "GET /..\x00 HTTP/1.0\r\n\r\n")
req.URL.Path = "/..\x00"
res, err := cst.c.Do(req)
if err != nil {
t.Fatal(err)
}
var got bytes.Buffer
bufr := bufio.NewReader(io.TeeReader(c, &got))
res, err := ReadResponse(bufr, nil)
if err != nil {
t.Fatal("ReadResponse: ", err)
}
defer res.Body.Close()
if res.StatusCode == 200 {
t.Errorf("got status 200; want an error. Body is:\n%s", got.Bytes())
t.Errorf("got status 200; want an error")
}
}

View file

@ -1551,7 +1551,7 @@ func testServerAllowsBlockingRemoteAddr(t *testing.T, mode testMode) {
// TestHeadResponses verifies that all MIME type sniffing and Content-Length
// counting of GET requests also happens on HEAD requests.
func TestHeadResponses(t *testing.T) { run(t, testHeadResponses, http3SkippedMode) }
func TestHeadResponses(t *testing.T) { run(t, testHeadResponses) }
func testHeadResponses(t *testing.T, mode testMode) {
cst := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {
_, err := w.Write([]byte("<html>"))
@ -1575,7 +1575,8 @@ func testHeadResponses(t *testing.T, mode testMode) {
if ct := res.Header.Get("Content-Type"); ct != "text/html; charset=utf-8" {
t.Errorf("Content-Type: %q; want text/html; charset=utf-8", ct)
}
if v := res.ContentLength; v != 10 {
// HTTP/3 does not automatically set ContentLength. This is intentional.
if v := res.ContentLength; v != 10 && mode != http3Mode {
t.Errorf("Content-Length: %d; want 10", v)
}
body, err := io.ReadAll(res.Body)
@ -7385,7 +7386,7 @@ func testHeadBody(t *testing.T, mode testMode, chunked bool, method string) {
// TestDisableContentLength verifies that the Content-Length is set by default
// or disabled when the header is set to nil.
func TestDisableContentLength(t *testing.T) { run(t, testDisableContentLength, http3SkippedMode) }
func TestDisableContentLength(t *testing.T) { run(t, testDisableContentLength) }
func testDisableContentLength(t *testing.T, mode testMode) {
noCL := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {
w.Header()["Content-Length"] = nil // disable the default Content-Length response
@ -7411,7 +7412,8 @@ func testDisableContentLength(t *testing.T, mode testMode) {
if err != nil {
t.Fatal(err)
}
if got := res.Header.Get("Content-Length"); got != "2" {
// HTTP/3 does not automatically set ContentLength. This is intentional.
if got := res.Header.Get("Content-Length"); got != "2" && mode != http3Mode {
t.Errorf("Content-Length: %q; want 2", got)
}
if err := res.Body.Close(); err != nil {

View file

@ -166,7 +166,7 @@ func (b *byteAtATimeReader) Read(p []byte) (n int, err error) {
return 1, nil
}
func TestContentTypeWithVariousSources(t *testing.T) { run(t, testContentTypeWithVariousSources, http3SkippedMode) }
func TestContentTypeWithVariousSources(t *testing.T) { run(t, testContentTypeWithVariousSources) }
func testContentTypeWithVariousSources(t *testing.T, mode testMode) {
const (
input = "\n<html>\n\t<head>\n"
@ -238,7 +238,8 @@ func testContentTypeWithVariousSources(t *testing.T, mode testMode) {
if ct := resp.Header.Get("Content-Type"); ct != expected {
t.Errorf("Content-Type = %q, want %q", ct, expected)
}
if want, got := resp.Header.Get("Content-Length"), fmt.Sprint(len(input)); want != got {
// HTTP/3 does not populate Content-Length automatically.
if want, got := resp.Header.Get("Content-Length"), fmt.Sprint(len(input)); want != got && mode != http3Mode {
t.Errorf("Content-Length = %q, want %q", want, got)
}
data, err := io.ReadAll(resp.Body)