File tree 3 files changed +69
-1
lines changed 3 files changed +69
-1
lines changed Original file line number Diff line number Diff line change @@ -55,6 +55,28 @@ func ExampleServer() {
55
55
// Output: Hello, client
56
56
}
57
57
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
+
58
80
func ExampleNewTLSServer () {
59
81
ts := httptest .NewTLSServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
60
82
fmt .Fprintln (w , "Hello, client" )
Original file line number Diff line number Diff line change @@ -27,6 +27,11 @@ type Server struct {
27
27
URL string // base URL of form http://ipaddr:port with no trailing slash
28
28
Listener net.Listener
29
29
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
+
30
35
// TLS is the optional TLS configuration, populated with a new config
31
36
// after TLS is started. If set on an unstarted server before StartTLS
32
37
// is called, existing fields are copied into the new config.
@@ -151,7 +156,11 @@ func (s *Server) StartTLS() {
151
156
s .TLS = new (tls.Config )
152
157
}
153
158
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
155
164
}
156
165
if len (s .TLS .Certificates ) == 0 {
157
166
s .TLS .Certificates = []tls.Certificate {cert }
@@ -166,6 +175,7 @@ func (s *Server) StartTLS() {
166
175
TLSClientConfig : & tls.Config {
167
176
RootCAs : certpool ,
168
177
},
178
+ ForceAttemptHTTP2 : s .EnableHTTP2 ,
169
179
}
170
180
s .Listener = tls .NewListener (s .Listener , s .TLS )
171
181
s .URL = "https://" + s .Listener .Addr ().String ()
Original file line number Diff line number Diff line change @@ -202,3 +202,39 @@ func TestServerZeroValueClose(t *testing.T) {
202
202
203
203
ts .Close () // tests that it doesn't panic
204
204
}
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 \t got: %q\n \t want: %q" , g , w )
237
+ }
238
+ })
239
+ }
240
+ }
You can’t perform that action at this time.
0 commit comments