Skip to content

Commit

Permalink
Add missing shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Andres Virviescas Santana committed Sep 3, 2020
1 parent 893131d commit fc8225f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
20 changes: 15 additions & 5 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ func (g *Group) HEAD(path string, handler fasthttp.RequestHandler) {
g.router.HEAD(g.prefix+path, handler)
}

// OPTIONS is a shortcut for group.Handle(fasthttp.MethodOptions, path, handler)
func (g *Group) OPTIONS(path string, handler fasthttp.RequestHandler) {
g.router.OPTIONS(g.prefix+path, handler)
}

// POST is a shortcut for group.Handle(fasthttp.MethodPost, path, handler)
func (g *Group) POST(path string, handler fasthttp.RequestHandler) {
g.router.POST(g.prefix+path, handler)
Expand All @@ -43,6 +38,21 @@ func (g *Group) DELETE(path string, handler fasthttp.RequestHandler) {
g.router.DELETE(g.prefix+path, handler)
}

// OPTIONS is a shortcut for group.Handle(fasthttp.MethodOptions, path, handler)
func (g *Group) CONNECT(path string, handler fasthttp.RequestHandler) {
g.router.CONNECT(g.prefix+path, handler)
}

// OPTIONS is a shortcut for group.Handle(fasthttp.MethodOptions, path, handler)
func (g *Group) OPTIONS(path string, handler fasthttp.RequestHandler) {
g.router.OPTIONS(g.prefix+path, handler)
}

// OPTIONS is a shortcut for group.Handle(fasthttp.MethodOptions, path, handler)
func (g *Group) TRACE(path string, handler fasthttp.RequestHandler) {
g.router.TRACE(g.prefix+path, handler)
}

// ANY is a shortcut for group.Handle(router.MethodWild, path, handler)
//
// WARNING: Use only for routes where the request method is not important
Expand Down
4 changes: 3 additions & 1 deletion group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ func TestGroup_shortcutsAndHandle(t *testing.T) {
shortcuts := []func(path string, handler fasthttp.RequestHandler){
g.GET,
g.HEAD,
g.OPTIONS,
g.POST,
g.PUT,
g.PATCH,
g.DELETE,
g.CONNECT,
g.OPTIONS,
g.TRACE,
g.ANY,
}

Expand Down
20 changes: 15 additions & 5 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,6 @@ func (r *Router) HEAD(path string, handler fasthttp.RequestHandler) {
r.Handle(fasthttp.MethodHead, path, handler)
}

// OPTIONS is a shortcut for router.Handle(fasthttp.MethodOptions, path, handler)
func (r *Router) OPTIONS(path string, handler fasthttp.RequestHandler) {
r.Handle(fasthttp.MethodOptions, path, handler)
}

// POST is a shortcut for router.Handle(fasthttp.MethodPost, path, handler)
func (r *Router) POST(path string, handler fasthttp.RequestHandler) {
r.Handle(fasthttp.MethodPost, path, handler)
Expand All @@ -140,6 +135,21 @@ func (r *Router) DELETE(path string, handler fasthttp.RequestHandler) {
r.Handle(fasthttp.MethodDelete, path, handler)
}

// CONNECT is a shortcut for router.Handle(fasthttp.MethodConnect, path, handler)
func (r *Router) CONNECT(path string, handler fasthttp.RequestHandler) {
r.Handle(fasthttp.MethodConnect, path, handler)
}

// OPTIONS is a shortcut for router.Handle(fasthttp.MethodOptions, path, handler)
func (r *Router) OPTIONS(path string, handler fasthttp.RequestHandler) {
r.Handle(fasthttp.MethodOptions, path, handler)
}

// TRACE is a shortcut for router.Handle(fasthttp.MethodTrace, path, handler)
func (r *Router) TRACE(path string, handler fasthttp.RequestHandler) {
r.Handle(fasthttp.MethodTrace, path, handler)
}

// ANY is a shortcut for router.Handle(router.MethodWild, path, handler)
//
// WARNING: Use only for routes where the request method is not important
Expand Down
34 changes: 25 additions & 9 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestRouter(t *testing.T) {
}

func TestRouterAPI(t *testing.T) {
var handled, get, head, options, post, put, patch, delete, any bool
var handled, get, head, post, put, patch, delete, connect, options, trace, any bool

httpHandler := func(ctx *fasthttp.RequestCtx) {
handled = true
Expand All @@ -160,9 +160,6 @@ func TestRouterAPI(t *testing.T) {
router.HEAD("/HEAD", func(ctx *fasthttp.RequestCtx) {
head = true
})
router.OPTIONS("/OPTIONS", func(ctx *fasthttp.RequestCtx) {
options = true
})
router.POST("/POST", func(ctx *fasthttp.RequestCtx) {
post = true
})
Expand All @@ -175,6 +172,15 @@ func TestRouterAPI(t *testing.T) {
router.DELETE("/DELETE", func(ctx *fasthttp.RequestCtx) {
delete = true
})
router.CONNECT("/CONNECT", func(ctx *fasthttp.RequestCtx) {
connect = true
})
router.OPTIONS("/OPTIONS", func(ctx *fasthttp.RequestCtx) {
options = true
})
router.TRACE("/TRACE", func(ctx *fasthttp.RequestCtx) {
trace = true
})
router.ANY("/ANY", func(ctx *fasthttp.RequestCtx) {
any = true
})
Expand All @@ -198,11 +204,6 @@ func TestRouterAPI(t *testing.T) {
t.Error("routing HEAD failed")
}

request(fasthttp.MethodOptions, "/OPTIONS")
if !options {
t.Error("routing OPTIONS failed")
}

request(fasthttp.MethodPost, "/POST")
if !post {
t.Error("routing POST failed")
Expand All @@ -223,6 +224,21 @@ func TestRouterAPI(t *testing.T) {
t.Error("routing DELETE failed")
}

request(fasthttp.MethodConnect, "/CONNECT")
if !connect {
t.Error("routing CONNECT failed")
}

request(fasthttp.MethodOptions, "/OPTIONS")
if !options {
t.Error("routing OPTIONS failed")
}

request(fasthttp.MethodTrace, "/TRACE")
if !trace {
t.Error("routing TRACE failed")
}

request(fasthttp.MethodGet, "/Handler")
if !handled {
t.Error("routing Handler failed")
Expand Down

0 comments on commit fc8225f

Please sign in to comment.