aboutsummaryrefslogtreecommitdiff
path: root/src/net/http/clientserver_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/net/http/clientserver_test.go')
-rw-r--r--src/net/http/clientserver_test.go65
1 files changed, 57 insertions, 8 deletions
diff --git a/src/net/http/clientserver_test.go b/src/net/http/clientserver_test.go
index 0873038757..32d97ea9f0 100644
--- a/src/net/http/clientserver_test.go
+++ b/src/net/http/clientserver_test.go
@@ -40,9 +40,10 @@ import (
type testMode string
const (
- http1Mode = testMode("h1") // HTTP/1.1
- https1Mode = testMode("https1") // HTTPS/1.1
- http2Mode = testMode("h2") // HTTP/2
+ http1Mode = testMode("h1") // HTTP/1.1
+ https1Mode = testMode("https1") // HTTPS/1.1
+ http2Mode = testMode("h2") // HTTP/2
+ http2UnencryptedMode = testMode("h2unencrypted") // HTTP/2
)
type testNotParallelOpt struct{}
@@ -132,6 +133,7 @@ type clientServerTest struct {
ts *httptest.Server
tr *Transport
c *Client
+ li *fakeNetListener
}
func (t *clientServerTest) close() {
@@ -169,6 +171,8 @@ func optWithServerLog(lg *log.Logger) func(*httptest.Server) {
}
}
+var optFakeNet = new(struct{})
+
// newClientServerTest creates and starts an httptest.Server.
//
// The mode parameter selects the implementation to test:
@@ -180,6 +184,9 @@ func optWithServerLog(lg *log.Logger) func(*httptest.Server) {
//
// func(*httptest.Server) // run before starting the server
// func(*http.Transport)
+//
+// The optFakeNet option configures the server and client to use a fake network implementation,
+// suitable for use in testing/synctest tests.
func newClientServerTest(t testing.TB, mode testMode, h Handler, opts ...any) *clientServerTest {
if mode == http2Mode {
CondSkipHTTP2(t)
@@ -189,9 +196,31 @@ func newClientServerTest(t testing.TB, mode testMode, h Handler, opts ...any) *c
h2: mode == http2Mode,
h: h,
}
- cst.ts = httptest.NewUnstartedServer(h)
var transportFuncs []func(*Transport)
+
+ if idx := slices.Index(opts, any(optFakeNet)); idx >= 0 {
+ opts = slices.Delete(opts, idx, idx+1)
+ cst.li = fakeNetListen()
+ cst.ts = &httptest.Server{
+ Config: &Server{Handler: h},
+ Listener: cst.li,
+ }
+ transportFuncs = append(transportFuncs, func(tr *Transport) {
+ tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
+ return cst.li.connect(), nil
+ }
+ })
+ } else {
+ cst.ts = httptest.NewUnstartedServer(h)
+ }
+
+ if mode == http2UnencryptedMode {
+ p := &Protocols{}
+ p.SetUnencryptedHTTP2(true)
+ cst.ts.Config.Protocols = p
+ }
+
for _, opt := range opts {
switch opt := opt.(type) {
case func(*Transport):
@@ -212,6 +241,9 @@ func newClientServerTest(t testing.TB, mode testMode, h Handler, opts ...any) *c
cst.ts.Start()
case https1Mode:
cst.ts.StartTLS()
+ case http2UnencryptedMode:
+ ExportHttp2ConfigureServer(cst.ts.Config, nil)
+ cst.ts.Start()
case http2Mode:
ExportHttp2ConfigureServer(cst.ts.Config, nil)
cst.ts.TLS = cst.ts.Config.TLSConfig
@@ -221,7 +253,7 @@ func newClientServerTest(t testing.TB, mode testMode, h Handler, opts ...any) *c
}
cst.c = cst.ts.Client()
cst.tr = cst.c.Transport.(*Transport)
- if mode == http2Mode {
+ if mode == http2Mode || mode == http2UnencryptedMode {
if err := ExportHttp2ConfigureTransport(cst.tr); err != nil {
t.Fatal(err)
}
@@ -229,6 +261,13 @@ func newClientServerTest(t testing.TB, mode testMode, h Handler, opts ...any) *c
for _, f := range transportFuncs {
f(cst.tr)
}
+
+ if mode == http2UnencryptedMode {
+ p := &Protocols{}
+ p.SetUnencryptedHTTP2(true)
+ cst.tr.Protocols = p
+ }
+
t.Cleanup(func() {
cst.close()
})
@@ -246,9 +285,19 @@ func (w testLogWriter) Write(b []byte) (int, error) {
// Testing the newClientServerTest helper itself.
func TestNewClientServerTest(t *testing.T) {
- run(t, testNewClientServerTest, []testMode{http1Mode, https1Mode, http2Mode})
+ modes := []testMode{http1Mode, https1Mode, http2Mode}
+ t.Run("realnet", func(t *testing.T) {
+ run(t, func(t *testing.T, mode testMode) {
+ testNewClientServerTest(t, mode)
+ }, modes)
+ })
+ t.Run("synctest", func(t *testing.T) {
+ runSynctest(t, func(t testing.TB, mode testMode) {
+ testNewClientServerTest(t, mode, optFakeNet)
+ }, modes)
+ })
}
-func testNewClientServerTest(t *testing.T, mode testMode) {
+func testNewClientServerTest(t testing.TB, mode testMode, opts ...any) {
var got struct {
sync.Mutex
proto string
@@ -260,7 +309,7 @@ func testNewClientServerTest(t *testing.T, mode testMode) {
got.proto = r.Proto
got.hasTLS = r.TLS != nil
})
- cst := newClientServerTest(t, mode, h)
+ cst := newClientServerTest(t, mode, h, opts...)
if _, err := cst.c.Head(cst.ts.URL); err != nil {
t.Fatal(err)
}