http2: in TestContentEncodingNoSniffing, do not allow the Transport to outlive the serverTester

This test constructed a new serverTester for each subtest, but reused
a Transport across all of the tests. I don't fully understand why, but
for some reason that occasionally led to "connection reset by peer"
failures when the port was reused.

Scoping the Transport to the lifetime of the serverTester seems to
resolve the errors: I tested with

	go test ./http2 -run=TestContentEncodingNoSniffing -httptest.serve=127.0.0.1:8080 -count=10000

and got lots of failures prior to this change and none after.

Fixes golang/go#46762
(I hope.)

Change-Id: I385c077c1d8627e42ce4d2db041878aacb5452fb
Reviewed-on: https://go-review.googlesource.com/c/net/+/380154
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/http2/server_test.go b/http2/server_test.go
index ae45f31..50cdcbd 100644
--- a/http2/server_test.go
+++ b/http2/server_test.go
@@ -4212,9 +4212,6 @@
 		},
 	}
 
-	tr := &Transport{TLSClientConfig: tlsConfigInsecure}
-	defer tr.CloseIdleConnections()
-
 	for _, tt := range resps {
 		t.Run(tt.name, func(t *testing.T) {
 			st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
@@ -4225,22 +4222,32 @@
 			}, optOnlyServer)
 			defer st.Close()
 
+			tr := &Transport{TLSClientConfig: tlsConfigInsecure}
+			defer tr.CloseIdleConnections()
+
 			req, _ := http.NewRequest("GET", st.ts.URL, nil)
 			res, err := tr.RoundTrip(req)
 			if err != nil {
-				t.Fatalf("Failed to fetch URL: %v", err)
+				t.Fatalf("GET %s: %v", st.ts.URL, err)
 			}
 			defer res.Body.Close()
-			if g, w := res.Header.Get("Content-Encoding"), tt.contentEncoding; g != w {
+
+			g := res.Header.Get("Content-Encoding")
+			t.Logf("%s: Content-Encoding: %s", st.ts.URL, g)
+
+			if w := tt.contentEncoding; g != w {
 				if w != nil { // The case where contentEncoding was set explicitly.
 					t.Errorf("Content-Encoding mismatch\n\tgot:  %q\n\twant: %q", g, w)
 				} else if g != "" { // "" should be the equivalent when the contentEncoding is unset.
 					t.Errorf("Unexpected Content-Encoding %q", g)
 				}
 			}
-			if g, w := res.Header.Get("Content-Type"), tt.wantContentType; g != w {
+
+			g = res.Header.Get("Content-Type")
+			if w := tt.wantContentType; g != w {
 				t.Errorf("Content-Type mismatch\n\tgot:  %q\n\twant: %q", g, w)
 			}
+			t.Logf("%s: Content-Type: %s", st.ts.URL, g)
 		})
 	}
 }