Skip to content

Commit

Permalink
Fixes #25 Added custom methods (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammednagy authored Jun 2, 2020
1 parent b2db51f commit 30007af
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions gearbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ type Gearbox interface {
Connect(path string, handlers ...handlerFunc) error
Options(path string, handlers ...handlerFunc) error
Trace(path string, handlers ...handlerFunc) error
Method(method, path string, handlers ...handlerFunc) error
Fallback(handlers ...handlerFunc) error
Use(middlewares ...handlerFunc)
}
Expand Down Expand Up @@ -232,6 +233,11 @@ func (gb *gearbox) Trace(path string, handlers ...handlerFunc) error {
return gb.registerRoute([]byte(MethodTrace), []byte(path), handlers)
}

// Trace registers an http relevant method
func (gb *gearbox) Method(method, path string, handlers ...handlerFunc) error {
return gb.registerRoute([]byte(method), []byte(path), handlers)
}

// Fallback registers an http handler only fired when no other routes match with request
func (gb *gearbox) Fallback(handlers ...handlerFunc) error {
return gb.registerFallback(handlers)
Expand Down
4 changes: 4 additions & 0 deletions gearbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func registerRoute(gb Gearbox, method, path string, handler func(ctx *Context))
gb.Options(path, handler)
case MethodTrace:
gb.Trace(path, handler)
default:
gb.Method(method, path, handler)
}
}

Expand All @@ -149,6 +151,7 @@ func TestMethods(t *testing.T) {
{method: MethodOptions, path: "/user/204/setting", handler: emptyHandler},
{method: MethodTrace, path: "/users/*", handler: emptyHandler},
{method: MethodTrace, path: "/users/test", handler: emptyHandler},
{method: "CUSTOM", path: "/users/test/private", handler: emptyHandler},
}

// get instance of gearbox
Expand Down Expand Up @@ -184,6 +187,7 @@ func TestMethods(t *testing.T) {
{method: MethodConnect, path: "/user/204", statusCode: StatusOK},
{method: MethodOptions, path: "/user/204/setting", statusCode: StatusOK},
{method: MethodTrace, path: "/users/testing", statusCode: StatusOK},
{method: "CUSTOM", path: "/users/test/private", statusCode: StatusOK},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 30007af

Please sign in to comment.