forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h2c_test.go
68 lines (55 loc) · 1.53 KB
/
h2c_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
package h2c_test
import (
"crypto/tls"
"flag"
"net"
"net/http"
"os"
"testing"
"golang.org/x/net/http2"
"github.com/open-policy-agent/opa/test/e2e"
)
var testRuntime *e2e.TestRuntime
func TestMain(m *testing.M) {
flag.Parse()
testServerParams := e2e.NewAPIServerTestParams()
testServerParams.Addrs = &[]string{":0"}
testServerParams.DiagnosticAddrs = &[]string{":0"}
testServerParams.InsecureAddr = ":0"
testServerParams.H2CEnabled = true
var err error
testRuntime, err = e2e.NewTestRuntime(testServerParams)
if err != nil {
os.Exit(1)
}
os.Exit(testRuntime.RunTests(m))
}
func TestH2CHTTPListeners(t *testing.T) {
// h2c-enabled client
client := http.Client{
Transport: &http2.Transport{
AllowHTTP: true,
DialTLS: func(network, addr string, _ *tls.Config) (net.Conn, error) {
return net.Dial(network, addr)
},
},
}
addrs := append(testRuntime.Runtime.Addrs(), testRuntime.Runtime.DiagnosticAddrs()...)
if expected, actual := 3, len(addrs); expected != actual {
t.Fatalf("expected %d addresses, found %d", expected, actual)
}
for _, addr := range addrs {
u := "http://" + addr + "/health"
resp, err := client.Get(u)
if err != nil {
t.Fatalf("failed to GET %s: %s", u, err)
}
defer resp.Body.Close()
if expected, actual := http.StatusOK, resp.StatusCode; expected != actual {
t.Errorf("resp status: expected %d, got %d", expected, actual)
}
if expected, actual := 2, resp.ProtoMajor; expected != actual {
t.Errorf("resp.ProtoMajor: expected %d, got %d", expected, actual)
}
}
}