Skip to content

Commit

Permalink
rename NewPatRouter to NewRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Oct 20, 2020
1 parent dfe6e88 commit 737cd47
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 46 deletions.
2 changes: 1 addition & 1 deletion rest/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 8 additions & 8 deletions rest/router/patrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -71,19 +71,19 @@ 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 {
http.NotFound(w, r)
}
}

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 {
Expand Down
70 changes: 35 additions & 35 deletions rest/router/patrouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -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
}))
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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"`
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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"`
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"`
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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{}
Expand Down
4 changes: 2 additions & 2 deletions rest/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
Expand Down

0 comments on commit 737cd47

Please sign in to comment.