From 32b6d2d9a849a0f0120e9139b403831669373b79 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Mon, 25 Sep 2023 09:46:32 -0400 Subject: [PATCH] net/http: add a test for an empty ServeMux Make sure a ServeMux with no patterns is well-behaved. Updates #61410. Change-Id: Ib3eb85b384e1309e785663902d2c45ae01e64807 Reviewed-on: https://go-review.googlesource.com/c/go/+/530479 Reviewed-by: Damien Neil Run-TryBot: Jonathan Amsterdam TryBot-Result: Gopher Robot --- src/net/http/server.go | 2 -- src/net/http/server_test.go | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/net/http/server.go b/src/net/http/server.go index f456e43cce..017a818846 100644 --- a/src/net/http/server.go +++ b/src/net/http/server.go @@ -33,8 +33,6 @@ import ( "golang.org/x/net/http/httpguts" ) -// TODO(jba): test - // Errors used by the HTTP server. var ( // ErrBodyNotAllowed is returned by ResponseWriter.Write calls diff --git a/src/net/http/server_test.go b/src/net/http/server_test.go index d418573452..e81e3bb6b0 100644 --- a/src/net/http/server_test.go +++ b/src/net/http/server_test.go @@ -118,6 +118,20 @@ func TestFindHandler(t *testing.T) { } } +func TestEmptyServeMux(t *testing.T) { + // Verify that a ServeMux with nothing registered + // doesn't panic. + mux := NewServeMux() + var r Request + r.Method = "GET" + r.Host = "example.com" + r.URL = &url.URL{Path: "/"} + _, p := mux.Handler(&r) + if p != "" { + t.Errorf(`got %q, want ""`, p) + } +} + func TestRegisterErr(t *testing.T) { mux := NewServeMux() h := &handler{}