]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: add a test for an empty ServeMux
authorJonathan Amsterdam <jba@google.com>
Mon, 25 Sep 2023 13:46:32 +0000 (09:46 -0400)
committerJonathan Amsterdam <jba@google.com>
Mon, 2 Oct 2023 20:28:47 +0000 (20:28 +0000)
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 <dneil@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/net/http/server.go
src/net/http/server_test.go

index f456e43cce5b61e9c7d4ddcc88eab38a391439a4..017a818846717110a158c99e0c86e8a347c180d1 100644 (file)
@@ -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
index d418573452ded85832be225079eea007ee4cd4d4..e81e3bb6b00f9b9c80bdbba40786351bba02c925 100644 (file)
@@ -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{}