rename NewPatRouter to NewRouter

master
kevin 4 years ago
parent dfe6e88529
commit 737cd4751a

@ -57,7 +57,7 @@ func (s *engine) SetUnsignedCallback(callback handler.UnsignedCallback) {
}
func (s *engine) Start() error {
return s.StartWithRouter(router.NewPatRouter())
return s.StartWithRouter(router.NewRouter())
}
func (s *engine) StartWithRouter(router httpx.Router) error {

@ -21,18 +21,18 @@ var (
ErrInvalidPath = errors.New("path must begin with '/'")
)
type PatRouter struct {
type patRouter struct {
trees map[string]*search.Tree
notFound http.Handler
}
func NewPatRouter() httpx.Router {
return &PatRouter{
func NewRouter() httpx.Router {
return &patRouter{
trees: make(map[string]*search.Tree),
}
}
func (pr *PatRouter) Handle(method, reqPath string, handler http.Handler) error {
func (pr *patRouter) Handle(method, reqPath string, handler http.Handler) error {
if !validMethod(method) {
return ErrInvalidMethod
}
@ -51,7 +51,7 @@ func (pr *PatRouter) Handle(method, reqPath string, handler http.Handler) error
}
}
func (pr *PatRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (pr *patRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
reqPath := path.Clean(r.URL.Path)
if tree, ok := pr.trees[r.Method]; ok {
if result, ok := tree.Search(reqPath); ok {
@ -71,11 +71,11 @@ func (pr *PatRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
func (pr *PatRouter) SetNotFoundHandler(handler http.Handler) {
func (pr *patRouter) SetNotFoundHandler(handler http.Handler) {
pr.notFound = handler
}
func (pr *PatRouter) handleNotFound(w http.ResponseWriter, r *http.Request) {
func (pr *patRouter) handleNotFound(w http.ResponseWriter, r *http.Request) {
if pr.notFound != nil {
pr.notFound.ServeHTTP(w, r)
} else {
@ -83,7 +83,7 @@ func (pr *PatRouter) handleNotFound(w http.ResponseWriter, r *http.Request) {
}
}
func (pr *PatRouter) methodNotAllowed(method, path string) (string, bool) {
func (pr *patRouter) methodNotAllowed(method, path string) (string, bool) {
var allows []string
for treeMethod, tree := range pr.trees {

@ -47,7 +47,7 @@ func TestPatRouterHandleErrors(t *testing.T) {
for _, test := range tests {
t.Run(test.method, func(t *testing.T) {
router := NewPatRouter()
router := NewRouter()
err := router.Handle(test.method, test.path, nil)
assert.Error(t, ErrInvalidMethod, err)
})
@ -56,7 +56,7 @@ func TestPatRouterHandleErrors(t *testing.T) {
func TestPatRouterNotFound(t *testing.T) {
var notFound bool
router := NewPatRouter()
router := NewRouter()
router.SetNotFoundHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
notFound = true
}))
@ -87,7 +87,7 @@ func TestPatRouter(t *testing.T) {
for _, test := range tests {
t.Run(test.method+":"+test.path, func(t *testing.T) {
routed := false
router := NewPatRouter()
router := NewRouter()
err := router.Handle(test.method, "/a/:b", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
routed = true
assert.Equal(t, 1, len(context.Vars(r)))
@ -125,7 +125,7 @@ func TestParseSlice(t *testing.T) {
assert.Nil(t, err)
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
rt := NewPatRouter()
rt := NewRouter()
err = rt.Handle(http.MethodPost, "/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
v := struct {
Names []string `form:"names"`
@ -149,7 +149,7 @@ func TestParseJsonPost(t *testing.T) {
assert.Nil(t, err)
r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(
w http.ResponseWriter, r *http.Request) {
v := struct {
@ -181,7 +181,7 @@ func TestParseJsonPostWithIntSlice(t *testing.T) {
assert.Nil(t, err)
r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(
w http.ResponseWriter, r *http.Request) {
v := struct {
@ -209,7 +209,7 @@ func TestParseJsonPostError(t *testing.T) {
assert.Nil(t, err)
r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
v := struct {
@ -237,7 +237,7 @@ func TestParseJsonPostInvalidRequest(t *testing.T) {
assert.Nil(t, err)
r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodPost, "/", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
v := struct {
@ -259,7 +259,7 @@ func TestParseJsonPostRequired(t *testing.T) {
assert.Nil(t, err)
r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
v := struct {
@ -282,7 +282,7 @@ func TestParsePath(t *testing.T) {
r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017", nil)
assert.Nil(t, err)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
v := struct {
@ -307,7 +307,7 @@ func TestParsePathRequired(t *testing.T) {
r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin", nil)
assert.Nil(t, err)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodGet, "/:name/", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
v := struct {
@ -328,7 +328,7 @@ func TestParseQuery(t *testing.T) {
r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000", nil)
assert.Nil(t, err)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
v := struct {
@ -353,7 +353,7 @@ func TestParseQueryRequired(t *testing.T) {
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever", nil)
assert.Nil(t, err)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
v := struct {
Nickname string `form:"nickname"`
@ -373,7 +373,7 @@ func TestParseOptional(t *testing.T) {
r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=", nil)
assert.Nil(t, err)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
v := struct {
@ -414,7 +414,7 @@ func TestParseNestedInRequestEmpty(t *testing.T) {
}
)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
var v WrappedRequest
@ -453,7 +453,7 @@ func TestParsePtrInRequest(t *testing.T) {
}
)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
var v WrappedRequest
@ -484,7 +484,7 @@ func TestParsePtrInRequestEmpty(t *testing.T) {
}
)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodPost, "/kevin", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
var v WrappedRequest
@ -501,7 +501,7 @@ func TestParseQueryOptional(t *testing.T) {
r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=", nil)
assert.Nil(t, err)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
v := struct {
@ -526,7 +526,7 @@ func TestParse(t *testing.T) {
r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000", nil)
assert.Nil(t, err)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
v := struct {
@ -564,7 +564,7 @@ func TestParseWrappedRequest(t *testing.T) {
}
)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
var v WrappedRequest
@ -596,7 +596,7 @@ func TestParseWrappedGetRequestWithJsonHeader(t *testing.T) {
}
)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
var v WrappedRequest
@ -629,7 +629,7 @@ func TestParseWrappedHeadRequestWithJsonHeader(t *testing.T) {
}
)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodHead, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
var v WrappedRequest
@ -661,7 +661,7 @@ func TestParseWrappedRequestPtr(t *testing.T) {
}
)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
var v WrappedRequest
@ -684,7 +684,7 @@ func TestParseWithAll(t *testing.T) {
assert.Nil(t, err)
r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
v := struct {
Name string `path:"name"`
@ -715,7 +715,7 @@ func TestParseWithAllUtf8(t *testing.T) {
assert.Nil(t, err)
r.Header.Set(httpx.ContentType, applicationJsonWithUtf8)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
v := struct {
@ -746,7 +746,7 @@ func TestParseWithMissingForm(t *testing.T) {
bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
assert.Nil(t, err)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
v := struct {
@ -773,7 +773,7 @@ func TestParseWithMissingAllForms(t *testing.T) {
bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
assert.Nil(t, err)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
v := struct {
@ -799,7 +799,7 @@ func TestParseWithMissingJson(t *testing.T) {
bytes.NewBufferString(`{"location": "shanghai"}`))
assert.Nil(t, err)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
v := struct {
@ -825,7 +825,7 @@ func TestParseWithMissingAllJsons(t *testing.T) {
r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000", nil)
assert.Nil(t, err)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
v := struct {
@ -852,7 +852,7 @@ func TestParseWithMissingPath(t *testing.T) {
bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
assert.Nil(t, err)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
v := struct {
@ -879,7 +879,7 @@ func TestParseWithMissingAllPaths(t *testing.T) {
bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
assert.Nil(t, err)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
v := struct {
@ -906,7 +906,7 @@ func TestParseGetWithContentLengthHeader(t *testing.T) {
r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
r.Header.Set(contentLength, "1024")
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodGet, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
v := struct {
@ -933,7 +933,7 @@ func TestParseJsonPostWithTypeMismatch(t *testing.T) {
assert.Nil(t, err)
r.Header.Set(httpx.ContentType, applicationJsonWithUtf8)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
v := struct {
@ -959,7 +959,7 @@ func TestParseJsonPostWithInt2String(t *testing.T) {
assert.Nil(t, err)
r.Header.Set(httpx.ContentType, applicationJsonWithUtf8)
router := NewPatRouter()
router := NewRouter()
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
v := struct {
@ -980,7 +980,7 @@ func TestParseJsonPostWithInt2String(t *testing.T) {
func BenchmarkPatRouter(b *testing.B) {
b.ReportAllocs()
router := NewPatRouter()
router := NewRouter()
router.Handle(http.MethodGet, "/api/:user/:name", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}))
w := &mockedResponseWriter{}

@ -14,7 +14,7 @@ import (
func TestWithMiddleware(t *testing.T) {
m := make(map[string]string)
router := router.NewPatRouter()
router := router.NewRouter()
handler := func(w http.ResponseWriter, r *http.Request) {
var v struct {
Nickname string `form:"nickname"`
@ -71,7 +71,7 @@ func TestWithMiddleware(t *testing.T) {
func TestMultiMiddleware(t *testing.T) {
m := make(map[string]string)
router := router.NewPatRouter()
router := router.NewRouter()
handler := func(w http.ResponseWriter, r *http.Request) {
var v struct {
Nickname string `form:"nickname"`

Loading…
Cancel
Save