2016-06-07 08:08:50 +00:00
|
|
|
// +build go1.6
|
|
|
|
|
|
|
|
package graceful
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2017-05-04 17:28:44 +00:00
|
|
|
"fmt"
|
2016-06-07 08:08:50 +00:00
|
|
|
"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)
|
|
|
|
})
|
|
|
|
|
2017-05-04 17:28:44 +00:00
|
|
|
server := &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: mux}
|
2016-06-07 08:08:50 +00:00
|
|
|
|
|
|
|
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}
|
2017-05-04 17:28:44 +00:00
|
|
|
r, err := client.Get(fmt.Sprintf("https://localhost:%d", port))
|
2016-06-07 08:08:50 +00:00
|
|
|
|
|
|
|
c <- os.Interrupt
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error encountered while connecting to test server: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-05-04 17:28:44 +00:00
|
|
|
if !r.ProtoAtLeast(2, 0) {
|
2016-06-07 08:08:50 +00:00
|
|
|
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
|
2017-05-04 17:28:44 +00:00
|
|
|
srv = &Server{Timeout: 0, TCPKeepAlive: 1 * time.Minute, Server: server, interrupt: c}
|
2016-06-07 08:08:50 +00:00
|
|
|
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() {
|
2017-05-04 17:28:44 +00:00
|
|
|
srv := &Server{Timeout: killTime, TCPKeepAlive: 1 * time.Minute, Server: server2, interrupt: c}
|
2016-06-07 08:08:50 +00:00
|
|
|
|
|
|
|
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},
|
2017-05-04 17:28:44 +00:00
|
|
|
NextProtos: []string{"h2"}, // We need to explicitly enable http/2 in Go 1.7+
|
2016-06-07 08:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|