-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathtls_mux_test.go
57 lines (48 loc) · 1.68 KB
/
tls_mux_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
package grpc_proxy
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"github.com/bradleyjkemp/grpc-tools/testutils"
"golang.org/x/net/http2"
"github.com/bradleyjkemp/grpc-tools/internal/tlsmux"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
// TestTLSMux_HTTP2Support verifies that HTTP/2 is supported.
func TestTLSMux_HTTP2Support(t *testing.T) {
// New logger.
logger := logrus.New()
// New proxy listener.
ln, err := net.Listen("tcp", "localhost:0")
listenerPort := ln.Addr().(*net.TCPAddr).Port
require.NoErrorf(t, err, "failed creating tcp listener on port %d", listenerPort)
proxyLis := newProxyListener(logger, ln)
// New keypair.
tlsCert, err := testutils.NewSelfSignedKeyPair()
require.NoError(t, err, "failed loading X509 keypair")
x509Cert, err := x509.ParseCertificate(tlsCert.Certificate[0])
require.NoError(t, err, "failed parsing certificate out of keypair")
// Get TLS listener.
_, httpsLis := tlsmux.New(logger, proxyLis, x509Cert, tlsCert, ioutil.Discard)
// Start mock server with TLS listener.
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
server.Listener = httpsLis
server.Start()
// Request the server with HTTP2 and make sure the connection passes.
client := &http.Client{
Transport: &http2.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
req, err := http.NewRequest("POST", "https://localhost:"+strconv.Itoa(listenerPort), strings.NewReader(""))
require.NoError(t, err, "failed creating request object")
_, err = client.Do(req)
require.NoError(t, err, "failed requesting")
}