Skip to content

Commit 5375c71

Browse files
committed
net/http/httptest: add EnableHTTP2 to Server
Adds a knob EnableHTTP2, that enables an unstarted Server and its respective client to speak HTTP/2, but only after StartTLS has been invoked. Fixes #34939 Change-Id: I287c568b8708a4d3c03e7d9eca7c323b8f4c65b6 Reviewed-on: https://go-review.googlesource.com/c/go/+/201557 Run-TryBot: Emmanuel Odeke <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent dc3b788 commit 5375c71

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

src/net/http/httptest/example_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ func ExampleServer() {
5555
// Output: Hello, client
5656
}
5757

58+
func ExampleServer_hTTP2() {
59+
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
60+
fmt.Fprintf(w, "Hello, %s", r.Proto)
61+
}))
62+
ts.EnableHTTP2 = true
63+
ts.StartTLS()
64+
defer ts.Close()
65+
66+
res, err := ts.Client().Get(ts.URL)
67+
if err != nil {
68+
log.Fatal(err)
69+
}
70+
greeting, err := ioutil.ReadAll(res.Body)
71+
res.Body.Close()
72+
if err != nil {
73+
log.Fatal(err)
74+
}
75+
fmt.Printf("%s", greeting)
76+
77+
// Output: Hello, HTTP/2.0
78+
}
79+
5880
func ExampleNewTLSServer() {
5981
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
6082
fmt.Fprintln(w, "Hello, client")

src/net/http/httptest/server.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ type Server struct {
2727
URL string // base URL of form http://ipaddr:port with no trailing slash
2828
Listener net.Listener
2929

30+
// EnableHTTP2 controls whether HTTP/2 is enabled
31+
// on the server. It must be set between calling
32+
// NewUnstartedServer and calling Server.StartTLS.
33+
EnableHTTP2 bool
34+
3035
// TLS is the optional TLS configuration, populated with a new config
3136
// after TLS is started. If set on an unstarted server before StartTLS
3237
// is called, existing fields are copied into the new config.
@@ -151,7 +156,11 @@ func (s *Server) StartTLS() {
151156
s.TLS = new(tls.Config)
152157
}
153158
if s.TLS.NextProtos == nil {
154-
s.TLS.NextProtos = []string{"http/1.1"}
159+
nextProtos := []string{"http/1.1"}
160+
if s.EnableHTTP2 {
161+
nextProtos = []string{"h2"}
162+
}
163+
s.TLS.NextProtos = nextProtos
155164
}
156165
if len(s.TLS.Certificates) == 0 {
157166
s.TLS.Certificates = []tls.Certificate{cert}
@@ -166,6 +175,7 @@ func (s *Server) StartTLS() {
166175
TLSClientConfig: &tls.Config{
167176
RootCAs: certpool,
168177
},
178+
ForceAttemptHTTP2: s.EnableHTTP2,
169179
}
170180
s.Listener = tls.NewListener(s.Listener, s.TLS)
171181
s.URL = "https://" + s.Listener.Addr().String()

src/net/http/httptest/server_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,39 @@ func TestServerZeroValueClose(t *testing.T) {
202202

203203
ts.Close() // tests that it doesn't panic
204204
}
205+
206+
func TestTLSServerWithHTTP2(t *testing.T) {
207+
modes := []struct {
208+
name string
209+
wantProto string
210+
}{
211+
{"http1", "HTTP/1.1"},
212+
{"http2", "HTTP/2.0"},
213+
}
214+
215+
for _, tt := range modes {
216+
t.Run(tt.name, func(t *testing.T) {
217+
cst := NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
218+
w.Header().Set("X-Proto", r.Proto)
219+
}))
220+
221+
switch tt.name {
222+
case "http2":
223+
cst.EnableHTTP2 = true
224+
cst.StartTLS()
225+
default:
226+
cst.Start()
227+
}
228+
229+
defer cst.Close()
230+
231+
res, err := cst.Client().Get(cst.URL)
232+
if err != nil {
233+
t.Fatalf("Failed to make request: %v", err)
234+
}
235+
if g, w := res.Header.Get("X-Proto"), tt.wantProto; g != w {
236+
t.Fatalf("X-Proto header mismatch:\n\tgot: %q\n\twant: %q", g, w)
237+
}
238+
})
239+
}
240+
}

0 commit comments

Comments
 (0)