Files
releases/v1/test/e2e/h2c/h2c_test.go
T
Anders Eknert afb30d3f9d Add gocritic linter, fix a bunch of stuff (#7377)
Brace yourselves! For there are many touched files here. No changes
in semantics however.

Spent a long time trying out the various optional rules gocritic
provides, and settled for a few of them. There are more I really
like, but that would take many hours to address across the codebase.

Perhaps others find gocritic too pedantic? If so, we can merge the
fixes without enabling the rule.

Signed-off-by: Anders Eknert <anders@styra.com>
2025-02-24 16:28:41 +01:00

69 lines
1.5 KiB
Go

package h2c_test
import (
"crypto/tls"
"flag"
"net"
"net/http"
"os"
"testing"
"golang.org/x/net/http2"
"github.com/open-policy-agent/opa/v1/test/e2e"
)
var testRuntime *e2e.TestRuntime
func TestMain(m *testing.M) {
flag.Parse()
testServerParams := e2e.NewAPIServerTestParams()
testServerParams.Addrs = &[]string{"localhost:0"}
testServerParams.DiagnosticAddrs = &[]string{"localhost: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 := 2, 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)
}
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)
}
resp.Body.Close()
}
}