From f2db0dca0b0399c08319d22cbcbfa83be2bb781a Mon Sep 17 00:00:00 2001 From: Sean Liao Date: Sat, 19 Apr 2025 13:50:35 +0100 Subject: net/http/httptest: redirect example.com requests to server The default server cert used by NewServer already includes example.com in its DNSNames, and by default, the client's RootCA configuration means it won't trust a response from the real example.com. Fixes #31054 Change-Id: I0686977e5ffe2c2f22f3fc09a47ee8ecc44765db Reviewed-on: https://go-review.googlesource.com/c/go/+/666855 Reviewed-by: Damien Neil Reviewed-by: Carlos Amedee LUCI-TryBot-Result: Go LUCI --- src/net/http/httptest/server_test.go | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src/net/http/httptest/server_test.go') diff --git a/src/net/http/httptest/server_test.go b/src/net/http/httptest/server_test.go index c96a0ff337..f3cfa7c2db 100644 --- a/src/net/http/httptest/server_test.go +++ b/src/net/http/httptest/server_test.go @@ -293,3 +293,40 @@ func TestTLSServerWithHTTP2(t *testing.T) { }) } } + +func TestClientExampleCom(t *testing.T) { + modes := []struct { + proto string + host string + }{ + {"http", "example.com"}, + {"http", "foo.example.com"}, + {"https", "example.com"}, + {"https", "foo.example.com"}, + } + + for _, tt := range modes { + t.Run(tt.proto+" "+tt.host, func(t *testing.T) { + cst := NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("requested-hostname", r.Host) + })) + switch tt.proto { + case "https": + cst.EnableHTTP2 = true + cst.StartTLS() + default: + cst.Start() + } + + defer cst.Close() + + res, err := cst.Client().Get(tt.proto + "://" + tt.host) + if err != nil { + t.Fatalf("Failed to make request: %v", err) + } + if got, want := res.Header.Get("requested-hostname"), tt.host; got != want { + t.Fatalf("Requested hostname mismatch\ngot: %q\nwant: %q", got, want) + } + }) + } +} -- cgit v1.3-5-g45d5