]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: add test for registration errors
authorJonathan Amsterdam <jba@google.com>
Wed, 13 Sep 2023 19:58:25 +0000 (15:58 -0400)
committerJonathan Amsterdam <jba@google.com>
Wed, 13 Sep 2023 21:00:34 +0000 (21:00 +0000)
Change-Id: Ice378e2f1c4cce180f020683d25070c5ae1edbad
Reviewed-on: https://go-review.googlesource.com/c/go/+/528255
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

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

index 6fe917e0867d4d7e30af07dcca7b933f597e7c8b..7ce078ced41b79a4fa95a544d46356719c9ceeaa 100644 (file)
@@ -2605,7 +2605,7 @@ func (mux *ServeMux) registerErr(pattern string, handler Handler) error {
 
        pat, err := parsePattern(pattern)
        if err != nil {
-               return err
+               return fmt.Errorf("parsing %q: %w", pattern, err)
        }
 
        // Get the caller's location, for better conflict error messages.
index 0e7bdb2f377161b769e996bbb0594fe555418640..b0cc093d43dd3992ad68c6e2524a56b11435c7ad 100644 (file)
@@ -9,6 +9,7 @@ package http
 import (
        "fmt"
        "net/url"
+       "regexp"
        "testing"
        "time"
 )
@@ -117,6 +118,35 @@ func TestFindHandler(t *testing.T) {
        }
 }
 
+func TestRegisterErr(t *testing.T) {
+       mux := NewServeMux()
+       h := &handler{}
+       mux.Handle("/a", h)
+
+       for _, test := range []struct {
+               pattern    string
+               handler    Handler
+               wantRegexp string
+       }{
+               {"", h, "invalid pattern"},
+               {"/", nil, "nil handler"},
+               {"/", HandlerFunc(nil), "nil handler"},
+               {"/{x", h, `parsing "/\{x": bad wildcard segment`},
+               {"/a", h, `conflicts with pattern.* \(registered at .*/server_test.go:\d+`},
+       } {
+               t.Run(fmt.Sprintf("%s:%#v", test.pattern, test.handler), func(t *testing.T) {
+                       err := mux.registerErr(test.pattern, test.handler)
+                       if err == nil {
+                               t.Fatal("got nil error")
+                       }
+                       re := regexp.MustCompile(test.wantRegexp)
+                       if g := err.Error(); !re.MatchString(g) {
+                               t.Errorf("\ngot %q\nwant string matching %q", g, test.wantRegexp)
+                       }
+               })
+       }
+}
+
 func TestExactMatch(t *testing.T) {
        for _, test := range []struct {
                pattern string