test(sowerd): stop the authority checks from passing for the wrong reason

Review of the previous commit found the validation test green for a reason it
did not claim: SowerdConfig with an empty FakeSite fails Validate on
"fake site is required" before validateSiteRoutes runs, so the bogus authority
was never exercised. Set a valid FakeSite and assert the error names the
authority field, which the assertion can no longer satisfy by accident.

Also fold the router mapping case into the end-to-end test and drop the
standalone TestSiteRouterPreserveHost: the positive mapping is already implied
by the wire assertion, and the default case costs one extra route and two
checks there. Both directions were verified by mutation — inverting the mapping
and letting Validate accept the bogus value each turn the covering test red.
This commit is contained in:
wweir
2026-10-12 16:09:19 +08:00
parent 80dfa072e9
commit b42b2738e7
2 changed files with 25 additions and 29 deletions
+16 -27
View File
@@ -539,10 +539,11 @@ func TestHandleConnRoutesFallbackBySNI(t *testing.T) {
}
}
// TestHandleConnPreservesHostForOptedInRoute pins the config-to-wire chain: a
// route that asked for preserve_host reaches its upstream with the client's
// authority. An upstream that compares Origin against the Host header rejects
// the rewritten host, and that failure only shows up at request time.
// TestHandleConnPreservesHostForOptedInRoute pins the whole chain: the route
// mapping for both authority values, then the wire, where an opted-in route
// must reach its upstream with the client's authority. An upstream that
// compares Origin against the Host header rejects the rewritten host, and that
// failure only shows up at request time.
func TestHandleConnPreservesHostForOptedInRoute(t *testing.T) {
const authority = "keep.example.com"
gotCh := make(chan string, 1)
@@ -554,7 +555,18 @@ func TestHandleConnPreservesHostForOptedInRoute(t *testing.T) {
router := newSiteRouter([]config.SiteRoute{
{Domains: []string{authority}, Upstream: upstream.URL, Authority: config.AuthorityClient},
{Domains: []string{"rewrite.example.com"}, Upstream: "http://127.0.0.1:9001"},
})
if entry := router.lookup(authority); entry == nil {
t.Fatal("lookup returned no entry for the opted-in route")
} else if !entry.preserveHost {
t.Error("opted-in route does not forward the client's Host")
}
if entry := router.lookup("rewrite.example.com"); entry == nil {
t.Fatal("lookup returned no entry for the default route")
} else if entry.preserveHost {
t.Error("default route forwards the client's Host, want the upstream host")
}
certServer := httptest.NewTLSServer(http.NotFoundHandler())
cert := certServer.TLS.Certificates[0]
@@ -589,29 +601,6 @@ func TestHandleConnPreservesHostForOptedInRoute(t *testing.T) {
}
}
func TestSiteRouterPreserveHost(t *testing.T) {
router := newSiteRouter([]config.SiteRoute{
{Domains: []string{"keep.example.com"}, Upstream: "http://127.0.0.1:9000", Authority: config.AuthorityClient},
{Domains: []string{"rewrite.example.com"}, Upstream: "http://127.0.0.1:9001"},
})
keep := router.lookup("keep.example.com")
if keep == nil {
t.Fatal("lookup returned nil for the preserve_host route")
}
if !keep.preserveHost {
t.Error("preserve_host route entry does not preserve the Host header")
}
rewrite := router.lookup("rewrite.example.com")
if rewrite == nil {
t.Fatal("lookup returned nil for the default route")
}
if rewrite.preserveHost {
t.Error("default route entry preserves the Host header, want the upstream host")
}
}
func TestSiteRouterResolvePathRoutes(t *testing.T) {
router := newSiteRouter([]config.SiteRoute{
{
+9 -2
View File
@@ -9,6 +9,7 @@ import (
"math/big"
"os"
"path/filepath"
"strings"
"testing"
"time"
@@ -147,13 +148,19 @@ upstream = "http://127.0.0.1:9001"
func TestSowerdConfigSiteRouteAuthorityRejectsUnknownValue(t *testing.T) {
t.Parallel()
cfg := SowerdConfig{Password: "secret"}
// FakeSite is set so Validate reaches the route check: an earlier field
// failing first would make this test green without exercising the authority.
cfg := SowerdConfig{Password: "secret", FakeSite: "127.0.0.1:18080"}
cfg.SiteRoutes = []SiteRoute{
{Domains: []string{"a.example.com"}, Upstream: "http://127.0.0.1:9000", Authority: "keep"},
}
if err := cfg.Validate(); err == nil {
err := cfg.Validate()
if err == nil {
t.Fatal("Validate accepted an unknown authority, want an error")
}
if !strings.Contains(err.Error(), "authority") {
t.Fatalf("Validate error = %v, want it to name the authority field", err)
}
}
func TestSowerdConfigValidate(t *testing.T) {