mirror of
https://github.com/golang/go
synced 2024-11-18 14:04:45 -07:00
net/http: add test for registration errors
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>
This commit is contained in:
parent
6760f20ef5
commit
e277455164
@ -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.
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user