Add NB_PROXY_UPSTREAM_HTTP_VERSION to select the upstream HTTP version

This commit is contained in:
Viktor Liu
2026-09-03 15:23:31 +02:00
parent fc2189bb49
commit 735a6d985f
4 changed files with 86 additions and 25 deletions
+17 -11
View File
@@ -5,6 +5,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
@@ -85,26 +86,31 @@ func TestMultiTransport_AppliesEnvOverridesToDirect(t *testing.T) {
"env tuning must also apply to the insecure-skip-verify direct transport")
}
// TestMultiTransport_ForceAttemptHTTP2 pins the protocol actually
// TestMultiTransport_UpstreamHTTPVersion pins the protocol actually
// negotiated with an HTTPS upstream that offers both h2 and http/1.1.
// The direct transports dial through a custom DialContext, so net/http
// only reaches for h2 while ForceAttemptHTTP2 is set; clearing it via
// NB_PROXY_FORCE_ATTEMPT_HTTP2 must leave the request on HTTP/1.1.
func TestMultiTransport_ForceAttemptHTTP2(t *testing.T) {
// The request rides the insecure clone, so this also covers the version
// surviving http.Transport.Clone.
func TestMultiTransport_UpstreamHTTPVersion(t *testing.T) {
tests := []struct {
name string
env string
wantProto string
}{
{name: "default negotiates h2", env: "", wantProto: "HTTP/2.0"},
{name: "opt-out stays on http/1.1", env: "false", wantProto: "HTTP/1.1"},
{name: "explicit opt-in negotiates h2", env: "true", wantProto: "HTTP/2.0"},
{name: "unset negotiates h2", env: "", wantProto: "HTTP/2.0"},
{name: "auto negotiates h2", env: "auto", wantProto: "HTTP/2.0"},
{name: "1.1 pins http/1.1", env: "1.1", wantProto: "HTTP/1.1"},
{name: "2 negotiates h2", env: "2", wantProto: "HTTP/2.0"},
{name: "unsupported value keeps the default", env: "http3", wantProto: "HTTP/2.0"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if tc.env != "" {
t.Setenv(EnvForceAttemptHTTP2, tc.env)
// t.Setenv registers the restore for whatever the process
// inherited; unsetting afterwards lets the default row
// exercise a genuinely absent variable.
t.Setenv(EnvUpstreamHTTPVersion, tc.env)
if tc.env == "" {
require.NoError(t, os.Unsetenv(EnvUpstreamHTTPVersion))
}
// The test server's certificate isn't in any root pool, so the
@@ -128,7 +134,7 @@ func TestMultiTransport_ForceAttemptHTTP2(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, tc.wantProto, resp.Proto,
"client-side protocol must follow %s=%q", EnvForceAttemptHTTP2, tc.env)
"client-side protocol must follow %s=%q", EnvUpstreamHTTPVersion, tc.env)
assert.Equal(t, tc.wantProto, string(body),
"the upstream must see the same protocol the client negotiated")
})