This repository has been archived by the owner on Sep 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 114
/
http2_test.go
125 lines (87 loc) · 2.58 KB
/
http2_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// +build go1.6
package graceful
import (
"crypto/tls"
"fmt"
"net/http"
"os"
"sync"
"testing"
"time"
"golang.org/x/net/http2"
)
func createServer() *http.Server {
mux := http.NewServeMux()
mux.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusOK)
})
server := &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: mux}
return server
}
func checkIfConnectionToServerIsHTTP2(t *testing.T, wg *sync.WaitGroup, c chan os.Signal) {
defer wg.Done()
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
err := http2.ConfigureTransport(tr)
if err != nil {
t.Fatal("Unable to upgrade client transport to HTTP/2")
}
client := http.Client{Transport: tr}
r, err := client.Get(fmt.Sprintf("https://localhost:%d", port))
c <- os.Interrupt
if err != nil {
t.Fatalf("Error encountered while connecting to test server: %s", err)
}
if !r.ProtoAtLeast(2, 0) {
t.Fatalf("Expected HTTP/2 connection to server, but connection was using %s", r.Proto)
}
}
func TestHTTP2ListenAndServeTLS(t *testing.T) {
c := make(chan os.Signal, 1)
var wg sync.WaitGroup
wg.Add(1)
server := createServer()
var srv *Server
go func() {
// set timeout of 0 to test idle connection closing
srv = &Server{Timeout: 0, TCPKeepAlive: 1 * time.Minute, Server: server, interrupt: c}
srv.ListenAndServeTLS("test-fixtures/cert.crt", "test-fixtures/key.pem")
wg.Done()
}()
time.Sleep(waitTime) // Wait for the server to start
wg.Add(1)
go checkIfConnectionToServerIsHTTP2(t, &wg, c)
wg.Wait()
c <- os.Interrupt // kill the server to close idle connections
// block on the stopChan until the server has shut down
select {
case <-srv.StopChan():
case <-time.After(killTime * 2):
t.Fatal("Timed out while waiting for explicit stop to complete")
}
}
func TestHTTP2ListenAndServeTLSConfig(t *testing.T) {
c := make(chan os.Signal, 1)
var wg sync.WaitGroup
wg.Add(1)
server2 := createServer()
go func() {
srv := &Server{Timeout: killTime, TCPKeepAlive: 1 * time.Minute, Server: server2, interrupt: c}
cert, err := tls.LoadX509KeyPair("test-fixtures/cert.crt", "test-fixtures/key.pem")
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
tlsConf := &tls.Config{
Certificates: []tls.Certificate{cert},
NextProtos: []string{"h2"}, // We need to explicitly enable http/2 in Go 1.7+
}
tlsConf.BuildNameToCertificate()
srv.ListenAndServeTLSConfig(tlsConf)
wg.Done()
}()
time.Sleep(waitTime) // Wait for the server to start
wg.Add(1)
go checkIfConnectionToServerIsHTTP2(t, &wg, c)
wg.Wait()
}